root / hw / virtio-net.c @ 7fc2f2c0
History | View | Annotate | Download (21.3 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 | 015cb166 | Alex Williamson | #define VIRTIO_NET_VM_VERSION 10 |
20 | b6503ed9 | aliguori | |
21 | 4ffb17f5 | Alex Williamson | #define MAC_TABLE_ENTRIES 64 |
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 | 6243375f | Mark McLoughlin | struct {
|
36 | 6243375f | Mark McLoughlin | VirtQueueElement elem; |
37 | 6243375f | Mark McLoughlin | ssize_t len; |
38 | 6243375f | Mark McLoughlin | } async_tx; |
39 | fbe78f4f | aliguori | int mergeable_rx_bufs;
|
40 | f10c592e | Alex Williamson | uint8_t promisc; |
41 | f10c592e | Alex Williamson | uint8_t allmulti; |
42 | 015cb166 | Alex Williamson | uint8_t alluni; |
43 | 015cb166 | Alex Williamson | uint8_t nomulti; |
44 | 015cb166 | Alex Williamson | uint8_t nouni; |
45 | 015cb166 | Alex Williamson | uint8_t nobcast; |
46 | b6503ed9 | aliguori | struct {
|
47 | b6503ed9 | aliguori | int in_use;
|
48 | 2d9aba39 | Alex Williamson | int first_multi;
|
49 | 8fd2a2f1 | Alex Williamson | uint8_t multi_overflow; |
50 | 8fd2a2f1 | Alex Williamson | uint8_t uni_overflow; |
51 | b6503ed9 | aliguori | uint8_t *macs; |
52 | b6503ed9 | aliguori | } mac_table; |
53 | f21c0ed9 | aliguori | uint32_t *vlans; |
54 | fbe78f4f | aliguori | } VirtIONet; |
55 | fbe78f4f | aliguori | |
56 | fbe78f4f | aliguori | /* TODO
|
57 | fbe78f4f | aliguori | * - we could suppress RX interrupt if we were so inclined.
|
58 | fbe78f4f | aliguori | */
|
59 | fbe78f4f | aliguori | |
60 | fbe78f4f | aliguori | static VirtIONet *to_virtio_net(VirtIODevice *vdev)
|
61 | fbe78f4f | aliguori | { |
62 | fbe78f4f | aliguori | return (VirtIONet *)vdev;
|
63 | fbe78f4f | aliguori | } |
64 | fbe78f4f | aliguori | |
65 | 0f03eca6 | aliguori | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
66 | fbe78f4f | aliguori | { |
67 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
68 | fbe78f4f | aliguori | struct virtio_net_config netcfg;
|
69 | fbe78f4f | aliguori | |
70 | 554c97dd | aliguori | netcfg.status = n->status; |
71 | 79674068 | aliguori | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
72 | fbe78f4f | aliguori | memcpy(config, &netcfg, sizeof(netcfg));
|
73 | fbe78f4f | aliguori | } |
74 | fbe78f4f | aliguori | |
75 | 0f03eca6 | aliguori | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
76 | 0f03eca6 | aliguori | { |
77 | 0f03eca6 | aliguori | VirtIONet *n = to_virtio_net(vdev); |
78 | 0f03eca6 | aliguori | struct virtio_net_config netcfg;
|
79 | 0f03eca6 | aliguori | |
80 | 0f03eca6 | aliguori | memcpy(&netcfg, config, sizeof(netcfg));
|
81 | 0f03eca6 | aliguori | |
82 | 79674068 | aliguori | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
|
83 | 79674068 | aliguori | memcpy(n->mac, netcfg.mac, ETH_ALEN); |
84 | 0f03eca6 | aliguori | qemu_format_nic_info_str(n->vc, n->mac); |
85 | 0f03eca6 | aliguori | } |
86 | 0f03eca6 | aliguori | } |
87 | 0f03eca6 | aliguori | |
88 | 554c97dd | aliguori | static void virtio_net_set_link_status(VLANClientState *vc) |
89 | 554c97dd | aliguori | { |
90 | 554c97dd | aliguori | VirtIONet *n = vc->opaque; |
91 | 554c97dd | aliguori | uint16_t old_status = n->status; |
92 | 554c97dd | aliguori | |
93 | 554c97dd | aliguori | if (vc->link_down)
|
94 | 554c97dd | aliguori | n->status &= ~VIRTIO_NET_S_LINK_UP; |
95 | 554c97dd | aliguori | else
|
96 | 554c97dd | aliguori | n->status |= VIRTIO_NET_S_LINK_UP; |
97 | 554c97dd | aliguori | |
98 | 554c97dd | aliguori | if (n->status != old_status)
|
99 | 554c97dd | aliguori | virtio_notify_config(&n->vdev); |
100 | 554c97dd | aliguori | } |
101 | 554c97dd | aliguori | |
102 | 002437cd | aliguori | static void virtio_net_reset(VirtIODevice *vdev) |
103 | 002437cd | aliguori | { |
104 | 002437cd | aliguori | VirtIONet *n = to_virtio_net(vdev); |
105 | 002437cd | aliguori | |
106 | 002437cd | aliguori | /* Reset back to compatibility mode */
|
107 | 002437cd | aliguori | n->promisc = 1;
|
108 | 002437cd | aliguori | n->allmulti = 0;
|
109 | 015cb166 | Alex Williamson | n->alluni = 0;
|
110 | 015cb166 | Alex Williamson | n->nomulti = 0;
|
111 | 015cb166 | Alex Williamson | n->nouni = 0;
|
112 | 015cb166 | Alex Williamson | n->nobcast = 0;
|
113 | b6503ed9 | aliguori | |
114 | f21c0ed9 | aliguori | /* Flush any MAC and VLAN filter table state */
|
115 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
116 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = 0;
|
117 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = 0;
|
118 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = 0;
|
119 | b6503ed9 | aliguori | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
|
120 | f21c0ed9 | aliguori | memset(n->vlans, 0, MAX_VLAN >> 3); |
121 | 002437cd | aliguori | } |
122 | 002437cd | aliguori | |
123 | fbe78f4f | aliguori | static uint32_t virtio_net_get_features(VirtIODevice *vdev)
|
124 | fbe78f4f | aliguori | { |
125 | 3d11d36c | aliguori | uint32_t features = (1 << VIRTIO_NET_F_MAC) |
|
126 | e16044ef | Mark McLoughlin | (1 << VIRTIO_NET_F_MRG_RXBUF) |
|
127 | 3d11d36c | aliguori | (1 << VIRTIO_NET_F_STATUS) |
|
128 | b6503ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_VQ) |
|
129 | f21c0ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_RX) |
|
130 | 015cb166 | Alex Williamson | (1 << VIRTIO_NET_F_CTRL_VLAN) |
|
131 | 015cb166 | Alex Williamson | (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
|
132 | fbe78f4f | aliguori | |
133 | fbe78f4f | aliguori | return features;
|
134 | fbe78f4f | aliguori | } |
135 | fbe78f4f | aliguori | |
136 | 8eca6b1b | aliguori | static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
|
137 | 8eca6b1b | aliguori | { |
138 | 8eca6b1b | aliguori | uint32_t features = 0;
|
139 | 8eca6b1b | aliguori | |
140 | 8eca6b1b | aliguori | /* Linux kernel 2.6.25. It understood MAC (as everyone must),
|
141 | 8eca6b1b | aliguori | * but also these: */
|
142 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_MAC);
|
143 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
|
144 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
|
145 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
|
146 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_GUEST_ECN);
|
147 | 8eca6b1b | aliguori | |
148 | 8eca6b1b | aliguori | return features & virtio_net_get_features(vdev);
|
149 | 8eca6b1b | aliguori | } |
150 | 8eca6b1b | aliguori | |
151 | fbe78f4f | aliguori | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
152 | fbe78f4f | aliguori | { |
153 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
154 | fbe78f4f | aliguori | |
155 | fbe78f4f | aliguori | n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
|
156 | fbe78f4f | aliguori | } |
157 | fbe78f4f | aliguori | |
158 | 002437cd | aliguori | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
159 | 002437cd | aliguori | VirtQueueElement *elem) |
160 | 002437cd | aliguori | { |
161 | 002437cd | aliguori | uint8_t on; |
162 | 002437cd | aliguori | |
163 | 002437cd | aliguori | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) { |
164 | 002437cd | aliguori | fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
|
165 | 002437cd | aliguori | exit(1);
|
166 | 002437cd | aliguori | } |
167 | 002437cd | aliguori | |
168 | 002437cd | aliguori | on = ldub_p(elem->out_sg[1].iov_base);
|
169 | 002437cd | aliguori | |
170 | 002437cd | aliguori | if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
|
171 | 002437cd | aliguori | n->promisc = on; |
172 | 002437cd | aliguori | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) |
173 | 002437cd | aliguori | n->allmulti = on; |
174 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI) |
175 | 015cb166 | Alex Williamson | n->alluni = on; |
176 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI) |
177 | 015cb166 | Alex Williamson | n->nomulti = on; |
178 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI) |
179 | 015cb166 | Alex Williamson | n->nouni = on; |
180 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST) |
181 | 015cb166 | Alex Williamson | n->nobcast = on; |
182 | 002437cd | aliguori | else
|
183 | 002437cd | aliguori | return VIRTIO_NET_ERR;
|
184 | 002437cd | aliguori | |
185 | 002437cd | aliguori | return VIRTIO_NET_OK;
|
186 | 002437cd | aliguori | } |
187 | 002437cd | aliguori | |
188 | b6503ed9 | aliguori | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
189 | b6503ed9 | aliguori | VirtQueueElement *elem) |
190 | b6503ed9 | aliguori | { |
191 | b6503ed9 | aliguori | struct virtio_net_ctrl_mac mac_data;
|
192 | b6503ed9 | aliguori | |
193 | b6503ed9 | aliguori | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 || |
194 | b6503ed9 | aliguori | elem->out_sg[1].iov_len < sizeof(mac_data) || |
195 | b6503ed9 | aliguori | elem->out_sg[2].iov_len < sizeof(mac_data)) |
196 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
197 | b6503ed9 | aliguori | |
198 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
199 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = 0;
|
200 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = 0;
|
201 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = 0;
|
202 | b6503ed9 | aliguori | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
|
203 | b6503ed9 | aliguori | |
204 | b6503ed9 | aliguori | mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
|
205 | b6503ed9 | aliguori | |
206 | b6503ed9 | aliguori | if (sizeof(mac_data.entries) + |
207 | b6503ed9 | aliguori | (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
|
208 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
209 | b6503ed9 | aliguori | |
210 | b6503ed9 | aliguori | if (mac_data.entries <= MAC_TABLE_ENTRIES) {
|
211 | b6503ed9 | aliguori | memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data), |
212 | b6503ed9 | aliguori | mac_data.entries * ETH_ALEN); |
213 | b6503ed9 | aliguori | n->mac_table.in_use += mac_data.entries; |
214 | b6503ed9 | aliguori | } else {
|
215 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = 1;
|
216 | b6503ed9 | aliguori | } |
217 | b6503ed9 | aliguori | |
218 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = n->mac_table.in_use; |
219 | 2d9aba39 | Alex Williamson | |
220 | b6503ed9 | aliguori | mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
|
221 | b6503ed9 | aliguori | |
222 | b6503ed9 | aliguori | if (sizeof(mac_data.entries) + |
223 | b6503ed9 | aliguori | (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
|
224 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
225 | b6503ed9 | aliguori | |
226 | b6503ed9 | aliguori | if (mac_data.entries) {
|
227 | b6503ed9 | aliguori | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
|
228 | b6503ed9 | aliguori | memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN), |
229 | b6503ed9 | aliguori | elem->out_sg[2].iov_base + sizeof(mac_data), |
230 | b6503ed9 | aliguori | mac_data.entries * ETH_ALEN); |
231 | b6503ed9 | aliguori | n->mac_table.in_use += mac_data.entries; |
232 | 8fd2a2f1 | Alex Williamson | } else {
|
233 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = 1;
|
234 | 8fd2a2f1 | Alex Williamson | } |
235 | b6503ed9 | aliguori | } |
236 | b6503ed9 | aliguori | |
237 | b6503ed9 | aliguori | return VIRTIO_NET_OK;
|
238 | b6503ed9 | aliguori | } |
239 | b6503ed9 | aliguori | |
240 | f21c0ed9 | aliguori | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
241 | f21c0ed9 | aliguori | VirtQueueElement *elem) |
242 | f21c0ed9 | aliguori | { |
243 | f21c0ed9 | aliguori | uint16_t vid; |
244 | f21c0ed9 | aliguori | |
245 | f21c0ed9 | aliguori | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) { |
246 | f21c0ed9 | aliguori | fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
|
247 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
248 | f21c0ed9 | aliguori | } |
249 | f21c0ed9 | aliguori | |
250 | f21c0ed9 | aliguori | vid = lduw_le_p(elem->out_sg[1].iov_base);
|
251 | f21c0ed9 | aliguori | |
252 | f21c0ed9 | aliguori | if (vid >= MAX_VLAN)
|
253 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
254 | f21c0ed9 | aliguori | |
255 | f21c0ed9 | aliguori | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
|
256 | f21c0ed9 | aliguori | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); |
257 | f21c0ed9 | aliguori | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) |
258 | f21c0ed9 | aliguori | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); |
259 | f21c0ed9 | aliguori | else
|
260 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
261 | f21c0ed9 | aliguori | |
262 | f21c0ed9 | aliguori | return VIRTIO_NET_OK;
|
263 | f21c0ed9 | aliguori | } |
264 | f21c0ed9 | aliguori | |
265 | 3d11d36c | aliguori | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
266 | 3d11d36c | aliguori | { |
267 | 002437cd | aliguori | VirtIONet *n = to_virtio_net(vdev); |
268 | 3d11d36c | aliguori | struct virtio_net_ctrl_hdr ctrl;
|
269 | 3d11d36c | aliguori | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
270 | 3d11d36c | aliguori | VirtQueueElement elem; |
271 | 3d11d36c | aliguori | |
272 | 3d11d36c | aliguori | while (virtqueue_pop(vq, &elem)) {
|
273 | 3d11d36c | aliguori | if ((elem.in_num < 1) || (elem.out_num < 1)) { |
274 | 3d11d36c | aliguori | fprintf(stderr, "virtio-net ctrl missing headers\n");
|
275 | 3d11d36c | aliguori | exit(1);
|
276 | 3d11d36c | aliguori | } |
277 | 3d11d36c | aliguori | |
278 | 3d11d36c | aliguori | if (elem.out_sg[0].iov_len < sizeof(ctrl) || |
279 | c6bb9a32 | aliguori | elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) { |
280 | 3d11d36c | aliguori | fprintf(stderr, "virtio-net ctrl header not in correct element\n");
|
281 | 3d11d36c | aliguori | exit(1);
|
282 | 3d11d36c | aliguori | } |
283 | 3d11d36c | aliguori | |
284 | 3d11d36c | aliguori | ctrl.class = ldub_p(elem.out_sg[0].iov_base);
|
285 | 3d11d36c | aliguori | ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class)); |
286 | 3d11d36c | aliguori | |
287 | 002437cd | aliguori | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
|
288 | 002437cd | aliguori | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); |
289 | b6503ed9 | aliguori | else if (ctrl.class == VIRTIO_NET_CTRL_MAC) |
290 | b6503ed9 | aliguori | status = virtio_net_handle_mac(n, ctrl.cmd, &elem); |
291 | f21c0ed9 | aliguori | else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) |
292 | f21c0ed9 | aliguori | status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); |
293 | 002437cd | aliguori | |
294 | 3d11d36c | aliguori | stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
|
295 | 3d11d36c | aliguori | |
296 | 3d11d36c | aliguori | virtqueue_push(vq, &elem, sizeof(status));
|
297 | 3d11d36c | aliguori | virtio_notify(vdev, vq); |
298 | 3d11d36c | aliguori | } |
299 | 3d11d36c | aliguori | } |
300 | 3d11d36c | aliguori | |
301 | fbe78f4f | aliguori | /* RX */
|
302 | fbe78f4f | aliguori | |
303 | fbe78f4f | aliguori | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) |
304 | fbe78f4f | aliguori | { |
305 | 8aeff62d | Mark McLoughlin | VirtIONet *n = to_virtio_net(vdev); |
306 | 8aeff62d | Mark McLoughlin | |
307 | 8aeff62d | Mark McLoughlin | qemu_flush_queued_packets(n->vc); |
308 | a61d1f67 | Glauber Costa | |
309 | a61d1f67 | Glauber Costa | /* We now have RX buffers, signal to the IO thread to break out of the
|
310 | a61d1f67 | Glauber Costa | * select to re-poll the tap file descriptor */
|
311 | a61d1f67 | Glauber Costa | qemu_notify_event(); |
312 | fbe78f4f | aliguori | } |
313 | fbe78f4f | aliguori | |
314 | fbe78f4f | aliguori | static int do_virtio_net_can_receive(VirtIONet *n, int bufsize) |
315 | fbe78f4f | aliguori | { |
316 | fbe78f4f | aliguori | if (!virtio_queue_ready(n->rx_vq) ||
|
317 | fbe78f4f | aliguori | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
318 | fbe78f4f | aliguori | return 0; |
319 | fbe78f4f | aliguori | |
320 | fbe78f4f | aliguori | if (virtio_queue_empty(n->rx_vq) ||
|
321 | fbe78f4f | aliguori | (n->mergeable_rx_bufs && |
322 | fbe78f4f | aliguori | !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
|
323 | fbe78f4f | aliguori | virtio_queue_set_notification(n->rx_vq, 1);
|
324 | fbe78f4f | aliguori | return 0; |
325 | fbe78f4f | aliguori | } |
326 | fbe78f4f | aliguori | |
327 | fbe78f4f | aliguori | virtio_queue_set_notification(n->rx_vq, 0);
|
328 | fbe78f4f | aliguori | return 1; |
329 | fbe78f4f | aliguori | } |
330 | fbe78f4f | aliguori | |
331 | e3f5ec2b | Mark McLoughlin | static int virtio_net_can_receive(VLANClientState *vc) |
332 | fbe78f4f | aliguori | { |
333 | e3f5ec2b | Mark McLoughlin | VirtIONet *n = vc->opaque; |
334 | fbe78f4f | aliguori | |
335 | fbe78f4f | aliguori | return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
|
336 | fbe78f4f | aliguori | } |
337 | fbe78f4f | aliguori | |
338 | fbe78f4f | aliguori | static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count) |
339 | fbe78f4f | aliguori | { |
340 | fbe78f4f | aliguori | int offset, i;
|
341 | fbe78f4f | aliguori | |
342 | fbe78f4f | aliguori | offset = i = 0;
|
343 | fbe78f4f | aliguori | while (offset < count && i < iovcnt) {
|
344 | fbe78f4f | aliguori | int len = MIN(iov[i].iov_len, count - offset);
|
345 | fbe78f4f | aliguori | memcpy(iov[i].iov_base, buf + offset, len); |
346 | fbe78f4f | aliguori | offset += len; |
347 | fbe78f4f | aliguori | i++; |
348 | fbe78f4f | aliguori | } |
349 | fbe78f4f | aliguori | |
350 | fbe78f4f | aliguori | return offset;
|
351 | fbe78f4f | aliguori | } |
352 | fbe78f4f | aliguori | |
353 | fbe78f4f | aliguori | static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, |
354 | 4689f4b3 | aliguori | const void *buf, size_t size, size_t hdr_len) |
355 | fbe78f4f | aliguori | { |
356 | 3f4cb3d3 | blueswir1 | struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base; |
357 | fbe78f4f | aliguori | int offset = 0; |
358 | fbe78f4f | aliguori | |
359 | fbe78f4f | aliguori | hdr->flags = 0;
|
360 | fbe78f4f | aliguori | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
361 | fbe78f4f | aliguori | |
362 | fbe78f4f | aliguori | /* We only ever receive a struct virtio_net_hdr from the tapfd,
|
363 | fbe78f4f | aliguori | * but we may be passing along a larger header to the guest.
|
364 | fbe78f4f | aliguori | */
|
365 | fbe78f4f | aliguori | iov[0].iov_base += hdr_len;
|
366 | fbe78f4f | aliguori | iov[0].iov_len -= hdr_len;
|
367 | fbe78f4f | aliguori | |
368 | fbe78f4f | aliguori | return offset;
|
369 | fbe78f4f | aliguori | } |
370 | fbe78f4f | aliguori | |
371 | 3831ab20 | aliguori | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
372 | 3831ab20 | aliguori | { |
373 | 3831ab20 | aliguori | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
374 | f21c0ed9 | aliguori | static const uint8_t vlan[] = {0x81, 0x00}; |
375 | 3831ab20 | aliguori | uint8_t *ptr = (uint8_t *)buf; |
376 | b6503ed9 | aliguori | int i;
|
377 | 3831ab20 | aliguori | |
378 | 3831ab20 | aliguori | if (n->promisc)
|
379 | 3831ab20 | aliguori | return 1; |
380 | 3831ab20 | aliguori | |
381 | f21c0ed9 | aliguori | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
382 | f21c0ed9 | aliguori | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; |
383 | f21c0ed9 | aliguori | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) |
384 | f21c0ed9 | aliguori | return 0; |
385 | f21c0ed9 | aliguori | } |
386 | f21c0ed9 | aliguori | |
387 | bbe2f399 | Alex Williamson | if (ptr[0] & 1) { // multicast |
388 | bbe2f399 | Alex Williamson | if (!memcmp(ptr, bcast, sizeof(bcast))) { |
389 | 015cb166 | Alex Williamson | return !n->nobcast;
|
390 | 015cb166 | Alex Williamson | } else if (n->nomulti) { |
391 | 015cb166 | Alex Williamson | return 0; |
392 | 8fd2a2f1 | Alex Williamson | } else if (n->allmulti || n->mac_table.multi_overflow) { |
393 | bbe2f399 | Alex Williamson | return 1; |
394 | bbe2f399 | Alex Williamson | } |
395 | 2d9aba39 | Alex Williamson | |
396 | 2d9aba39 | Alex Williamson | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
|
397 | 2d9aba39 | Alex Williamson | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
|
398 | 2d9aba39 | Alex Williamson | return 1; |
399 | 2d9aba39 | Alex Williamson | } |
400 | 2d9aba39 | Alex Williamson | } |
401 | bbe2f399 | Alex Williamson | } else { // unicast |
402 | 015cb166 | Alex Williamson | if (n->nouni) {
|
403 | 015cb166 | Alex Williamson | return 0; |
404 | 015cb166 | Alex Williamson | } else if (n->alluni || n->mac_table.uni_overflow) { |
405 | 8fd2a2f1 | Alex Williamson | return 1; |
406 | 8fd2a2f1 | Alex Williamson | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { |
407 | bbe2f399 | Alex Williamson | return 1; |
408 | bbe2f399 | Alex Williamson | } |
409 | 3831ab20 | aliguori | |
410 | 2d9aba39 | Alex Williamson | for (i = 0; i < n->mac_table.first_multi; i++) { |
411 | 2d9aba39 | Alex Williamson | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
|
412 | 2d9aba39 | Alex Williamson | return 1; |
413 | 2d9aba39 | Alex Williamson | } |
414 | 2d9aba39 | Alex Williamson | } |
415 | b6503ed9 | aliguori | } |
416 | b6503ed9 | aliguori | |
417 | 3831ab20 | aliguori | return 0; |
418 | 3831ab20 | aliguori | } |
419 | 3831ab20 | aliguori | |
420 | 4f1c942b | Mark McLoughlin | static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
421 | fbe78f4f | aliguori | { |
422 | e3f5ec2b | Mark McLoughlin | VirtIONet *n = vc->opaque; |
423 | fbe78f4f | aliguori | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; |
424 | 4689f4b3 | aliguori | size_t hdr_len, offset, i; |
425 | fbe78f4f | aliguori | |
426 | fbe78f4f | aliguori | if (!do_virtio_net_can_receive(n, size))
|
427 | 8aeff62d | Mark McLoughlin | return 0; |
428 | fbe78f4f | aliguori | |
429 | 3831ab20 | aliguori | if (!receive_filter(n, buf, size))
|
430 | 4f1c942b | Mark McLoughlin | return size;
|
431 | 3831ab20 | aliguori | |
432 | fbe78f4f | aliguori | /* hdr_len refers to the header we supply to the guest */
|
433 | fbe78f4f | aliguori | hdr_len = n->mergeable_rx_bufs ? |
434 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); |
435 | fbe78f4f | aliguori | |
436 | fbe78f4f | aliguori | offset = i = 0;
|
437 | fbe78f4f | aliguori | |
438 | fbe78f4f | aliguori | while (offset < size) {
|
439 | fbe78f4f | aliguori | VirtQueueElement elem; |
440 | fbe78f4f | aliguori | int len, total;
|
441 | fbe78f4f | aliguori | struct iovec sg[VIRTQUEUE_MAX_SIZE];
|
442 | fbe78f4f | aliguori | |
443 | fbe78f4f | aliguori | len = total = 0;
|
444 | fbe78f4f | aliguori | |
445 | fbe78f4f | aliguori | if ((i != 0 && !n->mergeable_rx_bufs) || |
446 | fbe78f4f | aliguori | virtqueue_pop(n->rx_vq, &elem) == 0) {
|
447 | fbe78f4f | aliguori | if (i == 0) |
448 | 4f1c942b | Mark McLoughlin | return -1; |
449 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net truncating packet\n");
|
450 | fbe78f4f | aliguori | exit(1);
|
451 | fbe78f4f | aliguori | } |
452 | fbe78f4f | aliguori | |
453 | fbe78f4f | aliguori | if (elem.in_num < 1) { |
454 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
|
455 | fbe78f4f | aliguori | exit(1);
|
456 | fbe78f4f | aliguori | } |
457 | fbe78f4f | aliguori | |
458 | fbe78f4f | aliguori | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { |
459 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net header not in first element\n");
|
460 | fbe78f4f | aliguori | exit(1);
|
461 | fbe78f4f | aliguori | } |
462 | fbe78f4f | aliguori | |
463 | fbe78f4f | aliguori | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); |
464 | fbe78f4f | aliguori | |
465 | fbe78f4f | aliguori | if (i == 0) { |
466 | fbe78f4f | aliguori | if (n->mergeable_rx_bufs)
|
467 | fbe78f4f | aliguori | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; |
468 | fbe78f4f | aliguori | |
469 | fbe78f4f | aliguori | offset += receive_header(n, sg, elem.in_num, |
470 | fbe78f4f | aliguori | buf + offset, size - offset, hdr_len); |
471 | fbe78f4f | aliguori | total += hdr_len; |
472 | fbe78f4f | aliguori | } |
473 | fbe78f4f | aliguori | |
474 | fbe78f4f | aliguori | /* copy in packet. ugh */
|
475 | fbe78f4f | aliguori | len = iov_fill(sg, elem.in_num, |
476 | fbe78f4f | aliguori | buf + offset, size - offset); |
477 | fbe78f4f | aliguori | total += len; |
478 | fbe78f4f | aliguori | |
479 | fbe78f4f | aliguori | /* signal other side */
|
480 | fbe78f4f | aliguori | virtqueue_fill(n->rx_vq, &elem, total, i++); |
481 | fbe78f4f | aliguori | |
482 | fbe78f4f | aliguori | offset += len; |
483 | fbe78f4f | aliguori | } |
484 | fbe78f4f | aliguori | |
485 | fbe78f4f | aliguori | if (mhdr)
|
486 | fbe78f4f | aliguori | mhdr->num_buffers = i; |
487 | fbe78f4f | aliguori | |
488 | fbe78f4f | aliguori | virtqueue_flush(n->rx_vq, i); |
489 | fbe78f4f | aliguori | virtio_notify(&n->vdev, n->rx_vq); |
490 | 4f1c942b | Mark McLoughlin | |
491 | 4f1c942b | Mark McLoughlin | return size;
|
492 | fbe78f4f | aliguori | } |
493 | fbe78f4f | aliguori | |
494 | 6243375f | Mark McLoughlin | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq); |
495 | 6243375f | Mark McLoughlin | |
496 | 6243375f | Mark McLoughlin | static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len) |
497 | 6243375f | Mark McLoughlin | { |
498 | 6243375f | Mark McLoughlin | VirtIONet *n = vc->opaque; |
499 | 6243375f | Mark McLoughlin | |
500 | 6243375f | Mark McLoughlin | virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len); |
501 | 6243375f | Mark McLoughlin | virtio_notify(&n->vdev, n->tx_vq); |
502 | 6243375f | Mark McLoughlin | |
503 | 6243375f | Mark McLoughlin | n->async_tx.elem.out_num = n->async_tx.len = 0;
|
504 | 6243375f | Mark McLoughlin | |
505 | 6243375f | Mark McLoughlin | virtio_queue_set_notification(n->tx_vq, 1);
|
506 | 6243375f | Mark McLoughlin | virtio_net_flush_tx(n, n->tx_vq); |
507 | 6243375f | Mark McLoughlin | } |
508 | 6243375f | Mark McLoughlin | |
509 | fbe78f4f | aliguori | /* TX */
|
510 | fbe78f4f | aliguori | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) |
511 | fbe78f4f | aliguori | { |
512 | fbe78f4f | aliguori | VirtQueueElement elem; |
513 | fbe78f4f | aliguori | int has_vnet_hdr = 0; |
514 | fbe78f4f | aliguori | |
515 | fbe78f4f | aliguori | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
|
516 | fbe78f4f | aliguori | return;
|
517 | fbe78f4f | aliguori | |
518 | 6243375f | Mark McLoughlin | if (n->async_tx.elem.out_num) {
|
519 | 6243375f | Mark McLoughlin | virtio_queue_set_notification(n->tx_vq, 0);
|
520 | 6243375f | Mark McLoughlin | return;
|
521 | 6243375f | Mark McLoughlin | } |
522 | 6243375f | Mark McLoughlin | |
523 | fbe78f4f | aliguori | while (virtqueue_pop(vq, &elem)) {
|
524 | 6243375f | Mark McLoughlin | ssize_t ret, len = 0;
|
525 | fbe78f4f | aliguori | unsigned int out_num = elem.out_num; |
526 | fbe78f4f | aliguori | struct iovec *out_sg = &elem.out_sg[0]; |
527 | fbe78f4f | aliguori | unsigned hdr_len;
|
528 | fbe78f4f | aliguori | |
529 | fbe78f4f | aliguori | /* hdr_len refers to the header received from the guest */
|
530 | fbe78f4f | aliguori | hdr_len = n->mergeable_rx_bufs ? |
531 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr_mrg_rxbuf) : |
532 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr); |
533 | fbe78f4f | aliguori | |
534 | fbe78f4f | aliguori | if (out_num < 1 || out_sg->iov_len != hdr_len) { |
535 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net header not in first element\n");
|
536 | fbe78f4f | aliguori | exit(1);
|
537 | fbe78f4f | aliguori | } |
538 | fbe78f4f | aliguori | |
539 | fbe78f4f | aliguori | /* ignore the header if GSO is not supported */
|
540 | fbe78f4f | aliguori | if (!has_vnet_hdr) {
|
541 | fbe78f4f | aliguori | out_num--; |
542 | fbe78f4f | aliguori | out_sg++; |
543 | fbe78f4f | aliguori | len += hdr_len; |
544 | fbe78f4f | aliguori | } else if (n->mergeable_rx_bufs) { |
545 | fbe78f4f | aliguori | /* tapfd expects a struct virtio_net_hdr */
|
546 | fbe78f4f | aliguori | hdr_len -= sizeof(struct virtio_net_hdr); |
547 | fbe78f4f | aliguori | out_sg->iov_len -= hdr_len; |
548 | fbe78f4f | aliguori | len += hdr_len; |
549 | fbe78f4f | aliguori | } |
550 | fbe78f4f | aliguori | |
551 | 6243375f | Mark McLoughlin | ret = qemu_sendv_packet_async(n->vc, out_sg, out_num, |
552 | 6243375f | Mark McLoughlin | virtio_net_tx_complete); |
553 | 6243375f | Mark McLoughlin | if (ret == 0) { |
554 | 6243375f | Mark McLoughlin | virtio_queue_set_notification(n->tx_vq, 0);
|
555 | 6243375f | Mark McLoughlin | n->async_tx.elem = elem; |
556 | 6243375f | Mark McLoughlin | n->async_tx.len = len; |
557 | 6243375f | Mark McLoughlin | return;
|
558 | 6243375f | Mark McLoughlin | } |
559 | 6243375f | Mark McLoughlin | |
560 | 6243375f | Mark McLoughlin | len += ret; |
561 | fbe78f4f | aliguori | |
562 | fbe78f4f | aliguori | virtqueue_push(vq, &elem, len); |
563 | fbe78f4f | aliguori | virtio_notify(&n->vdev, vq); |
564 | fbe78f4f | aliguori | } |
565 | fbe78f4f | aliguori | } |
566 | fbe78f4f | aliguori | |
567 | fbe78f4f | aliguori | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) |
568 | fbe78f4f | aliguori | { |
569 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
570 | fbe78f4f | aliguori | |
571 | fbe78f4f | aliguori | if (n->tx_timer_active) {
|
572 | fbe78f4f | aliguori | virtio_queue_set_notification(vq, 1);
|
573 | fbe78f4f | aliguori | qemu_del_timer(n->tx_timer); |
574 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
575 | fbe78f4f | aliguori | virtio_net_flush_tx(n, vq); |
576 | fbe78f4f | aliguori | } else {
|
577 | fbe78f4f | aliguori | qemu_mod_timer(n->tx_timer, |
578 | fbe78f4f | aliguori | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
579 | fbe78f4f | aliguori | n->tx_timer_active = 1;
|
580 | fbe78f4f | aliguori | virtio_queue_set_notification(vq, 0);
|
581 | fbe78f4f | aliguori | } |
582 | fbe78f4f | aliguori | } |
583 | fbe78f4f | aliguori | |
584 | fbe78f4f | aliguori | static void virtio_net_tx_timer(void *opaque) |
585 | fbe78f4f | aliguori | { |
586 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
587 | fbe78f4f | aliguori | |
588 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
589 | fbe78f4f | aliguori | |
590 | fbe78f4f | aliguori | /* Just in case the driver is not ready on more */
|
591 | fbe78f4f | aliguori | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
|
592 | fbe78f4f | aliguori | return;
|
593 | fbe78f4f | aliguori | |
594 | fbe78f4f | aliguori | virtio_queue_set_notification(n->tx_vq, 1);
|
595 | fbe78f4f | aliguori | virtio_net_flush_tx(n, n->tx_vq); |
596 | fbe78f4f | aliguori | } |
597 | fbe78f4f | aliguori | |
598 | fbe78f4f | aliguori | static void virtio_net_save(QEMUFile *f, void *opaque) |
599 | fbe78f4f | aliguori | { |
600 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
601 | fbe78f4f | aliguori | |
602 | fbe78f4f | aliguori | virtio_save(&n->vdev, f); |
603 | fbe78f4f | aliguori | |
604 | 79674068 | aliguori | qemu_put_buffer(f, n->mac, ETH_ALEN); |
605 | fbe78f4f | aliguori | qemu_put_be32(f, n->tx_timer_active); |
606 | e46cb38f | aliguori | qemu_put_be32(f, n->mergeable_rx_bufs); |
607 | 9d6271b8 | aliguori | qemu_put_be16(f, n->status); |
608 | f10c592e | Alex Williamson | qemu_put_byte(f, n->promisc); |
609 | f10c592e | Alex Williamson | qemu_put_byte(f, n->allmulti); |
610 | b6503ed9 | aliguori | qemu_put_be32(f, n->mac_table.in_use); |
611 | b6503ed9 | aliguori | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); |
612 | f21c0ed9 | aliguori | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
|
613 | 6c042c16 | Alex Williamson | qemu_put_be32(f, 0); /* vnet-hdr placeholder */ |
614 | 8fd2a2f1 | Alex Williamson | qemu_put_byte(f, n->mac_table.multi_overflow); |
615 | 8fd2a2f1 | Alex Williamson | qemu_put_byte(f, n->mac_table.uni_overflow); |
616 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->alluni); |
617 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->nomulti); |
618 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->nouni); |
619 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->nobcast); |
620 | fbe78f4f | aliguori | } |
621 | fbe78f4f | aliguori | |
622 | fbe78f4f | aliguori | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) |
623 | fbe78f4f | aliguori | { |
624 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
625 | 2d9aba39 | Alex Williamson | int i;
|
626 | fbe78f4f | aliguori | |
627 | 9d6271b8 | aliguori | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
628 | fbe78f4f | aliguori | return -EINVAL;
|
629 | fbe78f4f | aliguori | |
630 | fbe78f4f | aliguori | virtio_load(&n->vdev, f); |
631 | fbe78f4f | aliguori | |
632 | 79674068 | aliguori | qemu_get_buffer(f, n->mac, ETH_ALEN); |
633 | fbe78f4f | aliguori | n->tx_timer_active = qemu_get_be32(f); |
634 | e46cb38f | aliguori | n->mergeable_rx_bufs = qemu_get_be32(f); |
635 | fbe78f4f | aliguori | |
636 | 9d6271b8 | aliguori | if (version_id >= 3) |
637 | 9d6271b8 | aliguori | n->status = qemu_get_be16(f); |
638 | 9d6271b8 | aliguori | |
639 | 002437cd | aliguori | if (version_id >= 4) { |
640 | f10c592e | Alex Williamson | if (version_id < 8) { |
641 | f10c592e | Alex Williamson | n->promisc = qemu_get_be32(f); |
642 | f10c592e | Alex Williamson | n->allmulti = qemu_get_be32(f); |
643 | f10c592e | Alex Williamson | } else {
|
644 | f10c592e | Alex Williamson | n->promisc = qemu_get_byte(f); |
645 | f10c592e | Alex Williamson | n->allmulti = qemu_get_byte(f); |
646 | f10c592e | Alex Williamson | } |
647 | 002437cd | aliguori | } |
648 | 002437cd | aliguori | |
649 | b6503ed9 | aliguori | if (version_id >= 5) { |
650 | b6503ed9 | aliguori | n->mac_table.in_use = qemu_get_be32(f); |
651 | b6503ed9 | aliguori | /* MAC_TABLE_ENTRIES may be different from the saved image */
|
652 | b6503ed9 | aliguori | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
|
653 | b6503ed9 | aliguori | qemu_get_buffer(f, n->mac_table.macs, |
654 | b6503ed9 | aliguori | n->mac_table.in_use * ETH_ALEN); |
655 | b6503ed9 | aliguori | } else if (n->mac_table.in_use) { |
656 | b6503ed9 | aliguori | qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR); |
657 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
|
658 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
659 | b6503ed9 | aliguori | } |
660 | b6503ed9 | aliguori | } |
661 | b6503ed9 | aliguori | |
662 | f21c0ed9 | aliguori | if (version_id >= 6) |
663 | f21c0ed9 | aliguori | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
|
664 | f21c0ed9 | aliguori | |
665 | 6c042c16 | Alex Williamson | if (version_id >= 7 && qemu_get_be32(f)) { |
666 | 6c042c16 | Alex Williamson | fprintf(stderr, |
667 | 6c042c16 | Alex Williamson | "virtio-net: saved image requires vnet header support\n");
|
668 | 6c042c16 | Alex Williamson | exit(1);
|
669 | 6c042c16 | Alex Williamson | } |
670 | 6c042c16 | Alex Williamson | |
671 | 8fd2a2f1 | Alex Williamson | if (version_id >= 9) { |
672 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = qemu_get_byte(f); |
673 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = qemu_get_byte(f); |
674 | 8fd2a2f1 | Alex Williamson | } |
675 | 8fd2a2f1 | Alex Williamson | |
676 | 015cb166 | Alex Williamson | if (version_id >= 10) { |
677 | 015cb166 | Alex Williamson | n->alluni = qemu_get_byte(f); |
678 | 015cb166 | Alex Williamson | n->nomulti = qemu_get_byte(f); |
679 | 015cb166 | Alex Williamson | n->nouni = qemu_get_byte(f); |
680 | 015cb166 | Alex Williamson | n->nobcast = qemu_get_byte(f); |
681 | 015cb166 | Alex Williamson | } |
682 | 015cb166 | Alex Williamson | |
683 | 2d9aba39 | Alex Williamson | /* Find the first multicast entry in the saved MAC filter */
|
684 | 2d9aba39 | Alex Williamson | for (i = 0; i < n->mac_table.in_use; i++) { |
685 | 2d9aba39 | Alex Williamson | if (n->mac_table.macs[i * ETH_ALEN] & 1) { |
686 | 2d9aba39 | Alex Williamson | break;
|
687 | 2d9aba39 | Alex Williamson | } |
688 | 2d9aba39 | Alex Williamson | } |
689 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = i; |
690 | 2d9aba39 | Alex Williamson | |
691 | fbe78f4f | aliguori | if (n->tx_timer_active) {
|
692 | fbe78f4f | aliguori | qemu_mod_timer(n->tx_timer, |
693 | fbe78f4f | aliguori | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
694 | fbe78f4f | aliguori | } |
695 | fbe78f4f | aliguori | |
696 | fbe78f4f | aliguori | return 0; |
697 | fbe78f4f | aliguori | } |
698 | fbe78f4f | aliguori | |
699 | b946a153 | aliguori | static void virtio_net_cleanup(VLANClientState *vc) |
700 | b946a153 | aliguori | { |
701 | b946a153 | aliguori | VirtIONet *n = vc->opaque; |
702 | b946a153 | aliguori | |
703 | 6243375f | Mark McLoughlin | qemu_purge_queued_packets(vc); |
704 | 6243375f | Mark McLoughlin | |
705 | b946a153 | aliguori | unregister_savevm("virtio-net", n);
|
706 | b946a153 | aliguori | |
707 | b946a153 | aliguori | qemu_free(n->mac_table.macs); |
708 | b946a153 | aliguori | qemu_free(n->vlans); |
709 | b946a153 | aliguori | |
710 | b946a153 | aliguori | qemu_del_timer(n->tx_timer); |
711 | b946a153 | aliguori | qemu_free_timer(n->tx_timer); |
712 | b946a153 | aliguori | |
713 | b946a153 | aliguori | virtio_cleanup(&n->vdev); |
714 | b946a153 | aliguori | } |
715 | b946a153 | aliguori | |
716 | 53c25cea | Paul Brook | VirtIODevice *virtio_net_init(DeviceState *dev) |
717 | fbe78f4f | aliguori | { |
718 | fbe78f4f | aliguori | VirtIONet *n; |
719 | fbe78f4f | aliguori | static int virtio_net_id; |
720 | fbe78f4f | aliguori | |
721 | 53c25cea | Paul Brook | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
|
722 | 53c25cea | Paul Brook | sizeof(struct virtio_net_config), |
723 | 53c25cea | Paul Brook | sizeof(VirtIONet));
|
724 | fbe78f4f | aliguori | |
725 | 0f03eca6 | aliguori | n->vdev.get_config = virtio_net_get_config; |
726 | 0f03eca6 | aliguori | n->vdev.set_config = virtio_net_set_config; |
727 | fbe78f4f | aliguori | n->vdev.get_features = virtio_net_get_features; |
728 | fbe78f4f | aliguori | n->vdev.set_features = virtio_net_set_features; |
729 | 8eca6b1b | aliguori | n->vdev.bad_features = virtio_net_bad_features; |
730 | 002437cd | aliguori | n->vdev.reset = virtio_net_reset; |
731 | fbe78f4f | aliguori | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
|
732 | fbe78f4f | aliguori | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
|
733 | 4ffb17f5 | Alex Williamson | n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
|
734 | 53c25cea | Paul Brook | qdev_get_macaddr(dev, n->mac); |
735 | 554c97dd | aliguori | n->status = VIRTIO_NET_S_LINK_UP; |
736 | 53c25cea | Paul Brook | n->vc = qdev_get_vlan_client(dev, |
737 | b946a153 | aliguori | virtio_net_can_receive, |
738 | 463af534 | Mark McLoughlin | virtio_net_receive, NULL,
|
739 | b946a153 | aliguori | virtio_net_cleanup, n); |
740 | 554c97dd | aliguori | n->vc->link_status_changed = virtio_net_set_link_status; |
741 | fbe78f4f | aliguori | |
742 | 96d5e201 | aliguori | qemu_format_nic_info_str(n->vc, n->mac); |
743 | 96d5e201 | aliguori | |
744 | fbe78f4f | aliguori | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
745 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
746 | fbe78f4f | aliguori | n->mergeable_rx_bufs = 0;
|
747 | 002437cd | aliguori | n->promisc = 1; /* for compatibility */ |
748 | fbe78f4f | aliguori | |
749 | b6503ed9 | aliguori | n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); |
750 | b6503ed9 | aliguori | |
751 | f21c0ed9 | aliguori | n->vlans = qemu_mallocz(MAX_VLAN >> 3);
|
752 | ffe6370c | Michael S. Tsirkin | if (dev->nd->nvectors == NIC_NVECTORS_UNSPECIFIED)
|
753 | ffe6370c | Michael S. Tsirkin | n->vdev.nvectors = 3;
|
754 | ffe6370c | Michael S. Tsirkin | else
|
755 | ffe6370c | Michael S. Tsirkin | n->vdev.nvectors = dev->nd->nvectors; |
756 | f21c0ed9 | aliguori | |
757 | 9d6271b8 | aliguori | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
|
758 | fbe78f4f | aliguori | virtio_net_save, virtio_net_load, n); |
759 | cf21e106 | Paul Brook | |
760 | 53c25cea | Paul Brook | return &n->vdev;
|
761 | cf21e106 | Paul Brook | } |