aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 1 | /* |
| 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 Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 16 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <signal.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 28 | #include <sys/socket.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <sys/wait.h> |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 34 | |
| 35 | #include <xs.h> |
| 36 | #include <xenctrl.h> |
| 37 | #include <xen/io/xenbus.h> |
| 38 | #include <xen/io/netif.h> |
| 39 | |
| 40 | #include "hw.h" |
| 41 | #include "net.h" |
Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 42 | #include "net/checksum.h" |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 43 | #include "net/util.h" |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 44 | #include "qemu-char.h" |
| 45 | #include "xen_backend.h" |
| 46 | |
| 47 | /* ------------------------------------------------------------- */ |
| 48 | |
| 49 | struct XenNetDev { |
| 50 | struct XenDevice xendev; /* must be first */ |
| 51 | char *mac; |
| 52 | int tx_work; |
| 53 | int tx_ring_ref; |
| 54 | int rx_ring_ref; |
| 55 | struct netif_tx_sring *txs; |
| 56 | struct netif_rx_sring *rxs; |
| 57 | netif_tx_back_ring_t tx_ring; |
| 58 | netif_rx_back_ring_t rx_ring; |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 59 | NICConf conf; |
| 60 | NICState *nic; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /* ------------------------------------------------------------- */ |
| 64 | |
| 65 | static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st) |
| 66 | { |
| 67 | RING_IDX i = netdev->tx_ring.rsp_prod_pvt; |
| 68 | netif_tx_response_t *resp; |
| 69 | int notify; |
| 70 | |
| 71 | resp = RING_GET_RESPONSE(&netdev->tx_ring, i); |
| 72 | resp->id = txp->id; |
| 73 | resp->status = st; |
| 74 | |
| 75 | #if 0 |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 76 | if (txp->flags & NETTXF_extra_info) { |
| 77 | RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL; |
| 78 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | netdev->tx_ring.rsp_prod_pvt = ++i; |
| 82 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify); |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 83 | if (notify) { |
| 84 | xen_be_send_notify(&netdev->xendev); |
| 85 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 86 | |
| 87 | if (i == netdev->tx_ring.req_cons) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 88 | int more_to_do; |
| 89 | RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do); |
| 90 | if (more_to_do) { |
| 91 | netdev->tx_work++; |
| 92 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end) |
| 97 | { |
| 98 | #if 0 |
| 99 | /* |
| 100 | * Hmm, why netback fails everything in the ring? |
| 101 | * Should we do that even when not supporting SG and TSO? |
| 102 | */ |
| 103 | RING_IDX cons = netdev->tx_ring.req_cons; |
| 104 | |
| 105 | do { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 106 | make_tx_response(netif, txp, NETIF_RSP_ERROR); |
| 107 | if (cons >= end) { |
| 108 | break; |
| 109 | } |
| 110 | txp = RING_GET_REQUEST(&netdev->tx_ring, cons++); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 111 | } while (1); |
| 112 | netdev->tx_ring.req_cons = cons; |
| 113 | netif_schedule_work(netif); |
| 114 | netif_put(netif); |
| 115 | #else |
| 116 | net_tx_response(netdev, txp, NETIF_RSP_ERROR); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | static void net_tx_packets(struct XenNetDev *netdev) |
| 121 | { |
| 122 | netif_tx_request_t txreq; |
| 123 | RING_IDX rc, rp; |
| 124 | void *page; |
| 125 | void *tmpbuf = NULL; |
| 126 | |
| 127 | for (;;) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 128 | rc = netdev->tx_ring.req_cons; |
| 129 | rp = netdev->tx_ring.sring->req_prod; |
| 130 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 131 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 132 | while ((rc != rp)) { |
| 133 | if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) { |
| 134 | break; |
| 135 | } |
| 136 | memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq)); |
| 137 | netdev->tx_ring.req_cons = ++rc; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 138 | |
| 139 | #if 1 |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 140 | /* should not happen in theory, we don't announce the * |
| 141 | * feature-{sg,gso,whatelse} flags in xenstore (yet?) */ |
| 142 | if (txreq.flags & NETTXF_extra_info) { |
| 143 | xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n"); |
| 144 | net_tx_error(netdev, &txreq, rc); |
| 145 | continue; |
| 146 | } |
| 147 | if (txreq.flags & NETTXF_more_data) { |
| 148 | xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n"); |
| 149 | net_tx_error(netdev, &txreq, rc); |
| 150 | continue; |
| 151 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 152 | #endif |
| 153 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 154 | if (txreq.size < 14) { |
| 155 | xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size); |
| 156 | net_tx_error(netdev, &txreq, rc); |
| 157 | continue; |
| 158 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 159 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 160 | if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) { |
| 161 | xen_be_printf(&netdev->xendev, 0, "error: page crossing\n"); |
| 162 | net_tx_error(netdev, &txreq, rc); |
| 163 | continue; |
| 164 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 165 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 166 | xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n", |
| 167 | txreq.gref, txreq.offset, txreq.size, txreq.flags, |
| 168 | (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "", |
| 169 | (txreq.flags & NETTXF_data_validated) ? " data_validated" : "", |
| 170 | (txreq.flags & NETTXF_more_data) ? " more_data" : "", |
| 171 | (txreq.flags & NETTXF_extra_info) ? " extra_info" : ""); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 172 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 173 | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
| 174 | netdev->xendev.dom, |
| 175 | txreq.gref, PROT_READ); |
| 176 | if (page == NULL) { |
| 177 | xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n", |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 178 | txreq.gref); |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 179 | net_tx_error(netdev, &txreq, rc); |
| 180 | continue; |
| 181 | } |
| 182 | if (txreq.flags & NETTXF_csum_blank) { |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 183 | /* have read-only mapping -> can't fill checksum in-place */ |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 184 | if (!tmpbuf) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 185 | tmpbuf = g_malloc(XC_PAGE_SIZE); |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 186 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 187 | memcpy(tmpbuf, page + txreq.offset, txreq.size); |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 188 | net_checksum_calculate(tmpbuf, txreq.size); |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 189 | qemu_send_packet(&netdev->nic->nc, tmpbuf, txreq.size); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 190 | } else { |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 191 | qemu_send_packet(&netdev->nic->nc, page + txreq.offset, txreq.size); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 192 | } |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 193 | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1); |
| 194 | net_tx_response(netdev, &txreq, NETIF_RSP_OKAY); |
| 195 | } |
| 196 | if (!netdev->tx_work) { |
| 197 | break; |
| 198 | } |
| 199 | netdev->tx_work = 0; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 200 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 201 | g_free(tmpbuf); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* ------------------------------------------------------------- */ |
| 205 | |
| 206 | static void net_rx_response(struct XenNetDev *netdev, |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 207 | netif_rx_request_t *req, int8_t st, |
| 208 | uint16_t offset, uint16_t size, |
| 209 | uint16_t flags) |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 210 | { |
| 211 | RING_IDX i = netdev->rx_ring.rsp_prod_pvt; |
| 212 | netif_rx_response_t *resp; |
| 213 | int notify; |
| 214 | |
| 215 | resp = RING_GET_RESPONSE(&netdev->rx_ring, i); |
| 216 | resp->offset = offset; |
| 217 | resp->flags = flags; |
| 218 | resp->id = req->id; |
| 219 | resp->status = (int16_t)size; |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 220 | if (st < 0) { |
| 221 | resp->status = (int16_t)st; |
| 222 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 223 | |
| 224 | xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n", |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 225 | i, resp->status, resp->flags); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 226 | |
| 227 | netdev->rx_ring.rsp_prod_pvt = ++i; |
| 228 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify); |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 229 | if (notify) { |
| 230 | xen_be_send_notify(&netdev->xendev); |
| 231 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | #define NET_IP_ALIGN 2 |
| 235 | |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 236 | static int net_rx_ok(VLANClientState *nc) |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 237 | { |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 238 | struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 239 | RING_IDX rc, rp; |
| 240 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 241 | if (netdev->xendev.be_state != XenbusStateConnected) { |
| 242 | return 0; |
| 243 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 244 | |
| 245 | rc = netdev->rx_ring.req_cons; |
| 246 | rp = netdev->rx_ring.sring->req_prod; |
| 247 | xen_rmb(); |
| 248 | |
| 249 | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 250 | xen_be_printf(&netdev->xendev, 2, "%s: no rx buffers (%d/%d)\n", |
| 251 | __FUNCTION__, rc, rp); |
| 252 | return 0; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 253 | } |
| 254 | return 1; |
| 255 | } |
| 256 | |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 257 | static ssize_t net_rx_packet(VLANClientState *nc, const uint8_t *buf, size_t size) |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 258 | { |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 259 | struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 260 | netif_rx_request_t rxreq; |
| 261 | RING_IDX rc, rp; |
| 262 | void *page; |
| 263 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 264 | if (netdev->xendev.be_state != XenbusStateConnected) { |
| 265 | return -1; |
| 266 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 267 | |
| 268 | rc = netdev->rx_ring.req_cons; |
| 269 | rp = netdev->rx_ring.sring->req_prod; |
| 270 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ |
| 271 | |
| 272 | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 273 | xen_be_printf(&netdev->xendev, 2, "no buffer, drop packet\n"); |
| 274 | return -1; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 275 | } |
| 276 | if (size > XC_PAGE_SIZE - NET_IP_ALIGN) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 277 | xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)", |
| 278 | (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN); |
| 279 | return -1; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq)); |
| 283 | netdev->rx_ring.req_cons = ++rc; |
| 284 | |
| 285 | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 286 | netdev->xendev.dom, |
| 287 | rxreq.gref, PROT_WRITE); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 288 | if (page == NULL) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 289 | xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n", |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 290 | rxreq.gref); |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 291 | net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0); |
| 292 | return -1; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 293 | } |
| 294 | memcpy(page + NET_IP_ALIGN, buf, size); |
| 295 | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1); |
| 296 | net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0); |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 297 | |
| 298 | return size; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* ------------------------------------------------------------- */ |
| 302 | |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 303 | static NetClientInfo net_xen_info = { |
| 304 | .type = NET_CLIENT_TYPE_NIC, |
| 305 | .size = sizeof(NICState), |
| 306 | .can_receive = net_rx_ok, |
| 307 | .receive = net_rx_packet, |
| 308 | }; |
| 309 | |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 310 | static int net_init(struct XenDevice *xendev) |
| 311 | { |
| 312 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 313 | |
| 314 | /* read xenstore entries */ |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 315 | if (netdev->mac == NULL) { |
| 316 | netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac"); |
| 317 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 318 | |
| 319 | /* do we have all we need? */ |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 320 | if (netdev->mac == NULL) { |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 321 | return -1; |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) { |
| 325 | return -1; |
| 326 | } |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 327 | |
| 328 | netdev->conf.vlan = qemu_find_vlan(netdev->xendev.dev, 1); |
| 329 | netdev->conf.peer = NULL; |
| 330 | |
| 331 | netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf, |
| 332 | "xen", NULL, netdev); |
| 333 | |
| 334 | snprintf(netdev->nic->nc.info_str, sizeof(netdev->nic->nc.info_str), |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 335 | "nic: xenbus vif macaddr=%s", netdev->mac); |
| 336 | |
| 337 | /* fill info */ |
| 338 | xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); |
| 339 | xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int net_connect(struct XenDevice *xendev) |
| 345 | { |
| 346 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
| 347 | int rx_copy; |
| 348 | |
| 349 | if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref", |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 350 | &netdev->tx_ring_ref) == -1) { |
| 351 | return -1; |
| 352 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 353 | if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref", |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 354 | &netdev->rx_ring_ref) == -1) { |
| 355 | return 1; |
| 356 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 357 | if (xenstore_read_fe_int(&netdev->xendev, "event-channel", |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 358 | &netdev->xendev.remote_port) == -1) { |
| 359 | return -1; |
| 360 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 361 | |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 362 | if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) { |
| 363 | rx_copy = 0; |
| 364 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 365 | if (rx_copy == 0) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 366 | xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n"); |
| 367 | return -1; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 371 | netdev->xendev.dom, |
| 372 | netdev->tx_ring_ref, |
| 373 | PROT_READ | PROT_WRITE); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 374 | netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 375 | netdev->xendev.dom, |
| 376 | netdev->rx_ring_ref, |
| 377 | PROT_READ | PROT_WRITE); |
| 378 | if (!netdev->txs || !netdev->rxs) { |
| 379 | return -1; |
| 380 | } |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 381 | BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE); |
| 382 | BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE); |
| 383 | |
| 384 | xen_be_bind_evtchn(&netdev->xendev); |
| 385 | |
| 386 | xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, " |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 387 | "remote port %d, local port %d\n", |
| 388 | netdev->tx_ring_ref, netdev->rx_ring_ref, |
| 389 | netdev->xendev.remote_port, netdev->xendev.local_port); |
Gerd Hoffmann | 3e3cabc | 2009-06-11 11:32:51 +0200 | [diff] [blame] | 390 | |
| 391 | net_tx_packets(netdev); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static void net_disconnect(struct XenDevice *xendev) |
| 396 | { |
| 397 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
| 398 | |
| 399 | xen_be_unbind_evtchn(&netdev->xendev); |
| 400 | |
| 401 | if (netdev->txs) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 402 | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1); |
| 403 | netdev->txs = NULL; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 404 | } |
| 405 | if (netdev->rxs) { |
Anthony PERARD | 209cd7a | 2010-09-23 12:28:45 +0100 | [diff] [blame] | 406 | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1); |
| 407 | netdev->rxs = NULL; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 408 | } |
Mark McLoughlin | 658788c | 2009-11-25 18:49:28 +0000 | [diff] [blame] | 409 | if (netdev->nic) { |
| 410 | qemu_del_vlan_client(&netdev->nic->nc); |
| 411 | netdev->nic = NULL; |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | static void net_event(struct XenDevice *xendev) |
| 416 | { |
| 417 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
| 418 | net_tx_packets(netdev); |
| 419 | } |
| 420 | |
| 421 | static int net_free(struct XenDevice *xendev) |
| 422 | { |
| 423 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
| 424 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 425 | g_free(netdev->mac); |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* ------------------------------------------------------------- */ |
| 430 | |
| 431 | struct XenDevOps xen_netdev_ops = { |
| 432 | .size = sizeof(struct XenNetDev), |
| 433 | .flags = DEVOPS_FLAG_NEED_GNTDEV, |
| 434 | .init = net_init, |
John Haxby | 384087b | 2011-06-17 12:15:35 +0000 | [diff] [blame] | 435 | .initialise = net_connect, |
aliguori | e613b06 | 2009-04-22 15:19:35 +0000 | [diff] [blame] | 436 | .event = net_event, |
| 437 | .disconnect = net_disconnect, |
| 438 | .free = net_free, |
| 439 | }; |