Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 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 | #include <xen/balloon.h> |
| 42 | |
| 43 | #define XENVIF_QUEUE_LENGTH 32 |
| 44 | #define XENVIF_NAPI_WEIGHT 64 |
| 45 | |
| 46 | /* Number of bytes allowed on the internal guest Rx queue. */ |
| 47 | #define XENVIF_RX_QUEUE_BYTES (XEN_NETIF_RX_RING_SIZE/2 * PAGE_SIZE) |
| 48 | |
| 49 | /* This function is used to set SKBTX_DEV_ZEROCOPY as well as |
| 50 | * increasing the inflight counter. We need to increase the inflight |
| 51 | * counter because core driver calls into xenvif_zerocopy_callback |
| 52 | * which calls xenvif_skb_zerocopy_complete. |
| 53 | */ |
| 54 | void xenvif_skb_zerocopy_prepare(struct xenvif_queue *queue, |
| 55 | struct sk_buff *skb) |
| 56 | { |
| 57 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
| 58 | atomic_inc(&queue->inflight_packets); |
| 59 | } |
| 60 | |
| 61 | void xenvif_skb_zerocopy_complete(struct xenvif_queue *queue) |
| 62 | { |
| 63 | atomic_dec(&queue->inflight_packets); |
| 64 | |
| 65 | /* Wake the dealloc thread _after_ decrementing inflight_packets so |
| 66 | * that if kthread_stop() has already been called, the dealloc thread |
| 67 | * does not wait forever with nothing to wake it. |
| 68 | */ |
| 69 | wake_up(&queue->dealloc_wq); |
| 70 | } |
| 71 | |
| 72 | int xenvif_schedulable(struct xenvif *vif) |
| 73 | { |
| 74 | return netif_running(vif->dev) && |
| 75 | test_bit(VIF_STATUS_CONNECTED, &vif->status) && |
| 76 | !vif->disabled; |
| 77 | } |
| 78 | |
| 79 | static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) |
| 80 | { |
| 81 | struct xenvif_queue *queue = dev_id; |
| 82 | |
| 83 | if (RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)) |
| 84 | napi_schedule(&queue->napi); |
| 85 | |
| 86 | return IRQ_HANDLED; |
| 87 | } |
| 88 | |
| 89 | static int xenvif_poll(struct napi_struct *napi, int budget) |
| 90 | { |
| 91 | struct xenvif_queue *queue = |
| 92 | container_of(napi, struct xenvif_queue, napi); |
| 93 | int work_done; |
| 94 | |
| 95 | /* This vif is rogue, we pretend we've there is nothing to do |
| 96 | * for this vif to deschedule it from NAPI. But this interface |
| 97 | * will be turned off in thread context later. |
| 98 | */ |
| 99 | if (unlikely(queue->vif->disabled)) { |
| 100 | napi_complete(napi); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | work_done = xenvif_tx_action(queue, budget); |
| 105 | |
| 106 | if (work_done < budget) { |
| 107 | napi_complete(napi); |
| 108 | /* If the queue is rate-limited, it shall be |
| 109 | * rescheduled in the timer callback. |
| 110 | */ |
| 111 | if (likely(!queue->rate_limited)) |
| 112 | xenvif_napi_schedule_or_enable_events(queue); |
| 113 | } |
| 114 | |
| 115 | return work_done; |
| 116 | } |
| 117 | |
| 118 | static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) |
| 119 | { |
| 120 | struct xenvif_queue *queue = dev_id; |
| 121 | |
| 122 | xenvif_kick_thread(queue); |
| 123 | |
| 124 | return IRQ_HANDLED; |
| 125 | } |
| 126 | |
| 127 | irqreturn_t xenvif_interrupt(int irq, void *dev_id) |
| 128 | { |
| 129 | xenvif_tx_interrupt(irq, dev_id); |
| 130 | xenvif_rx_interrupt(irq, dev_id); |
| 131 | |
| 132 | return IRQ_HANDLED; |
| 133 | } |
| 134 | |
| 135 | int xenvif_queue_stopped(struct xenvif_queue *queue) |
| 136 | { |
| 137 | struct net_device *dev = queue->vif->dev; |
| 138 | unsigned int id = queue->id; |
| 139 | return netif_tx_queue_stopped(netdev_get_tx_queue(dev, id)); |
| 140 | } |
| 141 | |
| 142 | void xenvif_wake_queue(struct xenvif_queue *queue) |
| 143 | { |
| 144 | struct net_device *dev = queue->vif->dev; |
| 145 | unsigned int id = queue->id; |
| 146 | netif_tx_wake_queue(netdev_get_tx_queue(dev, id)); |
| 147 | } |
| 148 | |
| 149 | static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 150 | { |
| 151 | struct xenvif *vif = netdev_priv(dev); |
| 152 | struct xenvif_queue *queue = NULL; |
| 153 | unsigned int num_queues = vif->num_queues; |
| 154 | u16 index; |
| 155 | struct xenvif_rx_cb *cb; |
| 156 | |
| 157 | BUG_ON(skb->dev != dev); |
| 158 | |
| 159 | /* Drop the packet if queues are not set up */ |
| 160 | if (num_queues < 1) |
| 161 | goto drop; |
| 162 | |
| 163 | /* Obtain the queue to be used to transmit this packet */ |
| 164 | index = skb_get_queue_mapping(skb); |
| 165 | if (index >= num_queues) { |
| 166 | pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n.", |
| 167 | index, vif->dev->name); |
| 168 | index %= num_queues; |
| 169 | } |
| 170 | queue = &vif->queues[index]; |
| 171 | |
| 172 | /* Drop the packet if queue is not ready */ |
| 173 | if (queue->task == NULL || |
| 174 | queue->dealloc_task == NULL || |
| 175 | !xenvif_schedulable(vif)) |
| 176 | goto drop; |
| 177 | |
| 178 | if (vif->multicast_control && skb->pkt_type == PACKET_MULTICAST) { |
| 179 | struct ethhdr *eth = (struct ethhdr *)skb->data; |
| 180 | |
| 181 | if (!xenvif_mcast_match(vif, eth->h_dest)) |
| 182 | goto drop; |
| 183 | } |
| 184 | |
| 185 | cb = XENVIF_RX_CB(skb); |
| 186 | cb->expires = jiffies + vif->drain_timeout; |
| 187 | |
| 188 | xenvif_rx_queue_tail(queue, skb); |
| 189 | xenvif_kick_thread(queue); |
| 190 | |
| 191 | return NETDEV_TX_OK; |
| 192 | |
| 193 | drop: |
| 194 | vif->dev->stats.tx_dropped++; |
| 195 | dev_kfree_skb(skb); |
| 196 | return NETDEV_TX_OK; |
| 197 | } |
| 198 | |
| 199 | static struct net_device_stats *xenvif_get_stats(struct net_device *dev) |
| 200 | { |
| 201 | struct xenvif *vif = netdev_priv(dev); |
| 202 | struct xenvif_queue *queue = NULL; |
| 203 | unsigned int num_queues = vif->num_queues; |
| 204 | unsigned long rx_bytes = 0; |
| 205 | unsigned long rx_packets = 0; |
| 206 | unsigned long tx_bytes = 0; |
| 207 | unsigned long tx_packets = 0; |
| 208 | unsigned int index; |
| 209 | |
| 210 | if (vif->queues == NULL) |
| 211 | goto out; |
| 212 | |
| 213 | /* Aggregate tx and rx stats from each queue */ |
| 214 | for (index = 0; index < num_queues; ++index) { |
| 215 | queue = &vif->queues[index]; |
| 216 | rx_bytes += queue->stats.rx_bytes; |
| 217 | rx_packets += queue->stats.rx_packets; |
| 218 | tx_bytes += queue->stats.tx_bytes; |
| 219 | tx_packets += queue->stats.tx_packets; |
| 220 | } |
| 221 | |
| 222 | out: |
| 223 | vif->dev->stats.rx_bytes = rx_bytes; |
| 224 | vif->dev->stats.rx_packets = rx_packets; |
| 225 | vif->dev->stats.tx_bytes = tx_bytes; |
| 226 | vif->dev->stats.tx_packets = tx_packets; |
| 227 | |
| 228 | return &vif->dev->stats; |
| 229 | } |
| 230 | |
| 231 | static void xenvif_up(struct xenvif *vif) |
| 232 | { |
| 233 | struct xenvif_queue *queue = NULL; |
| 234 | unsigned int num_queues = vif->num_queues; |
| 235 | unsigned int queue_index; |
| 236 | |
| 237 | for (queue_index = 0; queue_index < num_queues; ++queue_index) { |
| 238 | queue = &vif->queues[queue_index]; |
| 239 | napi_enable(&queue->napi); |
| 240 | enable_irq(queue->tx_irq); |
| 241 | if (queue->tx_irq != queue->rx_irq) |
| 242 | enable_irq(queue->rx_irq); |
| 243 | xenvif_napi_schedule_or_enable_events(queue); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static void xenvif_down(struct xenvif *vif) |
| 248 | { |
| 249 | struct xenvif_queue *queue = NULL; |
| 250 | unsigned int num_queues = vif->num_queues; |
| 251 | unsigned int queue_index; |
| 252 | |
| 253 | for (queue_index = 0; queue_index < num_queues; ++queue_index) { |
| 254 | queue = &vif->queues[queue_index]; |
| 255 | disable_irq(queue->tx_irq); |
| 256 | if (queue->tx_irq != queue->rx_irq) |
| 257 | disable_irq(queue->rx_irq); |
| 258 | napi_disable(&queue->napi); |
| 259 | del_timer_sync(&queue->credit_timeout); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static int xenvif_open(struct net_device *dev) |
| 264 | { |
| 265 | struct xenvif *vif = netdev_priv(dev); |
| 266 | if (test_bit(VIF_STATUS_CONNECTED, &vif->status)) |
| 267 | xenvif_up(vif); |
| 268 | netif_tx_start_all_queues(dev); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int xenvif_close(struct net_device *dev) |
| 273 | { |
| 274 | struct xenvif *vif = netdev_priv(dev); |
| 275 | if (test_bit(VIF_STATUS_CONNECTED, &vif->status)) |
| 276 | xenvif_down(vif); |
| 277 | netif_tx_stop_all_queues(dev); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int xenvif_change_mtu(struct net_device *dev, int mtu) |
| 282 | { |
| 283 | struct xenvif *vif = netdev_priv(dev); |
| 284 | int max = vif->can_sg ? 65535 - VLAN_ETH_HLEN : ETH_DATA_LEN; |
| 285 | |
| 286 | if (mtu > max) |
| 287 | return -EINVAL; |
| 288 | dev->mtu = mtu; |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static netdev_features_t xenvif_fix_features(struct net_device *dev, |
| 293 | netdev_features_t features) |
| 294 | { |
| 295 | struct xenvif *vif = netdev_priv(dev); |
| 296 | |
| 297 | if (!vif->can_sg) |
| 298 | features &= ~NETIF_F_SG; |
| 299 | if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV4)) |
| 300 | features &= ~NETIF_F_TSO; |
| 301 | if (~(vif->gso_mask | vif->gso_prefix_mask) & GSO_BIT(TCPV6)) |
| 302 | features &= ~NETIF_F_TSO6; |
| 303 | if (!vif->ip_csum) |
| 304 | features &= ~NETIF_F_IP_CSUM; |
| 305 | if (!vif->ipv6_csum) |
| 306 | features &= ~NETIF_F_IPV6_CSUM; |
| 307 | |
| 308 | return features; |
| 309 | } |
| 310 | |
| 311 | static const struct xenvif_stat { |
| 312 | char name[ETH_GSTRING_LEN]; |
| 313 | u16 offset; |
| 314 | } xenvif_stats[] = { |
| 315 | { |
| 316 | "rx_gso_checksum_fixup", |
| 317 | offsetof(struct xenvif_stats, rx_gso_checksum_fixup) |
| 318 | }, |
| 319 | /* If (sent != success + fail), there are probably packets never |
| 320 | * freed up properly! |
| 321 | */ |
| 322 | { |
| 323 | "tx_zerocopy_sent", |
| 324 | offsetof(struct xenvif_stats, tx_zerocopy_sent), |
| 325 | }, |
| 326 | { |
| 327 | "tx_zerocopy_success", |
| 328 | offsetof(struct xenvif_stats, tx_zerocopy_success), |
| 329 | }, |
| 330 | { |
| 331 | "tx_zerocopy_fail", |
| 332 | offsetof(struct xenvif_stats, tx_zerocopy_fail) |
| 333 | }, |
| 334 | /* Number of packets exceeding MAX_SKB_FRAG slots. You should use |
| 335 | * a guest with the same MAX_SKB_FRAG |
| 336 | */ |
| 337 | { |
| 338 | "tx_frag_overflow", |
| 339 | offsetof(struct xenvif_stats, tx_frag_overflow) |
| 340 | }, |
| 341 | }; |
| 342 | |
| 343 | static int xenvif_get_sset_count(struct net_device *dev, int string_set) |
| 344 | { |
| 345 | switch (string_set) { |
| 346 | case ETH_SS_STATS: |
| 347 | return ARRAY_SIZE(xenvif_stats); |
| 348 | default: |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static void xenvif_get_ethtool_stats(struct net_device *dev, |
| 354 | struct ethtool_stats *stats, u64 * data) |
| 355 | { |
| 356 | struct xenvif *vif = netdev_priv(dev); |
| 357 | unsigned int num_queues = vif->num_queues; |
| 358 | int i; |
| 359 | unsigned int queue_index; |
| 360 | |
| 361 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) { |
| 362 | unsigned long accum = 0; |
| 363 | for (queue_index = 0; queue_index < num_queues; ++queue_index) { |
| 364 | void *vif_stats = &vif->queues[queue_index].stats; |
| 365 | accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset); |
| 366 | } |
| 367 | data[i] = accum; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data) |
| 372 | { |
| 373 | int i; |
| 374 | |
| 375 | switch (stringset) { |
| 376 | case ETH_SS_STATS: |
| 377 | for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) |
| 378 | memcpy(data + i * ETH_GSTRING_LEN, |
| 379 | xenvif_stats[i].name, ETH_GSTRING_LEN); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static const struct ethtool_ops xenvif_ethtool_ops = { |
| 385 | .get_link = ethtool_op_get_link, |
| 386 | |
| 387 | .get_sset_count = xenvif_get_sset_count, |
| 388 | .get_ethtool_stats = xenvif_get_ethtool_stats, |
| 389 | .get_strings = xenvif_get_strings, |
| 390 | }; |
| 391 | |
| 392 | static const struct net_device_ops xenvif_netdev_ops = { |
| 393 | .ndo_start_xmit = xenvif_start_xmit, |
| 394 | .ndo_get_stats = xenvif_get_stats, |
| 395 | .ndo_open = xenvif_open, |
| 396 | .ndo_stop = xenvif_close, |
| 397 | .ndo_change_mtu = xenvif_change_mtu, |
| 398 | .ndo_fix_features = xenvif_fix_features, |
| 399 | .ndo_set_mac_address = eth_mac_addr, |
| 400 | .ndo_validate_addr = eth_validate_addr, |
| 401 | }; |
| 402 | |
| 403 | struct xenvif *xenvif_alloc(struct device *parent, domid_t domid, |
| 404 | unsigned int handle) |
| 405 | { |
| 406 | int err; |
| 407 | struct net_device *dev; |
| 408 | struct xenvif *vif; |
| 409 | char name[IFNAMSIZ] = {}; |
| 410 | |
| 411 | snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); |
| 412 | /* Allocate a netdev with the max. supported number of queues. |
| 413 | * When the guest selects the desired number, it will be updated |
| 414 | * via netif_set_real_num_*_queues(). |
| 415 | */ |
| 416 | dev = alloc_netdev_mq(sizeof(struct xenvif), name, NET_NAME_UNKNOWN, |
| 417 | ether_setup, xenvif_max_queues); |
| 418 | if (dev == NULL) { |
| 419 | pr_warn("Could not allocate netdev for %s\n", name); |
| 420 | return ERR_PTR(-ENOMEM); |
| 421 | } |
| 422 | |
| 423 | SET_NETDEV_DEV(dev, parent); |
| 424 | |
| 425 | vif = netdev_priv(dev); |
| 426 | |
| 427 | vif->domid = domid; |
| 428 | vif->handle = handle; |
| 429 | vif->can_sg = 1; |
| 430 | vif->ip_csum = 1; |
| 431 | vif->dev = dev; |
| 432 | vif->disabled = false; |
| 433 | vif->drain_timeout = msecs_to_jiffies(rx_drain_timeout_msecs); |
| 434 | vif->stall_timeout = msecs_to_jiffies(rx_stall_timeout_msecs); |
| 435 | |
| 436 | /* Start out with no queues. */ |
| 437 | vif->queues = NULL; |
| 438 | vif->num_queues = 0; |
| 439 | |
| 440 | spin_lock_init(&vif->lock); |
| 441 | INIT_LIST_HEAD(&vif->fe_mcast_addr); |
| 442 | |
| 443 | dev->netdev_ops = &xenvif_netdev_ops; |
| 444 | dev->hw_features = NETIF_F_SG | |
| 445 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | |
| 446 | NETIF_F_TSO | NETIF_F_TSO6; |
| 447 | dev->features = dev->hw_features | NETIF_F_RXCSUM; |
| 448 | dev->ethtool_ops = &xenvif_ethtool_ops; |
| 449 | |
| 450 | dev->tx_queue_len = XENVIF_QUEUE_LENGTH; |
| 451 | |
| 452 | /* |
| 453 | * Initialise a dummy MAC address. We choose the numerically |
| 454 | * largest non-broadcast address to prevent the address getting |
| 455 | * stolen by an Ethernet bridge for STP purposes. |
| 456 | * (FE:FF:FF:FF:FF:FF) |
| 457 | */ |
| 458 | eth_broadcast_addr(dev->dev_addr); |
| 459 | dev->dev_addr[0] &= ~0x01; |
| 460 | |
| 461 | netif_carrier_off(dev); |
| 462 | |
| 463 | err = register_netdev(dev); |
| 464 | if (err) { |
| 465 | netdev_warn(dev, "Could not register device: err=%d\n", err); |
| 466 | free_netdev(dev); |
| 467 | return ERR_PTR(err); |
| 468 | } |
| 469 | |
| 470 | netdev_dbg(dev, "Successfully created xenvif\n"); |
| 471 | |
| 472 | __module_get(THIS_MODULE); |
| 473 | |
| 474 | return vif; |
| 475 | } |
| 476 | |
| 477 | int xenvif_init_queue(struct xenvif_queue *queue) |
| 478 | { |
| 479 | int err, i; |
| 480 | |
| 481 | queue->credit_bytes = queue->remaining_credit = ~0UL; |
| 482 | queue->credit_usec = 0UL; |
| 483 | init_timer(&queue->credit_timeout); |
| 484 | queue->credit_timeout.function = xenvif_tx_credit_callback; |
| 485 | queue->credit_window_start = get_jiffies_64(); |
| 486 | |
| 487 | queue->rx_queue_max = XENVIF_RX_QUEUE_BYTES; |
| 488 | |
| 489 | skb_queue_head_init(&queue->rx_queue); |
| 490 | skb_queue_head_init(&queue->tx_queue); |
| 491 | |
| 492 | queue->pending_cons = 0; |
| 493 | queue->pending_prod = MAX_PENDING_REQS; |
| 494 | for (i = 0; i < MAX_PENDING_REQS; ++i) |
| 495 | queue->pending_ring[i] = i; |
| 496 | |
| 497 | spin_lock_init(&queue->callback_lock); |
| 498 | spin_lock_init(&queue->response_lock); |
| 499 | |
| 500 | /* If ballooning is disabled, this will consume real memory, so you |
| 501 | * better enable it. The long term solution would be to use just a |
| 502 | * bunch of valid page descriptors, without dependency on ballooning |
| 503 | */ |
| 504 | err = gnttab_alloc_pages(MAX_PENDING_REQS, |
| 505 | queue->mmap_pages); |
| 506 | if (err) { |
| 507 | netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n"); |
| 508 | return -ENOMEM; |
| 509 | } |
| 510 | |
| 511 | for (i = 0; i < MAX_PENDING_REQS; i++) { |
| 512 | queue->pending_tx_info[i].callback_struct = (struct ubuf_info) |
| 513 | { .callback = xenvif_zerocopy_callback, |
| 514 | .ctx = NULL, |
| 515 | .desc = i }; |
| 516 | queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE; |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | void xenvif_carrier_on(struct xenvif *vif) |
| 523 | { |
| 524 | rtnl_lock(); |
| 525 | if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN) |
| 526 | dev_set_mtu(vif->dev, ETH_DATA_LEN); |
| 527 | netdev_update_features(vif->dev); |
| 528 | set_bit(VIF_STATUS_CONNECTED, &vif->status); |
| 529 | if (netif_running(vif->dev)) |
| 530 | xenvif_up(vif); |
| 531 | rtnl_unlock(); |
| 532 | } |
| 533 | |
| 534 | int xenvif_connect(struct xenvif_queue *queue, unsigned long tx_ring_ref, |
| 535 | unsigned long rx_ring_ref, unsigned int tx_evtchn, |
| 536 | unsigned int rx_evtchn) |
| 537 | { |
| 538 | struct task_struct *task; |
| 539 | int err = -ENOMEM; |
| 540 | |
| 541 | BUG_ON(queue->tx_irq); |
| 542 | BUG_ON(queue->task); |
| 543 | BUG_ON(queue->dealloc_task); |
| 544 | |
| 545 | err = xenvif_map_frontend_rings(queue, tx_ring_ref, rx_ring_ref); |
| 546 | if (err < 0) |
| 547 | goto err; |
| 548 | |
| 549 | init_waitqueue_head(&queue->wq); |
| 550 | init_waitqueue_head(&queue->dealloc_wq); |
| 551 | atomic_set(&queue->inflight_packets, 0); |
| 552 | |
| 553 | netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll, |
| 554 | XENVIF_NAPI_WEIGHT); |
| 555 | |
| 556 | if (tx_evtchn == rx_evtchn) { |
| 557 | /* feature-split-event-channels == 0 */ |
| 558 | err = bind_interdomain_evtchn_to_irqhandler( |
| 559 | queue->vif->domid, tx_evtchn, xenvif_interrupt, 0, |
| 560 | queue->name, queue); |
| 561 | if (err < 0) |
| 562 | goto err_unmap; |
| 563 | queue->tx_irq = queue->rx_irq = err; |
| 564 | disable_irq(queue->tx_irq); |
| 565 | } else { |
| 566 | /* feature-split-event-channels == 1 */ |
| 567 | snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name), |
| 568 | "%s-tx", queue->name); |
| 569 | err = bind_interdomain_evtchn_to_irqhandler( |
| 570 | queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0, |
| 571 | queue->tx_irq_name, queue); |
| 572 | if (err < 0) |
| 573 | goto err_unmap; |
| 574 | queue->tx_irq = err; |
| 575 | disable_irq(queue->tx_irq); |
| 576 | |
| 577 | snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name), |
| 578 | "%s-rx", queue->name); |
| 579 | err = bind_interdomain_evtchn_to_irqhandler( |
| 580 | queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0, |
| 581 | queue->rx_irq_name, queue); |
| 582 | if (err < 0) |
| 583 | goto err_tx_unbind; |
| 584 | queue->rx_irq = err; |
| 585 | disable_irq(queue->rx_irq); |
| 586 | } |
| 587 | |
| 588 | queue->stalled = true; |
| 589 | |
| 590 | task = kthread_create(xenvif_kthread_guest_rx, |
| 591 | (void *)queue, "%s-guest-rx", queue->name); |
| 592 | if (IS_ERR(task)) { |
| 593 | pr_warn("Could not allocate kthread for %s\n", queue->name); |
| 594 | err = PTR_ERR(task); |
| 595 | goto err_rx_unbind; |
| 596 | } |
| 597 | queue->task = task; |
| 598 | get_task_struct(task); |
| 599 | |
| 600 | task = kthread_create(xenvif_dealloc_kthread, |
| 601 | (void *)queue, "%s-dealloc", queue->name); |
| 602 | if (IS_ERR(task)) { |
| 603 | pr_warn("Could not allocate kthread for %s\n", queue->name); |
| 604 | err = PTR_ERR(task); |
| 605 | goto err_rx_unbind; |
| 606 | } |
| 607 | queue->dealloc_task = task; |
| 608 | |
| 609 | wake_up_process(queue->task); |
| 610 | wake_up_process(queue->dealloc_task); |
| 611 | |
| 612 | return 0; |
| 613 | |
| 614 | err_rx_unbind: |
| 615 | unbind_from_irqhandler(queue->rx_irq, queue); |
| 616 | queue->rx_irq = 0; |
| 617 | err_tx_unbind: |
| 618 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 619 | queue->tx_irq = 0; |
| 620 | err_unmap: |
| 621 | xenvif_unmap_frontend_rings(queue); |
| 622 | err: |
| 623 | module_put(THIS_MODULE); |
| 624 | return err; |
| 625 | } |
| 626 | |
| 627 | void xenvif_carrier_off(struct xenvif *vif) |
| 628 | { |
| 629 | struct net_device *dev = vif->dev; |
| 630 | |
| 631 | rtnl_lock(); |
| 632 | if (test_and_clear_bit(VIF_STATUS_CONNECTED, &vif->status)) { |
| 633 | netif_carrier_off(dev); /* discard queued packets */ |
| 634 | if (netif_running(dev)) |
| 635 | xenvif_down(vif); |
| 636 | } |
| 637 | rtnl_unlock(); |
| 638 | } |
| 639 | |
| 640 | void xenvif_disconnect(struct xenvif *vif) |
| 641 | { |
| 642 | struct xenvif_queue *queue = NULL; |
| 643 | unsigned int num_queues = vif->num_queues; |
| 644 | unsigned int queue_index; |
| 645 | |
| 646 | xenvif_carrier_off(vif); |
| 647 | |
| 648 | for (queue_index = 0; queue_index < num_queues; ++queue_index) { |
| 649 | queue = &vif->queues[queue_index]; |
| 650 | |
| 651 | netif_napi_del(&queue->napi); |
| 652 | |
| 653 | if (queue->task) { |
| 654 | kthread_stop(queue->task); |
| 655 | put_task_struct(queue->task); |
| 656 | queue->task = NULL; |
| 657 | } |
| 658 | |
| 659 | if (queue->dealloc_task) { |
| 660 | kthread_stop(queue->dealloc_task); |
| 661 | queue->dealloc_task = NULL; |
| 662 | } |
| 663 | |
| 664 | if (queue->tx_irq) { |
| 665 | if (queue->tx_irq == queue->rx_irq) |
| 666 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 667 | else { |
| 668 | unbind_from_irqhandler(queue->tx_irq, queue); |
| 669 | unbind_from_irqhandler(queue->rx_irq, queue); |
| 670 | } |
| 671 | queue->tx_irq = 0; |
| 672 | } |
| 673 | |
| 674 | xenvif_unmap_frontend_rings(queue); |
| 675 | } |
| 676 | |
| 677 | xenvif_mcast_addr_list_free(vif); |
| 678 | } |
| 679 | |
| 680 | /* Reverse the relevant parts of xenvif_init_queue(). |
| 681 | * Used for queue teardown from xenvif_free(), and on the |
| 682 | * error handling paths in xenbus.c:connect(). |
| 683 | */ |
| 684 | void xenvif_deinit_queue(struct xenvif_queue *queue) |
| 685 | { |
| 686 | gnttab_free_pages(MAX_PENDING_REQS, queue->mmap_pages); |
| 687 | } |
| 688 | |
| 689 | void xenvif_free(struct xenvif *vif) |
| 690 | { |
| 691 | struct xenvif_queue *queue = NULL; |
| 692 | unsigned int num_queues = vif->num_queues; |
| 693 | unsigned int queue_index; |
| 694 | |
| 695 | unregister_netdev(vif->dev); |
| 696 | |
| 697 | for (queue_index = 0; queue_index < num_queues; ++queue_index) { |
| 698 | queue = &vif->queues[queue_index]; |
| 699 | xenvif_deinit_queue(queue); |
| 700 | } |
| 701 | |
| 702 | vfree(vif->queues); |
| 703 | vif->queues = NULL; |
| 704 | vif->num_queues = 0; |
| 705 | |
| 706 | free_netdev(vif->dev); |
| 707 | |
| 708 | module_put(THIS_MODULE); |
| 709 | } |