blob: 7d92c2d022b0a9d3b67ee973bff8af5f94ac7bf6 [file] [log] [blame]
aliguorie613b062009-04-22 15:19:35 +00001/*
2 * xen paravirt network card backend
3 *
4 * (c) Gerd Hoffmann <kraxel@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * with this program; if not, see <http://www.gnu.org/licenses/>.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010017 *
18 * Contributions after 2012-01-13 are licensed under the terms of the
19 * GNU GPL, version 2 or (at your option) any later version.
aliguorie613b062009-04-22 15:19:35 +000020 */
21
Peter Maydell21cbfe52016-01-26 18:17:06 +000022#include "qemu/osdep.h"
aliguorie613b062009-04-22 15:19:35 +000023#include <sys/socket.h>
24#include <sys/ioctl.h>
aliguorie613b062009-04-22 15:19:35 +000025#include <sys/wait.h>
aliguorie613b062009-04-22 15:19:35 +000026
Paolo Bonzini1422e322012-10-24 08:43:34 +020027#include "net/net.h"
Mark McLoughlin7200ac32009-10-22 17:49:03 +010028#include "net/checksum.h"
Mark McLoughlin658788c2009-11-25 18:49:28 +000029#include "net/util.h"
Paul Durrant2d0ed5e2019-01-08 14:48:46 +000030#include "hw/xen/xen-legacy-backend.h"
aliguorie613b062009-04-22 15:19:35 +000031
Anthony PERARDa3434a22019-06-21 11:54:41 +010032#include "hw/xen/interface/io/netif.h"
Anthony PERARDb41f6712012-06-21 11:43:59 +000033
aliguorie613b062009-04-22 15:19:35 +000034/* ------------------------------------------------------------- */
35
36struct XenNetDev {
Paul Durrant2d0ed5e2019-01-08 14:48:46 +000037 struct XenLegacyDevice xendev; /* must be first */
aliguorie613b062009-04-22 15:19:35 +000038 char *mac;
39 int tx_work;
40 int tx_ring_ref;
41 int rx_ring_ref;
42 struct netif_tx_sring *txs;
43 struct netif_rx_sring *rxs;
44 netif_tx_back_ring_t tx_ring;
45 netif_rx_back_ring_t rx_ring;
Mark McLoughlin658788c2009-11-25 18:49:28 +000046 NICConf conf;
47 NICState *nic;
aliguorie613b062009-04-22 15:19:35 +000048};
49
50/* ------------------------------------------------------------- */
51
52static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st)
53{
54 RING_IDX i = netdev->tx_ring.rsp_prod_pvt;
55 netif_tx_response_t *resp;
56 int notify;
57
58 resp = RING_GET_RESPONSE(&netdev->tx_ring, i);
59 resp->id = txp->id;
60 resp->status = st;
61
62#if 0
Anthony PERARD209cd7a2010-09-23 12:28:45 +010063 if (txp->flags & NETTXF_extra_info) {
64 RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
65 }
aliguorie613b062009-04-22 15:19:35 +000066#endif
67
68 netdev->tx_ring.rsp_prod_pvt = ++i;
69 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify);
Anthony PERARD209cd7a2010-09-23 12:28:45 +010070 if (notify) {
Emil Condreaba18fa22016-10-25 08:50:16 +030071 xen_pv_send_notify(&netdev->xendev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +010072 }
aliguorie613b062009-04-22 15:19:35 +000073
74 if (i == netdev->tx_ring.req_cons) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +010075 int more_to_do;
76 RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do);
77 if (more_to_do) {
78 netdev->tx_work++;
79 }
aliguorie613b062009-04-22 15:19:35 +000080 }
81}
82
83static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end)
84{
85#if 0
86 /*
87 * Hmm, why netback fails everything in the ring?
88 * Should we do that even when not supporting SG and TSO?
89 */
90 RING_IDX cons = netdev->tx_ring.req_cons;
91
92 do {
Anthony PERARD209cd7a2010-09-23 12:28:45 +010093 make_tx_response(netif, txp, NETIF_RSP_ERROR);
94 if (cons >= end) {
95 break;
96 }
97 txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
aliguorie613b062009-04-22 15:19:35 +000098 } while (1);
99 netdev->tx_ring.req_cons = cons;
100 netif_schedule_work(netif);
101 netif_put(netif);
102#else
103 net_tx_response(netdev, txp, NETIF_RSP_ERROR);
104#endif
105}
106
107static void net_tx_packets(struct XenNetDev *netdev)
108{
109 netif_tx_request_t txreq;
110 RING_IDX rc, rp;
111 void *page;
112 void *tmpbuf = NULL;
113
114 for (;;) {
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100115 rc = netdev->tx_ring.req_cons;
116 rp = netdev->tx_ring.sring->req_prod;
117 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
aliguorie613b062009-04-22 15:19:35 +0000118
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100119 while ((rc != rp)) {
120 if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) {
121 break;
122 }
123 memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
124 netdev->tx_ring.req_cons = ++rc;
aliguorie613b062009-04-22 15:19:35 +0000125
126#if 1
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100127 /* should not happen in theory, we don't announce the *
128 * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
129 if (txreq.flags & NETTXF_extra_info) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300130 xen_pv_printf(&netdev->xendev, 0, "FIXME: extra info flag\n");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100131 net_tx_error(netdev, &txreq, rc);
132 continue;
133 }
134 if (txreq.flags & NETTXF_more_data) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300135 xen_pv_printf(&netdev->xendev, 0, "FIXME: more data flag\n");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100136 net_tx_error(netdev, &txreq, rc);
137 continue;
138 }
aliguorie613b062009-04-22 15:19:35 +0000139#endif
140
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100141 if (txreq.size < 14) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300142 xen_pv_printf(&netdev->xendev, 0, "bad packet size: %d\n",
Emil Condreab9730c52016-10-25 08:50:08 +0300143 txreq.size);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100144 net_tx_error(netdev, &txreq, rc);
145 continue;
146 }
aliguorie613b062009-04-22 15:19:35 +0000147
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100148 if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300149 xen_pv_printf(&netdev->xendev, 0, "error: page crossing\n");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100150 net_tx_error(netdev, &txreq, rc);
151 continue;
152 }
aliguorie613b062009-04-22 15:19:35 +0000153
Emil Condrea96c77db2016-10-25 08:50:14 +0300154 xen_pv_printf(&netdev->xendev, 3,
Emil Condreac22e91b2016-10-25 08:50:07 +0300155 "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100156 txreq.gref, txreq.offset, txreq.size, txreq.flags,
157 (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "",
158 (txreq.flags & NETTXF_data_validated) ? " data_validated" : "",
159 (txreq.flags & NETTXF_more_data) ? " more_data" : "",
160 (txreq.flags & NETTXF_extra_info) ? " extra_info" : "");
aliguorie613b062009-04-22 15:19:35 +0000161
Paul Durrant58560f22018-05-17 16:35:53 +0100162 page = xen_be_map_grant_ref(&netdev->xendev, txreq.gref,
163 PROT_READ);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100164 if (page == NULL) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300165 xen_pv_printf(&netdev->xendev, 0,
Emil Condreac22e91b2016-10-25 08:50:07 +0300166 "error: tx gref dereference failed (%d)\n",
167 txreq.gref);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100168 net_tx_error(netdev, &txreq, rc);
169 continue;
170 }
171 if (txreq.flags & NETTXF_csum_blank) {
aliguorie613b062009-04-22 15:19:35 +0000172 /* have read-only mapping -> can't fill checksum in-place */
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100173 if (!tmpbuf) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500174 tmpbuf = g_malloc(XC_PAGE_SIZE);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100175 }
aliguorie613b062009-04-22 15:19:35 +0000176 memcpy(tmpbuf, page + txreq.offset, txreq.size);
Bin Mengf5746332020-12-11 17:35:12 +0800177 net_checksum_calculate(tmpbuf, txreq.size, CSUM_ALL);
Jason Wangb356f762013-01-30 19:12:22 +0800178 qemu_send_packet(qemu_get_queue(netdev->nic), tmpbuf,
179 txreq.size);
aliguorie613b062009-04-22 15:19:35 +0000180 } else {
Jason Wangb356f762013-01-30 19:12:22 +0800181 qemu_send_packet(qemu_get_queue(netdev->nic),
182 page + txreq.offset, txreq.size);
aliguorie613b062009-04-22 15:19:35 +0000183 }
Paul Durrant58560f22018-05-17 16:35:53 +0100184 xen_be_unmap_grant_ref(&netdev->xendev, page);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100185 net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
186 }
187 if (!netdev->tx_work) {
188 break;
189 }
190 netdev->tx_work = 0;
aliguorie613b062009-04-22 15:19:35 +0000191 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500192 g_free(tmpbuf);
aliguorie613b062009-04-22 15:19:35 +0000193}
194
195/* ------------------------------------------------------------- */
196
197static void net_rx_response(struct XenNetDev *netdev,
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100198 netif_rx_request_t *req, int8_t st,
199 uint16_t offset, uint16_t size,
200 uint16_t flags)
aliguorie613b062009-04-22 15:19:35 +0000201{
202 RING_IDX i = netdev->rx_ring.rsp_prod_pvt;
203 netif_rx_response_t *resp;
204 int notify;
205
206 resp = RING_GET_RESPONSE(&netdev->rx_ring, i);
207 resp->offset = offset;
208 resp->flags = flags;
209 resp->id = req->id;
210 resp->status = (int16_t)size;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100211 if (st < 0) {
212 resp->status = (int16_t)st;
213 }
aliguorie613b062009-04-22 15:19:35 +0000214
Emil Condrea96c77db2016-10-25 08:50:14 +0300215 xen_pv_printf(&netdev->xendev, 3,
Emil Condreab9730c52016-10-25 08:50:08 +0300216 "rx response: idx %d, status %d, flags 0x%x\n",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100217 i, resp->status, resp->flags);
aliguorie613b062009-04-22 15:19:35 +0000218
219 netdev->rx_ring.rsp_prod_pvt = ++i;
220 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100221 if (notify) {
Emil Condreaba18fa22016-10-25 08:50:16 +0300222 xen_pv_send_notify(&netdev->xendev);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100223 }
aliguorie613b062009-04-22 15:19:35 +0000224}
225
226#define NET_IP_ALIGN 2
227
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100228static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size)
aliguorie613b062009-04-22 15:19:35 +0000229{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800230 struct XenNetDev *netdev = qemu_get_nic_opaque(nc);
aliguorie613b062009-04-22 15:19:35 +0000231 netif_rx_request_t rxreq;
232 RING_IDX rc, rp;
233 void *page;
234
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100235 if (netdev->xendev.be_state != XenbusStateConnected) {
236 return -1;
237 }
aliguorie613b062009-04-22 15:19:35 +0000238
239 rc = netdev->rx_ring.req_cons;
240 rp = netdev->rx_ring.sring->req_prod;
241 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
242
243 if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
Fam Zheng7bba83b2015-07-28 17:52:56 +0800244 return 0;
aliguorie613b062009-04-22 15:19:35 +0000245 }
246 if (size > XC_PAGE_SIZE - NET_IP_ALIGN) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300247 xen_pv_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100248 (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN);
249 return -1;
aliguorie613b062009-04-22 15:19:35 +0000250 }
251
252 memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
253 netdev->rx_ring.req_cons = ++rc;
254
Paul Durrant58560f22018-05-17 16:35:53 +0100255 page = xen_be_map_grant_ref(&netdev->xendev, rxreq.gref, PROT_WRITE);
aliguorie613b062009-04-22 15:19:35 +0000256 if (page == NULL) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300257 xen_pv_printf(&netdev->xendev, 0,
Emil Condreab9730c52016-10-25 08:50:08 +0300258 "error: rx gref dereference failed (%d)\n",
aliguorie613b062009-04-22 15:19:35 +0000259 rxreq.gref);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100260 net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0);
261 return -1;
aliguorie613b062009-04-22 15:19:35 +0000262 }
263 memcpy(page + NET_IP_ALIGN, buf, size);
Paul Durrant58560f22018-05-17 16:35:53 +0100264 xen_be_unmap_grant_ref(&netdev->xendev, page);
aliguorie613b062009-04-22 15:19:35 +0000265 net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100266
267 return size;
aliguorie613b062009-04-22 15:19:35 +0000268}
269
270/* ------------------------------------------------------------- */
271
Mark McLoughlin658788c2009-11-25 18:49:28 +0000272static NetClientInfo net_xen_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600273 .type = NET_CLIENT_DRIVER_NIC,
Mark McLoughlin658788c2009-11-25 18:49:28 +0000274 .size = sizeof(NICState),
Mark McLoughlin658788c2009-11-25 18:49:28 +0000275 .receive = net_rx_packet,
276};
277
Paul Durrant2d0ed5e2019-01-08 14:48:46 +0000278static int net_init(struct XenLegacyDevice *xendev)
aliguorie613b062009-04-22 15:19:35 +0000279{
280 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
aliguorie613b062009-04-22 15:19:35 +0000281
282 /* read xenstore entries */
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100283 if (netdev->mac == NULL) {
284 netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac");
285 }
aliguorie613b062009-04-22 15:19:35 +0000286
287 /* do we have all we need? */
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100288 if (netdev->mac == NULL) {
Mark McLoughlin658788c2009-11-25 18:49:28 +0000289 return -1;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100290 }
291
292 if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) {
293 return -1;
294 }
Mark McLoughlin658788c2009-11-25 18:49:28 +0000295
Mark McLoughlin658788c2009-11-25 18:49:28 +0000296 netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
297 "xen", NULL, netdev);
298
Laurent Vivier53b85d92022-10-21 11:09:10 +0200299 qemu_set_info_str(qemu_get_queue(netdev->nic),
300 "nic: xenbus vif macaddr=%s", netdev->mac);
aliguorie613b062009-04-22 15:19:35 +0000301
302 /* fill info */
303 xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1);
304 xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0);
305
306 return 0;
307}
308
Paul Durrant2d0ed5e2019-01-08 14:48:46 +0000309static int net_connect(struct XenLegacyDevice *xendev)
aliguorie613b062009-04-22 15:19:35 +0000310{
311 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
312 int rx_copy;
313
314 if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100315 &netdev->tx_ring_ref) == -1) {
316 return -1;
317 }
aliguorie613b062009-04-22 15:19:35 +0000318 if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100319 &netdev->rx_ring_ref) == -1) {
320 return 1;
321 }
aliguorie613b062009-04-22 15:19:35 +0000322 if (xenstore_read_fe_int(&netdev->xendev, "event-channel",
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100323 &netdev->xendev.remote_port) == -1) {
324 return -1;
325 }
aliguorie613b062009-04-22 15:19:35 +0000326
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100327 if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) {
328 rx_copy = 0;
329 }
aliguorie613b062009-04-22 15:19:35 +0000330 if (rx_copy == 0) {
Emil Condrea96c77db2016-10-25 08:50:14 +0300331 xen_pv_printf(&netdev->xendev, 0,
Emil Condreab9730c52016-10-25 08:50:08 +0300332 "frontend doesn't support rx-copy.\n");
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100333 return -1;
aliguorie613b062009-04-22 15:19:35 +0000334 }
335
Paul Durrant58560f22018-05-17 16:35:53 +0100336 netdev->txs = xen_be_map_grant_ref(&netdev->xendev,
337 netdev->tx_ring_ref,
338 PROT_READ | PROT_WRITE);
Chen Gangb4f72e32014-12-17 04:48:54 +0800339 if (!netdev->txs) {
340 return -1;
341 }
Paul Durrant58560f22018-05-17 16:35:53 +0100342 netdev->rxs = xen_be_map_grant_ref(&netdev->xendev,
343 netdev->rx_ring_ref,
344 PROT_READ | PROT_WRITE);
Chen Gangb4f72e32014-12-17 04:48:54 +0800345 if (!netdev->rxs) {
Paul Durrant58560f22018-05-17 16:35:53 +0100346 xen_be_unmap_grant_ref(&netdev->xendev, netdev->txs);
Chen Gangb4f72e32014-12-17 04:48:54 +0800347 netdev->txs = NULL;
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100348 return -1;
349 }
aliguorie613b062009-04-22 15:19:35 +0000350 BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE);
351 BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE);
352
353 xen_be_bind_evtchn(&netdev->xendev);
354
Emil Condrea96c77db2016-10-25 08:50:14 +0300355 xen_pv_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100356 "remote port %d, local port %d\n",
357 netdev->tx_ring_ref, netdev->rx_ring_ref,
358 netdev->xendev.remote_port, netdev->xendev.local_port);
Gerd Hoffmann3e3cabc2009-06-11 11:32:51 +0200359
360 net_tx_packets(netdev);
aliguorie613b062009-04-22 15:19:35 +0000361 return 0;
362}
363
Paul Durrant2d0ed5e2019-01-08 14:48:46 +0000364static void net_disconnect(struct XenLegacyDevice *xendev)
aliguorie613b062009-04-22 15:19:35 +0000365{
366 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
367
Emil Condrea65807f42016-10-25 08:50:15 +0300368 xen_pv_unbind_evtchn(&netdev->xendev);
aliguorie613b062009-04-22 15:19:35 +0000369
370 if (netdev->txs) {
Paul Durrant58560f22018-05-17 16:35:53 +0100371 xen_be_unmap_grant_ref(&netdev->xendev, netdev->txs);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100372 netdev->txs = NULL;
aliguorie613b062009-04-22 15:19:35 +0000373 }
374 if (netdev->rxs) {
Paul Durrant58560f22018-05-17 16:35:53 +0100375 xen_be_unmap_grant_ref(&netdev->xendev, netdev->rxs);
Anthony PERARD209cd7a2010-09-23 12:28:45 +0100376 netdev->rxs = NULL;
aliguorie613b062009-04-22 15:19:35 +0000377 }
aliguorie613b062009-04-22 15:19:35 +0000378}
379
Paul Durrant2d0ed5e2019-01-08 14:48:46 +0000380static void net_event(struct XenLegacyDevice *xendev)
aliguorie613b062009-04-22 15:19:35 +0000381{
382 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
383 net_tx_packets(netdev);
Jason Wangb356f762013-01-30 19:12:22 +0800384 qemu_flush_queued_packets(qemu_get_queue(netdev->nic));
aliguorie613b062009-04-22 15:19:35 +0000385}
386
Paul Durrant2d0ed5e2019-01-08 14:48:46 +0000387static int net_free(struct XenLegacyDevice *xendev)
aliguorie613b062009-04-22 15:19:35 +0000388{
389 struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
390
Chen Gangd4685832014-12-17 04:52:16 +0800391 if (netdev->nic) {
392 qemu_del_nic(netdev->nic);
393 netdev->nic = NULL;
394 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500395 g_free(netdev->mac);
Chen Ganga39d97c2014-12-17 04:58:42 +0800396 netdev->mac = NULL;
aliguorie613b062009-04-22 15:19:35 +0000397 return 0;
398}
399
400/* ------------------------------------------------------------- */
401
402struct XenDevOps xen_netdev_ops = {
403 .size = sizeof(struct XenNetDev),
404 .flags = DEVOPS_FLAG_NEED_GNTDEV,
405 .init = net_init,
John Haxby384087b2011-06-17 12:15:35 +0000406 .initialise = net_connect,
aliguorie613b062009-04-22 15:19:35 +0000407 .event = net_event,
408 .disconnect = net_disconnect,
409 .free = net_free,
410};