root / hw / virtio-net.c @ 4870852c
History | View | Annotate | Download (17.7 kB)
1 | fbe78f4f | aliguori | /*
|
---|---|---|---|
2 | fbe78f4f | aliguori | * Virtio Network Device
|
3 | fbe78f4f | aliguori | *
|
4 | fbe78f4f | aliguori | * Copyright IBM, Corp. 2007
|
5 | fbe78f4f | aliguori | *
|
6 | fbe78f4f | aliguori | * Authors:
|
7 | fbe78f4f | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | fbe78f4f | aliguori | *
|
9 | fbe78f4f | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | fbe78f4f | aliguori | * the COPYING file in the top-level directory.
|
11 | fbe78f4f | aliguori | *
|
12 | fbe78f4f | aliguori | */
|
13 | fbe78f4f | aliguori | |
14 | fbe78f4f | aliguori | #include "virtio.h" |
15 | fbe78f4f | aliguori | #include "net.h" |
16 | fbe78f4f | aliguori | #include "qemu-timer.h" |
17 | fbe78f4f | aliguori | #include "virtio-net.h" |
18 | fbe78f4f | aliguori | |
19 | f21c0ed9 | aliguori | #define VIRTIO_NET_VM_VERSION 6 |
20 | b6503ed9 | aliguori | |
21 | b6503ed9 | aliguori | #define MAC_TABLE_ENTRIES 32 |
22 | f21c0ed9 | aliguori | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
23 | 9d6271b8 | aliguori | |
24 | fbe78f4f | aliguori | typedef struct VirtIONet |
25 | fbe78f4f | aliguori | { |
26 | fbe78f4f | aliguori | VirtIODevice vdev; |
27 | 79674068 | aliguori | uint8_t mac[ETH_ALEN]; |
28 | 554c97dd | aliguori | uint16_t status; |
29 | fbe78f4f | aliguori | VirtQueue *rx_vq; |
30 | fbe78f4f | aliguori | VirtQueue *tx_vq; |
31 | 3d11d36c | aliguori | VirtQueue *ctrl_vq; |
32 | fbe78f4f | aliguori | VLANClientState *vc; |
33 | fbe78f4f | aliguori | QEMUTimer *tx_timer; |
34 | fbe78f4f | aliguori | int tx_timer_active;
|
35 | fbe78f4f | aliguori | int mergeable_rx_bufs;
|
36 | 002437cd | aliguori | int promisc;
|
37 | 002437cd | aliguori | int allmulti;
|
38 | b6503ed9 | aliguori | struct {
|
39 | b6503ed9 | aliguori | int in_use;
|
40 | b6503ed9 | aliguori | uint8_t *macs; |
41 | b6503ed9 | aliguori | } mac_table; |
42 | f21c0ed9 | aliguori | uint32_t *vlans; |
43 | fbe78f4f | aliguori | } VirtIONet; |
44 | fbe78f4f | aliguori | |
45 | fbe78f4f | aliguori | /* TODO
|
46 | fbe78f4f | aliguori | * - we could suppress RX interrupt if we were so inclined.
|
47 | fbe78f4f | aliguori | */
|
48 | fbe78f4f | aliguori | |
49 | fbe78f4f | aliguori | static VirtIONet *to_virtio_net(VirtIODevice *vdev)
|
50 | fbe78f4f | aliguori | { |
51 | fbe78f4f | aliguori | return (VirtIONet *)vdev;
|
52 | fbe78f4f | aliguori | } |
53 | fbe78f4f | aliguori | |
54 | 0f03eca6 | aliguori | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
55 | fbe78f4f | aliguori | { |
56 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
57 | fbe78f4f | aliguori | struct virtio_net_config netcfg;
|
58 | fbe78f4f | aliguori | |
59 | 554c97dd | aliguori | netcfg.status = n->status; |
60 | 79674068 | aliguori | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
61 | fbe78f4f | aliguori | memcpy(config, &netcfg, sizeof(netcfg));
|
62 | fbe78f4f | aliguori | } |
63 | fbe78f4f | aliguori | |
64 | 0f03eca6 | aliguori | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
65 | 0f03eca6 | aliguori | { |
66 | 0f03eca6 | aliguori | VirtIONet *n = to_virtio_net(vdev); |
67 | 0f03eca6 | aliguori | struct virtio_net_config netcfg;
|
68 | 0f03eca6 | aliguori | |
69 | 0f03eca6 | aliguori | memcpy(&netcfg, config, sizeof(netcfg));
|
70 | 0f03eca6 | aliguori | |
71 | 79674068 | aliguori | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
|
72 | 79674068 | aliguori | memcpy(n->mac, netcfg.mac, ETH_ALEN); |
73 | 0f03eca6 | aliguori | qemu_format_nic_info_str(n->vc, n->mac); |
74 | 0f03eca6 | aliguori | } |
75 | 0f03eca6 | aliguori | } |
76 | 0f03eca6 | aliguori | |
77 | 554c97dd | aliguori | static void virtio_net_set_link_status(VLANClientState *vc) |
78 | 554c97dd | aliguori | { |
79 | 554c97dd | aliguori | VirtIONet *n = vc->opaque; |
80 | 554c97dd | aliguori | uint16_t old_status = n->status; |
81 | 554c97dd | aliguori | |
82 | 554c97dd | aliguori | if (vc->link_down)
|
83 | 554c97dd | aliguori | n->status &= ~VIRTIO_NET_S_LINK_UP; |
84 | 554c97dd | aliguori | else
|
85 | 554c97dd | aliguori | n->status |= VIRTIO_NET_S_LINK_UP; |
86 | 554c97dd | aliguori | |
87 | 554c97dd | aliguori | if (n->status != old_status)
|
88 | 554c97dd | aliguori | virtio_notify_config(&n->vdev); |
89 | 554c97dd | aliguori | } |
90 | 554c97dd | aliguori | |
91 | 002437cd | aliguori | static void virtio_net_reset(VirtIODevice *vdev) |
92 | 002437cd | aliguori | { |
93 | 002437cd | aliguori | VirtIONet *n = to_virtio_net(vdev); |
94 | 002437cd | aliguori | |
95 | 002437cd | aliguori | /* Reset back to compatibility mode */
|
96 | 002437cd | aliguori | n->promisc = 1;
|
97 | 002437cd | aliguori | n->allmulti = 0;
|
98 | b6503ed9 | aliguori | |
99 | f21c0ed9 | aliguori | /* Flush any MAC and VLAN filter table state */
|
100 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
101 | b6503ed9 | aliguori | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
|
102 | f21c0ed9 | aliguori | memset(n->vlans, 0, MAX_VLAN >> 3); |
103 | 002437cd | aliguori | } |
104 | 002437cd | aliguori | |
105 | fbe78f4f | aliguori | static uint32_t virtio_net_get_features(VirtIODevice *vdev)
|
106 | fbe78f4f | aliguori | { |
107 | 3d11d36c | aliguori | uint32_t features = (1 << VIRTIO_NET_F_MAC) |
|
108 | 3d11d36c | aliguori | (1 << VIRTIO_NET_F_STATUS) |
|
109 | b6503ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_VQ) |
|
110 | f21c0ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_RX) |
|
111 | f21c0ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_VLAN);
|
112 | fbe78f4f | aliguori | |
113 | fbe78f4f | aliguori | return features;
|
114 | fbe78f4f | aliguori | } |
115 | fbe78f4f | aliguori | |
116 | 8eca6b1b | aliguori | static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
|
117 | 8eca6b1b | aliguori | { |
118 | 8eca6b1b | aliguori | uint32_t features = 0;
|
119 | 8eca6b1b | aliguori | |
120 | 8eca6b1b | aliguori | /* Linux kernel 2.6.25. It understood MAC (as everyone must),
|
121 | 8eca6b1b | aliguori | * but also these: */
|
122 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_MAC);
|
123 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
|
124 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
|
125 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
|
126 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_ECN);
|
127 | 8eca6b1b | aliguori | |
128 | 8eca6b1b | aliguori | return features & virtio_net_get_features(vdev);
|
129 | 8eca6b1b | aliguori | } |
130 | 8eca6b1b | aliguori | |
131 | fbe78f4f | aliguori | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
132 | fbe78f4f | aliguori | { |
133 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
134 | fbe78f4f | aliguori | |
135 | fbe78f4f | aliguori | n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
|
136 | fbe78f4f | aliguori | } |
137 | fbe78f4f | aliguori | |
138 | 002437cd | aliguori | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
139 | 002437cd | aliguori | VirtQueueElement *elem) |
140 | 002437cd | aliguori | { |
141 | 002437cd | aliguori | uint8_t on; |
142 | 002437cd | aliguori | |
143 | 002437cd | aliguori | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) { |
144 | 002437cd | aliguori | fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
|
145 | 002437cd | aliguori | exit(1);
|
146 | 002437cd | aliguori | } |
147 | 002437cd | aliguori | |
148 | 002437cd | aliguori | on = ldub_p(elem->out_sg[1].iov_base);
|
149 | 002437cd | aliguori | |
150 | 002437cd | aliguori | if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
|
151 | 002437cd | aliguori | n->promisc = on; |
152 | 002437cd | aliguori | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) |
153 | 002437cd | aliguori | n->allmulti = on; |
154 | 002437cd | aliguori | else
|
155 | 002437cd | aliguori | return VIRTIO_NET_ERR;
|
156 | 002437cd | aliguori | |
157 | 002437cd | aliguori | return VIRTIO_NET_OK;
|
158 | 002437cd | aliguori | } |
159 | 002437cd | aliguori | |
160 | b6503ed9 | aliguori | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
161 | b6503ed9 | aliguori | VirtQueueElement *elem) |
162 | b6503ed9 | aliguori | { |
163 | b6503ed9 | aliguori | struct virtio_net_ctrl_mac mac_data;
|
164 | b6503ed9 | aliguori | |
165 | b6503ed9 | aliguori | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 || |
166 | b6503ed9 | aliguori | elem->out_sg[1].iov_len < sizeof(mac_data) || |
167 | b6503ed9 | aliguori | elem->out_sg[2].iov_len < sizeof(mac_data)) |
168 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
169 | b6503ed9 | aliguori | |
170 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
171 | b6503ed9 | aliguori | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
|
172 | b6503ed9 | aliguori | |
173 | b6503ed9 | aliguori | mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
|
174 | b6503ed9 | aliguori | |
175 | b6503ed9 | aliguori | if (sizeof(mac_data.entries) + |
176 | b6503ed9 | aliguori | (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
|
177 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
178 | b6503ed9 | aliguori | |
179 | b6503ed9 | aliguori | if (mac_data.entries <= MAC_TABLE_ENTRIES) {
|
180 | b6503ed9 | aliguori | memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data), |
181 | b6503ed9 | aliguori | mac_data.entries * ETH_ALEN); |
182 | b6503ed9 | aliguori | n->mac_table.in_use += mac_data.entries; |
183 | b6503ed9 | aliguori | } else {
|
184 | b6503ed9 | aliguori | n->promisc = 1;
|
185 | b6503ed9 | aliguori | return VIRTIO_NET_OK;
|
186 | b6503ed9 | aliguori | } |
187 | b6503ed9 | aliguori | |
188 | b6503ed9 | aliguori | mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
|
189 | b6503ed9 | aliguori | |
190 | b6503ed9 | aliguori | if (sizeof(mac_data.entries) + |
191 | b6503ed9 | aliguori | (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
|
192 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
193 | b6503ed9 | aliguori | |
194 | b6503ed9 | aliguori | if (mac_data.entries) {
|
195 | b6503ed9 | aliguori | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
|
196 | b6503ed9 | aliguori | memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN), |
197 | b6503ed9 | aliguori | elem->out_sg[2].iov_base + sizeof(mac_data), |
198 | b6503ed9 | aliguori | mac_data.entries * ETH_ALEN); |
199 | b6503ed9 | aliguori | n->mac_table.in_use += mac_data.entries; |
200 | b6503ed9 | aliguori | } else
|
201 | b6503ed9 | aliguori | n->allmulti = 1;
|
202 | b6503ed9 | aliguori | } |
203 | b6503ed9 | aliguori | |
204 | b6503ed9 | aliguori | return VIRTIO_NET_OK;
|
205 | b6503ed9 | aliguori | } |
206 | b6503ed9 | aliguori | |
207 | f21c0ed9 | aliguori | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
208 | f21c0ed9 | aliguori | VirtQueueElement *elem) |
209 | f21c0ed9 | aliguori | { |
210 | f21c0ed9 | aliguori | uint16_t vid; |
211 | f21c0ed9 | aliguori | |
212 | f21c0ed9 | aliguori | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) { |
213 | f21c0ed9 | aliguori | fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
|
214 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
215 | f21c0ed9 | aliguori | } |
216 | f21c0ed9 | aliguori | |
217 | f21c0ed9 | aliguori | vid = lduw_le_p(elem->out_sg[1].iov_base);
|
218 | f21c0ed9 | aliguori | |
219 | f21c0ed9 | aliguori | if (vid >= MAX_VLAN)
|
220 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
221 | f21c0ed9 | aliguori | |
222 | f21c0ed9 | aliguori | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
|
223 | f21c0ed9 | aliguori | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); |
224 | f21c0ed9 | aliguori | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) |
225 | f21c0ed9 | aliguori | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); |
226 | f21c0ed9 | aliguori | else
|
227 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
228 | f21c0ed9 | aliguori | |
229 | f21c0ed9 | aliguori | return VIRTIO_NET_OK;
|
230 | f21c0ed9 | aliguori | } |
231 | f21c0ed9 | aliguori | |
232 | 3d11d36c | aliguori | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
233 | 3d11d36c | aliguori | { |
234 | 002437cd | aliguori | VirtIONet *n = to_virtio_net(vdev); |
235 | 3d11d36c | aliguori | struct virtio_net_ctrl_hdr ctrl;
|
236 | 3d11d36c | aliguori | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
237 | 3d11d36c | aliguori | VirtQueueElement elem; |
238 | 3d11d36c | aliguori | |
239 | 3d11d36c | aliguori | while (virtqueue_pop(vq, &elem)) {
|
240 | 3d11d36c | aliguori | if ((elem.in_num < 1) || (elem.out_num < 1)) { |
241 | 3d11d36c | aliguori | fprintf(stderr, "virtio-net ctrl missing headers\n");
|
242 | 3d11d36c | aliguori | exit(1);
|
243 | 3d11d36c | aliguori | } |
244 | 3d11d36c | aliguori | |
245 | 3d11d36c | aliguori | if (elem.out_sg[0].iov_len < sizeof(ctrl) || |
246 | c6bb9a32 | aliguori | elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) { |
247 | 3d11d36c | aliguori | fprintf(stderr, "virtio-net ctrl header not in correct element\n");
|
248 | 3d11d36c | aliguori | exit(1);
|
249 | 3d11d36c | aliguori | } |
250 | 3d11d36c | aliguori | |
251 | 3d11d36c | aliguori | ctrl.class = ldub_p(elem.out_sg[0].iov_base);
|
252 | 3d11d36c | aliguori | ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class)); |
253 | 3d11d36c | aliguori | |
254 | 002437cd | aliguori | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
|
255 | 002437cd | aliguori | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); |
256 | b6503ed9 | aliguori | else if (ctrl.class == VIRTIO_NET_CTRL_MAC) |
257 | b6503ed9 | aliguori | status = virtio_net_handle_mac(n, ctrl.cmd, &elem); |
258 | f21c0ed9 | aliguori | else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) |
259 | f21c0ed9 | aliguori | status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); |
260 | 002437cd | aliguori | |
261 | 3d11d36c | aliguori | stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
|
262 | 3d11d36c | aliguori | |
263 | 3d11d36c | aliguori | virtqueue_push(vq, &elem, sizeof(status));
|
264 | 3d11d36c | aliguori | virtio_notify(vdev, vq); |
265 | 3d11d36c | aliguori | } |
266 | 3d11d36c | aliguori | } |
267 | 3d11d36c | aliguori | |
268 | fbe78f4f | aliguori | /* RX */
|
269 | fbe78f4f | aliguori | |
270 | fbe78f4f | aliguori | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) |
271 | fbe78f4f | aliguori | { |
272 | fbe78f4f | aliguori | } |
273 | fbe78f4f | aliguori | |
274 | fbe78f4f | aliguori | static int do_virtio_net_can_receive(VirtIONet *n, int bufsize) |
275 | fbe78f4f | aliguori | { |
276 | fbe78f4f | aliguori | if (!virtio_queue_ready(n->rx_vq) ||
|
277 | fbe78f4f | aliguori | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
278 | fbe78f4f | aliguori | return 0; |
279 | fbe78f4f | aliguori | |
280 | fbe78f4f | aliguori | if (virtio_queue_empty(n->rx_vq) ||
|
281 | fbe78f4f | aliguori | (n->mergeable_rx_bufs && |
282 | fbe78f4f | aliguori | !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
|
283 | fbe78f4f | aliguori | virtio_queue_set_notification(n->rx_vq, 1);
|
284 | fbe78f4f | aliguori | return 0; |
285 | fbe78f4f | aliguori | } |
286 | fbe78f4f | aliguori | |
287 | fbe78f4f | aliguori | virtio_queue_set_notification(n->rx_vq, 0);
|
288 | fbe78f4f | aliguori | return 1; |
289 | fbe78f4f | aliguori | } |
290 | fbe78f4f | aliguori | |
291 | fbe78f4f | aliguori | static int virtio_net_can_receive(void *opaque) |
292 | fbe78f4f | aliguori | { |
293 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
294 | fbe78f4f | aliguori | |
295 | fbe78f4f | aliguori | return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
|
296 | fbe78f4f | aliguori | } |
297 | fbe78f4f | aliguori | |
298 | fbe78f4f | aliguori | static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count) |
299 | fbe78f4f | aliguori | { |
300 | fbe78f4f | aliguori | int offset, i;
|
301 | fbe78f4f | aliguori | |
302 | fbe78f4f | aliguori | offset = i = 0;
|
303 | fbe78f4f | aliguori | while (offset < count && i < iovcnt) {
|
304 | fbe78f4f | aliguori | int len = MIN(iov[i].iov_len, count - offset);
|
305 | fbe78f4f | aliguori | memcpy(iov[i].iov_base, buf + offset, len); |
306 | fbe78f4f | aliguori | offset += len; |
307 | fbe78f4f | aliguori | i++; |
308 | fbe78f4f | aliguori | } |
309 | fbe78f4f | aliguori | |
310 | fbe78f4f | aliguori | return offset;
|
311 | fbe78f4f | aliguori | } |
312 | fbe78f4f | aliguori | |
313 | fbe78f4f | aliguori | static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, |
314 | 4689f4b3 | aliguori | const void *buf, size_t size, size_t hdr_len) |
315 | fbe78f4f | aliguori | { |
316 | 3f4cb3d3 | blueswir1 | struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base; |
317 | fbe78f4f | aliguori | int offset = 0; |
318 | fbe78f4f | aliguori | |
319 | fbe78f4f | aliguori | hdr->flags = 0;
|
320 | fbe78f4f | aliguori | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
321 | fbe78f4f | aliguori | |
322 | fbe78f4f | aliguori | /* We only ever receive a struct virtio_net_hdr from the tapfd,
|
323 | fbe78f4f | aliguori | * but we may be passing along a larger header to the guest.
|
324 | fbe78f4f | aliguori | */
|
325 | fbe78f4f | aliguori | iov[0].iov_base += hdr_len;
|
326 | fbe78f4f | aliguori | iov[0].iov_len -= hdr_len;
|
327 | fbe78f4f | aliguori | |
328 | fbe78f4f | aliguori | return offset;
|
329 | fbe78f4f | aliguori | } |
330 | fbe78f4f | aliguori | |
331 | 3831ab20 | aliguori | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
332 | 3831ab20 | aliguori | { |
333 | 3831ab20 | aliguori | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
334 | f21c0ed9 | aliguori | static const uint8_t vlan[] = {0x81, 0x00}; |
335 | 3831ab20 | aliguori | uint8_t *ptr = (uint8_t *)buf; |
336 | b6503ed9 | aliguori | int i;
|
337 | 3831ab20 | aliguori | |
338 | 3831ab20 | aliguori | if (n->promisc)
|
339 | 3831ab20 | aliguori | return 1; |
340 | 3831ab20 | aliguori | |
341 | f21c0ed9 | aliguori | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
342 | f21c0ed9 | aliguori | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; |
343 | f21c0ed9 | aliguori | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) |
344 | f21c0ed9 | aliguori | return 0; |
345 | f21c0ed9 | aliguori | } |
346 | f21c0ed9 | aliguori | |
347 | 3831ab20 | aliguori | if ((ptr[0] & 1) && n->allmulti) |
348 | 3831ab20 | aliguori | return 1; |
349 | 3831ab20 | aliguori | |
350 | 3831ab20 | aliguori | if (!memcmp(ptr, bcast, sizeof(bcast))) |
351 | 3831ab20 | aliguori | return 1; |
352 | 3831ab20 | aliguori | |
353 | 3831ab20 | aliguori | if (!memcmp(ptr, n->mac, ETH_ALEN))
|
354 | 3831ab20 | aliguori | return 1; |
355 | 3831ab20 | aliguori | |
356 | b6503ed9 | aliguori | for (i = 0; i < n->mac_table.in_use; i++) { |
357 | b6503ed9 | aliguori | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
|
358 | b6503ed9 | aliguori | return 1; |
359 | b6503ed9 | aliguori | } |
360 | b6503ed9 | aliguori | |
361 | 3831ab20 | aliguori | return 0; |
362 | 3831ab20 | aliguori | } |
363 | 3831ab20 | aliguori | |
364 | fbe78f4f | aliguori | static void virtio_net_receive(void *opaque, const uint8_t *buf, int size) |
365 | fbe78f4f | aliguori | { |
366 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
367 | fbe78f4f | aliguori | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; |
368 | 4689f4b3 | aliguori | size_t hdr_len, offset, i; |
369 | fbe78f4f | aliguori | |
370 | fbe78f4f | aliguori | if (!do_virtio_net_can_receive(n, size))
|
371 | fbe78f4f | aliguori | return;
|
372 | fbe78f4f | aliguori | |
373 | 3831ab20 | aliguori | if (!receive_filter(n, buf, size))
|
374 | 3831ab20 | aliguori | return;
|
375 | 3831ab20 | aliguori | |
376 | fbe78f4f | aliguori | /* hdr_len refers to the header we supply to the guest */
|
377 | fbe78f4f | aliguori | hdr_len = n->mergeable_rx_bufs ? |
378 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); |
379 | fbe78f4f | aliguori | |
380 | fbe78f4f | aliguori | offset = i = 0;
|
381 | fbe78f4f | aliguori | |
382 | fbe78f4f | aliguori | while (offset < size) {
|
383 | fbe78f4f | aliguori | VirtQueueElement elem; |
384 | fbe78f4f | aliguori | int len, total;
|
385 | fbe78f4f | aliguori | struct iovec sg[VIRTQUEUE_MAX_SIZE];
|
386 | fbe78f4f | aliguori | |
387 | fbe78f4f | aliguori | len = total = 0;
|
388 | fbe78f4f | aliguori | |
389 | fbe78f4f | aliguori | if ((i != 0 && !n->mergeable_rx_bufs) || |
390 | fbe78f4f | aliguori | virtqueue_pop(n->rx_vq, &elem) == 0) {
|
391 | fbe78f4f | aliguori | if (i == 0) |
392 | fbe78f4f | aliguori | return;
|
393 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net truncating packet\n");
|
394 | fbe78f4f | aliguori | exit(1);
|
395 | fbe78f4f | aliguori | } |
396 | fbe78f4f | aliguori | |
397 | fbe78f4f | aliguori | if (elem.in_num < 1) { |
398 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
|
399 | fbe78f4f | aliguori | exit(1);
|
400 | fbe78f4f | aliguori | } |
401 | fbe78f4f | aliguori | |
402 | fbe78f4f | aliguori | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { |
403 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net header not in first element\n");
|
404 | fbe78f4f | aliguori | exit(1);
|
405 | fbe78f4f | aliguori | } |
406 | fbe78f4f | aliguori | |
407 | fbe78f4f | aliguori | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); |
408 | fbe78f4f | aliguori | |
409 | fbe78f4f | aliguori | if (i == 0) { |
410 | fbe78f4f | aliguori | if (n->mergeable_rx_bufs)
|
411 | fbe78f4f | aliguori | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; |
412 | fbe78f4f | aliguori | |
413 | fbe78f4f | aliguori | offset += receive_header(n, sg, elem.in_num, |
414 | fbe78f4f | aliguori | buf + offset, size - offset, hdr_len); |
415 | fbe78f4f | aliguori | total += hdr_len; |
416 | fbe78f4f | aliguori | } |
417 | fbe78f4f | aliguori | |
418 | fbe78f4f | aliguori | /* copy in packet. ugh */
|
419 | fbe78f4f | aliguori | len = iov_fill(sg, elem.in_num, |
420 | fbe78f4f | aliguori | buf + offset, size - offset); |
421 | fbe78f4f | aliguori | total += len; |
422 | fbe78f4f | aliguori | |
423 | fbe78f4f | aliguori | /* signal other side */
|
424 | fbe78f4f | aliguori | virtqueue_fill(n->rx_vq, &elem, total, i++); |
425 | fbe78f4f | aliguori | |
426 | fbe78f4f | aliguori | offset += len; |
427 | fbe78f4f | aliguori | } |
428 | fbe78f4f | aliguori | |
429 | fbe78f4f | aliguori | if (mhdr)
|
430 | fbe78f4f | aliguori | mhdr->num_buffers = i; |
431 | fbe78f4f | aliguori | |
432 | fbe78f4f | aliguori | virtqueue_flush(n->rx_vq, i); |
433 | fbe78f4f | aliguori | virtio_notify(&n->vdev, n->rx_vq); |
434 | fbe78f4f | aliguori | } |
435 | fbe78f4f | aliguori | |
436 | fbe78f4f | aliguori | /* TX */
|
437 | fbe78f4f | aliguori | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) |
438 | fbe78f4f | aliguori | { |
439 | fbe78f4f | aliguori | VirtQueueElement elem; |
440 | fbe78f4f | aliguori | int has_vnet_hdr = 0; |
441 | fbe78f4f | aliguori | |
442 | fbe78f4f | aliguori | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
|
443 | fbe78f4f | aliguori | return;
|
444 | fbe78f4f | aliguori | |
445 | fbe78f4f | aliguori | while (virtqueue_pop(vq, &elem)) {
|
446 | fbe78f4f | aliguori | ssize_t len = 0;
|
447 | fbe78f4f | aliguori | unsigned int out_num = elem.out_num; |
448 | fbe78f4f | aliguori | struct iovec *out_sg = &elem.out_sg[0]; |
449 | fbe78f4f | aliguori | unsigned hdr_len;
|
450 | fbe78f4f | aliguori | |
451 | fbe78f4f | aliguori | /* hdr_len refers to the header received from the guest */
|
452 | fbe78f4f | aliguori | hdr_len = n->mergeable_rx_bufs ? |
453 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr_mrg_rxbuf) : |
454 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr); |
455 | fbe78f4f | aliguori | |
456 | fbe78f4f | aliguori | if (out_num < 1 || out_sg->iov_len != hdr_len) { |
457 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net header not in first element\n");
|
458 | fbe78f4f | aliguori | exit(1);
|
459 | fbe78f4f | aliguori | } |
460 | fbe78f4f | aliguori | |
461 | fbe78f4f | aliguori | /* ignore the header if GSO is not supported */
|
462 | fbe78f4f | aliguori | if (!has_vnet_hdr) {
|
463 | fbe78f4f | aliguori | out_num--; |
464 | fbe78f4f | aliguori | out_sg++; |
465 | fbe78f4f | aliguori | len += hdr_len; |
466 | fbe78f4f | aliguori | } else if (n->mergeable_rx_bufs) { |
467 | fbe78f4f | aliguori | /* tapfd expects a struct virtio_net_hdr */
|
468 | fbe78f4f | aliguori | hdr_len -= sizeof(struct virtio_net_hdr); |
469 | fbe78f4f | aliguori | out_sg->iov_len -= hdr_len; |
470 | fbe78f4f | aliguori | len += hdr_len; |
471 | fbe78f4f | aliguori | } |
472 | fbe78f4f | aliguori | |
473 | fbe78f4f | aliguori | len += qemu_sendv_packet(n->vc, out_sg, out_num); |
474 | fbe78f4f | aliguori | |
475 | fbe78f4f | aliguori | virtqueue_push(vq, &elem, len); |
476 | fbe78f4f | aliguori | virtio_notify(&n->vdev, vq); |
477 | fbe78f4f | aliguori | } |
478 | fbe78f4f | aliguori | } |
479 | fbe78f4f | aliguori | |
480 | fbe78f4f | aliguori | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) |
481 | fbe78f4f | aliguori | { |
482 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
483 | fbe78f4f | aliguori | |
484 | fbe78f4f | aliguori | if (n->tx_timer_active) {
|
485 | fbe78f4f | aliguori | virtio_queue_set_notification(vq, 1);
|
486 | fbe78f4f | aliguori | qemu_del_timer(n->tx_timer); |
487 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
488 | fbe78f4f | aliguori | virtio_net_flush_tx(n, vq); |
489 | fbe78f4f | aliguori | } else {
|
490 | fbe78f4f | aliguori | qemu_mod_timer(n->tx_timer, |
491 | fbe78f4f | aliguori | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
492 | fbe78f4f | aliguori | n->tx_timer_active = 1;
|
493 | fbe78f4f | aliguori | virtio_queue_set_notification(vq, 0);
|
494 | fbe78f4f | aliguori | } |
495 | fbe78f4f | aliguori | } |
496 | fbe78f4f | aliguori | |
497 | fbe78f4f | aliguori | static void virtio_net_tx_timer(void *opaque) |
498 | fbe78f4f | aliguori | { |
499 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
500 | fbe78f4f | aliguori | |
501 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
502 | fbe78f4f | aliguori | |
503 | fbe78f4f | aliguori | /* Just in case the driver is not ready on more */
|
504 | fbe78f4f | aliguori | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
|
505 | fbe78f4f | aliguori | return;
|
506 | fbe78f4f | aliguori | |
507 | fbe78f4f | aliguori | virtio_queue_set_notification(n->tx_vq, 1);
|
508 | fbe78f4f | aliguori | virtio_net_flush_tx(n, n->tx_vq); |
509 | fbe78f4f | aliguori | } |
510 | fbe78f4f | aliguori | |
511 | fbe78f4f | aliguori | static void virtio_net_save(QEMUFile *f, void *opaque) |
512 | fbe78f4f | aliguori | { |
513 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
514 | fbe78f4f | aliguori | |
515 | fbe78f4f | aliguori | virtio_save(&n->vdev, f); |
516 | fbe78f4f | aliguori | |
517 | 79674068 | aliguori | qemu_put_buffer(f, n->mac, ETH_ALEN); |
518 | fbe78f4f | aliguori | qemu_put_be32(f, n->tx_timer_active); |
519 | e46cb38f | aliguori | qemu_put_be32(f, n->mergeable_rx_bufs); |
520 | 9d6271b8 | aliguori | qemu_put_be16(f, n->status); |
521 | 002437cd | aliguori | qemu_put_be32(f, n->promisc); |
522 | 002437cd | aliguori | qemu_put_be32(f, n->allmulti); |
523 | b6503ed9 | aliguori | qemu_put_be32(f, n->mac_table.in_use); |
524 | b6503ed9 | aliguori | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); |
525 | f21c0ed9 | aliguori | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
|
526 | fbe78f4f | aliguori | } |
527 | fbe78f4f | aliguori | |
528 | fbe78f4f | aliguori | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) |
529 | fbe78f4f | aliguori | { |
530 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
531 | fbe78f4f | aliguori | |
532 | 9d6271b8 | aliguori | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
533 | fbe78f4f | aliguori | return -EINVAL;
|
534 | fbe78f4f | aliguori | |
535 | fbe78f4f | aliguori | virtio_load(&n->vdev, f); |
536 | fbe78f4f | aliguori | |
537 | 79674068 | aliguori | qemu_get_buffer(f, n->mac, ETH_ALEN); |
538 | fbe78f4f | aliguori | n->tx_timer_active = qemu_get_be32(f); |
539 | e46cb38f | aliguori | n->mergeable_rx_bufs = qemu_get_be32(f); |
540 | fbe78f4f | aliguori | |
541 | 9d6271b8 | aliguori | if (version_id >= 3) |
542 | 9d6271b8 | aliguori | n->status = qemu_get_be16(f); |
543 | 9d6271b8 | aliguori | |
544 | 002437cd | aliguori | if (version_id >= 4) { |
545 | 002437cd | aliguori | n->promisc = qemu_get_be32(f); |
546 | 002437cd | aliguori | n->allmulti = qemu_get_be32(f); |
547 | 002437cd | aliguori | } |
548 | 002437cd | aliguori | |
549 | b6503ed9 | aliguori | if (version_id >= 5) { |
550 | b6503ed9 | aliguori | n->mac_table.in_use = qemu_get_be32(f); |
551 | b6503ed9 | aliguori | /* MAC_TABLE_ENTRIES may be different from the saved image */
|
552 | b6503ed9 | aliguori | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
|
553 | b6503ed9 | aliguori | qemu_get_buffer(f, n->mac_table.macs, |
554 | b6503ed9 | aliguori | n->mac_table.in_use * ETH_ALEN); |
555 | b6503ed9 | aliguori | } else if (n->mac_table.in_use) { |
556 | b6503ed9 | aliguori | qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR); |
557 | b6503ed9 | aliguori | n->promisc = 1;
|
558 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
559 | b6503ed9 | aliguori | } |
560 | b6503ed9 | aliguori | } |
561 | b6503ed9 | aliguori | |
562 | f21c0ed9 | aliguori | if (version_id >= 6) |
563 | f21c0ed9 | aliguori | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
|
564 | f21c0ed9 | aliguori | |
565 | fbe78f4f | aliguori | if (n->tx_timer_active) {
|
566 | fbe78f4f | aliguori | qemu_mod_timer(n->tx_timer, |
567 | fbe78f4f | aliguori | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
568 | fbe78f4f | aliguori | } |
569 | fbe78f4f | aliguori | |
570 | fbe78f4f | aliguori | return 0; |
571 | fbe78f4f | aliguori | } |
572 | fbe78f4f | aliguori | |
573 | b946a153 | aliguori | static void virtio_net_cleanup(VLANClientState *vc) |
574 | b946a153 | aliguori | { |
575 | b946a153 | aliguori | VirtIONet *n = vc->opaque; |
576 | b946a153 | aliguori | |
577 | b946a153 | aliguori | unregister_savevm("virtio-net", n);
|
578 | b946a153 | aliguori | |
579 | b946a153 | aliguori | qemu_free(n->mac_table.macs); |
580 | b946a153 | aliguori | qemu_free(n->vlans); |
581 | b946a153 | aliguori | |
582 | b946a153 | aliguori | qemu_del_timer(n->tx_timer); |
583 | b946a153 | aliguori | qemu_free_timer(n->tx_timer); |
584 | b946a153 | aliguori | |
585 | b946a153 | aliguori | virtio_cleanup(&n->vdev); |
586 | b946a153 | aliguori | } |
587 | b946a153 | aliguori | |
588 | 72da4208 | aliguori | PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
|
589 | fbe78f4f | aliguori | { |
590 | fbe78f4f | aliguori | VirtIONet *n; |
591 | fbe78f4f | aliguori | static int virtio_net_id; |
592 | fbe78f4f | aliguori | |
593 | a7c4996b | aliguori | n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
|
594 | a7c4996b | aliguori | PCI_VENDOR_ID_REDHAT_QUMRANET, |
595 | a7c4996b | aliguori | PCI_DEVICE_ID_VIRTIO_NET, |
596 | 99b3718e | aliguori | PCI_VENDOR_ID_REDHAT_QUMRANET, |
597 | 99b3718e | aliguori | VIRTIO_ID_NET, |
598 | 173a543b | blueswir1 | PCI_CLASS_NETWORK_ETHERNET, 0x00,
|
599 | 554c97dd | aliguori | sizeof(struct virtio_net_config), |
600 | 554c97dd | aliguori | sizeof(VirtIONet));
|
601 | fbe78f4f | aliguori | if (!n)
|
602 | 72da4208 | aliguori | return NULL; |
603 | fbe78f4f | aliguori | |
604 | 0f03eca6 | aliguori | n->vdev.get_config = virtio_net_get_config; |
605 | 0f03eca6 | aliguori | n->vdev.set_config = virtio_net_set_config; |
606 | fbe78f4f | aliguori | n->vdev.get_features = virtio_net_get_features; |
607 | fbe78f4f | aliguori | n->vdev.set_features = virtio_net_set_features; |
608 | 8eca6b1b | aliguori | n->vdev.bad_features = virtio_net_bad_features; |
609 | 002437cd | aliguori | n->vdev.reset = virtio_net_reset; |
610 | fbe78f4f | aliguori | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
|
611 | fbe78f4f | aliguori | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
|
612 | 3d11d36c | aliguori | n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
|
613 | 79674068 | aliguori | memcpy(n->mac, nd->macaddr, ETH_ALEN); |
614 | 554c97dd | aliguori | n->status = VIRTIO_NET_S_LINK_UP; |
615 | 7a9f6e4a | aliguori | n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, |
616 | b946a153 | aliguori | virtio_net_receive, |
617 | b946a153 | aliguori | virtio_net_can_receive, |
618 | b946a153 | aliguori | virtio_net_cleanup, n); |
619 | 554c97dd | aliguori | n->vc->link_status_changed = virtio_net_set_link_status; |
620 | fbe78f4f | aliguori | |
621 | 96d5e201 | aliguori | qemu_format_nic_info_str(n->vc, n->mac); |
622 | 96d5e201 | aliguori | |
623 | fbe78f4f | aliguori | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
624 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
625 | fbe78f4f | aliguori | n->mergeable_rx_bufs = 0;
|
626 | 002437cd | aliguori | n->promisc = 1; /* for compatibility */ |
627 | fbe78f4f | aliguori | |
628 | b6503ed9 | aliguori | n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); |
629 | b6503ed9 | aliguori | |
630 | f21c0ed9 | aliguori | n->vlans = qemu_mallocz(MAX_VLAN >> 3);
|
631 | f21c0ed9 | aliguori | |
632 | 9d6271b8 | aliguori | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
|
633 | fbe78f4f | aliguori | virtio_net_save, virtio_net_load, n); |
634 | 72da4208 | aliguori | return (PCIDevice *)n;
|
635 | fbe78f4f | aliguori | } |