1 /* 2 * Network-device interface management. 3 * 4 * Copyright (c) 2004-2005, Keir Fraser 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License version 2 8 * as published by the Free Software Foundation; or, when distributed 9 * separately from the Linux kernel or incorporated into other 10 * software packages, subject to the following license: 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a copy 13 * of this source file (the "Software"), to deal in the Software without 14 * restriction, including without limitation the rights to use, copy, modify, 15 * merge, publish, distribute, sublicense, and/or sell copies of the Software, 16 * and to permit persons to whom the Software is furnished to do so, subject to 17 * the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included in 20 * all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 28 * IN THE SOFTWARE. 29 */ 30 31 #include "common.h" 32 33 #include <linux/kthread.h> 34 #include <linux/ethtool.h> 35 #include <linux/rtnetlink.h> 36 #include <linux/if_vlan.h> 37 #include <linux/vmalloc.h> 38 39 #include <xen/events.h> 40 #include <asm/xen/hypercall.h> 41 42 #define XENVIF_QUEUE_LENGTH 32 43 #define XENVIF_NAPI_WEIGHT 64 44 45 int xenvif_schedulable(struct xenvif *vif) 46 { 47 return netif_running(vif->dev) && netif_carrier_ok(vif->dev); 48 } 49 50 static int xenvif_rx_schedulable(struct xenvif *vif) 51 { 52 return xenvif_schedulable(vif) && !xenvif_rx_ring_full(vif); 53 } 54 55 static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) 56 { 57 struct xenvif *vif = dev_id; 58 59 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) 60 napi_schedule(&vif->napi); 61 62 return IRQ_HANDLED; 63 } 64 65 static int xenvif_poll(struct napi_struct *napi, int budget) 66 { 67 struct xenvif *vif = container_of(napi, struct xenvif, napi); 68 int work_done; 69 70 work_done = xenvif_tx_action(vif, budget); 71 72 if (work_done < budget) { 73 int more_to_do = 0; 74 unsigned long flags; 75 76 /* It is necessary to disable IRQ before calling 77 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might 78 * lose event from the frontend. 79 * 80 * Consider: 81 * RING_HAS_UNCONSUMED_REQUESTS 82 * <frontend generates event to trigger napi_schedule> 83 * __napi_complete 84 * 85 * This handler is still in scheduled state so the 86 * event has no effect at all. After __napi_complete 87 * this handler is descheduled and cannot get 88 * scheduled again. We lose event in this case and the ring 89 * will be completely stalled. 90 */ 91 92 local_irq_save(flags); 93 94 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do); 95 if (!more_to_do) 96 __napi_complete(napi); 97 98 local_irq_restore(flags); 99 } 100 101 return work_done; 102 } 103 104 static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) 105 { 106 struct xenvif *vif = dev_id; 107 108 if (xenvif_rx_schedulable(vif)) 109 netif_wake_queue(vif->dev); 110 111 return IRQ_HANDLED; 112 } 113 114 static irqreturn_t xenvif_interrupt(int irq, void *dev_id) 115 { 116 xenvif_tx_interrupt(irq, dev_id); 117 xenvif_rx_interrupt(irq, dev_id); 118 119 return IRQ_HANDLED; 120 } 121 122 static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev) 123 { 124 struct xenvif *vif = netdev_priv(dev); 125 126 BUG_ON(skb->dev != dev); 127 128 /* Drop the packet if vif is not ready */ 129 if (vif->task == NULL) 130 goto drop; 131 132 /* Drop the packet if the target domain has no receive buffers. */ 133 if (!xenvif_rx_schedulable(vif)) 134 goto drop; 135 136 /* Reserve ring slots for the worst-case number of fragments. */ 137 vif->rx_req_cons_peek += xenvif_count_skb_slots(vif, skb); 138 139 if (vif->can_queue && xenvif_must_stop_queue(vif)) 140 netif_stop_queue(dev); 141 142 xenvif_queue_tx_skb(vif, skb); 143 144 return NETDEV_TX_OK; 145 146 drop: 147 vif->dev->stats.tx_dropped++; 148 dev_kfree_skb(skb); 149 return NETDEV_TX_OK; 150 } 151 152 void xenvif_notify_tx_completion(struct xenvif *vif) 153 { 154 if (netif_queue_stopped(vif->dev) && xenvif_rx_schedulable(vif)) 155 netif_wake_queue(vif->dev); 156 } 157 158 static struct net_device_stats *xenvif_get_stats(struct net_device *dev) 159 { 160 struct xenvif *vif = netdev_priv(dev); 161 return &vif->dev->stats; 162 } 163 164 static void xenvif_up(struct xenvif *vif) 165 { 166 napi_enable(&vif->napi); 167 enable_irq(vif->tx_irq); 168 if (vif->tx_irq != vif->rx_irq) 169 enable_irq(vif->rx_irq); 170 xenvif_check_rx_xenvif(vif); 171 } 172 173 static void xenvif_down(struct xenvif *vif) 174 { 175 napi_disable(&vif->napi); 176 disable_irq(vif->tx_irq); 177 if (vif->tx_irq != vif->rx_irq) 178 disable_irq(vif->rx_irq); 179 del_timer_sync(&vif->credit_timeout); 180 } 181 182 static int xenvif_open(struct net_device *dev) 183 { 184 struct xenvif *vif = netdev_priv(dev); 185 if (netif_carrier_ok(dev)) 186 xenvif_up(vif); 187 netif_start_queue(dev); 188 return 0; 189 } 190 191 static int xenvif_close(struct net_device *dev) 192 { 193 struct xenvif *vif = netdev_priv(dev); 194 if (netif_carrier_ok(dev)) 195 xenvif_down(vif); 196 netif_stop_queue(dev); 197 return 0; 198 } 199 200 static int xenvif_change_mtu(struct net_device *dev, int mtu) 201 { 202 struct xenvif *vif = netdev_priv(dev); 203 int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN; 204 205 if (mtu > max) 206 return -EINVAL; 207 dev->mtu = mtu; 208 return 0; 209 } 210 211 static netdev_features_t xenvif_fix_features(struct net_device *dev, 212 netdev_features_t features) 213 { 214 struct xenvif *vif = netdev_priv(dev); 215 216 if (!vif->can_sg) 217 features &= ~NETIF_F_SG; 218 if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4)) 219 features &= ~NETIF_F_TSO; 220 if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6)) 221 features &= ~NETIF_F_TSO6; 222 if (!vif->ip_csum) 223 features &= ~NETIF_F_IP_CSUM; 224 if (!vif->ipv6_csum) 225 features &= ~NETIF_F_IPV6_CSUM; 226 227 return features; 228 } 229 230 static const struct xenvif_stat { 231 char name[ETH_GSTRING_LEN]; 232 u16 offset; 233 } xenvif_stats[] = { 234 { 235 "rx_gso_checksum_fixup", 236 offsetof(struct xenvif, rx_gso_checksum_fixup) 237 }, 238 }; 239 240 static int xenvif_get_sset_count(struct net_device *dev, int string_set) 241 { 242 switch (string_set) { 243 case ETH_SS_STATS: 244 return ARRAY_SIZE(xenvif_stats); 245 default: 246 return -EINVAL; 247 } 248 } 249 250 static void xenvif_get_ethtool_stats(struct net_device *dev, 251 struct ethtool_stats *stats, u64 * data) 252 { 253 void *vif = netdev_priv(dev); 254 int i; 255 256 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) 257 data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset); 258 } 259 260 static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data) 261 { 262 int i; 263 264 switch (stringset) { 265 case ETH_SS_STATS: 266 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) 267 memcpy(data + i * ETH_GSTRING_LEN, 268 xenvif_stats[i].name, ETH_GSTRING_LEN); 269 break; 270 } 271 } 272 273 static const struct ethtool_ops xenvif_ethtool_ops = { 274 .get_link = ethtool_op_get_link, 275 276 .get_sset_count = xenvif_get_sset_count, 277 .get_ethtool_stats = xenvif_get_ethtool_stats, 278 .get_strings = xenvif_get_strings, 279 }; 280 281 static const struct net_device_ops xenvif_netdev_ops = { 282 .ndo_start_xmit = xenvif_start_xmit, 283 .ndo_get_stats = xenvif_get_stats, 284 .ndo_open = xenvif_open, 285 .ndo_stop = xenvif_close, 286 .ndo_change_mtu = xenvif_change_mtu, 287 .ndo_fix_features = xenvif_fix_features, 288 .ndo_set_mac_address = eth_mac_addr, 289 .ndo_validate_addr = eth_validate_addr, 290 }; 291 292 struct xenvif *xenvif_alloc(struct device *parent, domid_t domid, 293 unsigned int handle) 294 { 295 int err; 296 struct net_device *dev; 297 struct xenvif *vif; 298 char name[IFNAMSIZ] = {}; 299 int i; 300 301 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); 302 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup); 303 if (dev == NULL) { 304 pr_warn("Could not allocate netdev for %s\n", name); 305 return ERR_PTR(-ENOMEM); 306 } 307 308 SET_NETDEV_DEV(dev, parent); 309 310 vif = netdev_priv(dev); 311 312 vif->grant_copy_op = vmalloc(sizeof(struct gnttab_copy) * 313 MAX_GRANT_COPY_OPS); 314 if (vif->grant_copy_op == NULL) { 315 pr_warn("Could not allocate grant copy space for %s\n", name); 316 free_netdev(dev); 317 return ERR_PTR(-ENOMEM); 318 } 319 320 vif->domid = domid; 321 vif->handle = handle; 322 vif->can_sg = 1; 323 vif->ip_csum = 1; 324 vif->dev = dev; 325 326 vif->credit_bytes = vif->remaining_credit = ~0UL; 327 vif->credit_usec = 0UL; 328 init_timer(&vif->credit_timeout); 329 vif->credit_window_start = get_jiffies_64(); 330 331 dev->netdev_ops = &xenvif_netdev_ops; 332 dev->hw_features = NETIF_F_SG | 333 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 334 NETIF_F_TSO | NETIF_F_TSO6; 335 dev->features = dev->hw_features | NETIF_F_RXCSUM; 336 SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops); 337 338 dev->tx_queue_len = XENVIF_QUEUE_LENGTH; 339 340 skb_queue_head_init(&vif->rx_queue); 341 skb_queue_head_init(&vif->tx_queue); 342 343 vif->pending_cons = 0; 344 vif->pending_prod = MAX_PENDING_REQS; 345 for (i = 0; i < MAX_PENDING_REQS; i++) 346 vif->pending_ring[i] = i; 347 for (i = 0; i < MAX_PENDING_REQS; i++) 348 vif->mmap_pages[i] = NULL; 349 350 /* 351 * Initialise a dummy MAC address. We choose the numerically 352 * largest non-broadcast address to prevent the address getting 353 * stolen by an Ethernet bridge for STP purposes. 354 * (FE:FF:FF:FF:FF:FF) 355 */ 356 memset(dev->dev_addr, 0xFF, ETH_ALEN); 357 dev->dev_addr[0] &= ~0x01; 358 359 netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT); 360 361 netif_carrier_off(dev); 362 363 err = register_netdev(dev); 364 if (err) { 365 netdev_warn(dev, "Could not register device: err=%d\n", err); 366 free_netdev(dev); 367 return ERR_PTR(err); 368 } 369 370 netdev_dbg(dev, "Successfully created xenvif\n"); 371 372 __module_get(THIS_MODULE); 373 374 return vif; 375 } 376 377 int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref, 378 unsigned long rx_ring_ref, unsigned int tx_evtchn, 379 unsigned int rx_evtchn) 380 { 381 struct task_struct *task; 382 int err = -ENOMEM; 383 384 BUG_ON(vif->tx_irq); 385 BUG_ON(vif->task); 386 387 err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref); 388 if (err < 0) 389 goto err; 390 391 if (tx_evtchn == rx_evtchn) { 392 /* feature-split-event-channels == 0 */ 393 err = bind_interdomain_evtchn_to_irqhandler( 394 vif->domid, tx_evtchn, xenvif_interrupt, 0, 395 vif->dev->name, vif); 396 if (err < 0) 397 goto err_unmap; 398 vif->tx_irq = vif->rx_irq = err; 399 disable_irq(vif->tx_irq); 400 } else { 401 /* feature-split-event-channels == 1 */ 402 snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name), 403 "%s-tx", vif->dev->name); 404 err = bind_interdomain_evtchn_to_irqhandler( 405 vif->domid, tx_evtchn, xenvif_tx_interrupt, 0, 406 vif->tx_irq_name, vif); 407 if (err < 0) 408 goto err_unmap; 409 vif->tx_irq = err; 410 disable_irq(vif->tx_irq); 411 412 snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name), 413 "%s-rx", vif->dev->name); 414 err = bind_interdomain_evtchn_to_irqhandler( 415 vif->domid, rx_evtchn, xenvif_rx_interrupt, 0, 416 vif->rx_irq_name, vif); 417 if (err < 0) 418 goto err_tx_unbind; 419 vif->rx_irq = err; 420 disable_irq(vif->rx_irq); 421 } 422 423 init_waitqueue_head(&vif->wq); 424 task = kthread_create(xenvif_kthread, 425 (void *)vif, "%s", vif->dev->name); 426 if (IS_ERR(task)) { 427 pr_warn("Could not allocate kthread for %s\n", vif->dev->name); 428 err = PTR_ERR(task); 429 goto err_rx_unbind; 430 } 431 432 vif->task = task; 433 434 rtnl_lock(); 435 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) 436 dev_set_mtu(vif->dev, ETH_DATA_LEN); 437 netdev_update_features(vif->dev); 438 netif_carrier_on(vif->dev); 439 if (netif_running(vif->dev)) 440 xenvif_up(vif); 441 rtnl_unlock(); 442 443 wake_up_process(vif->task); 444 445 return 0; 446 447 err_rx_unbind: 448 unbind_from_irqhandler(vif->rx_irq, vif); 449 vif->rx_irq = 0; 450 err_tx_unbind: 451 unbind_from_irqhandler(vif->tx_irq, vif); 452 vif->tx_irq = 0; 453 err_unmap: 454 xenvif_unmap_frontend_rings(vif); 455 err: 456 module_put(THIS_MODULE); 457 return err; 458 } 459 460 void xenvif_carrier_off(struct xenvif *vif) 461 { 462 struct net_device *dev = vif->dev; 463 464 rtnl_lock(); 465 netif_carrier_off(dev); /* discard queued packets */ 466 if (netif_running(dev)) 467 xenvif_down(vif); 468 rtnl_unlock(); 469 } 470 471 void xenvif_disconnect(struct xenvif *vif) 472 { 473 if (netif_carrier_ok(vif->dev)) 474 xenvif_carrier_off(vif); 475 476 if (vif->task) { 477 kthread_stop(vif->task); 478 vif->task = NULL; 479 } 480 481 if (vif->tx_irq) { 482 if (vif->tx_irq == vif->rx_irq) 483 unbind_from_irqhandler(vif->tx_irq, vif); 484 else { 485 unbind_from_irqhandler(vif->tx_irq, vif); 486 unbind_from_irqhandler(vif->rx_irq, vif); 487 } 488 vif->tx_irq = 0; 489 } 490 491 xenvif_unmap_frontend_rings(vif); 492 } 493 494 void xenvif_free(struct xenvif *vif) 495 { 496 netif_napi_del(&vif->napi); 497 498 unregister_netdev(vif->dev); 499 500 vfree(vif->grant_copy_op); 501 free_netdev(vif->dev); 502 503 module_put(THIS_MODULE); 504 } 505