aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/xen-netback/interface.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/xen-netback/interface.c')
-rw-r--r--drivers/net/xen-netback/interface.c496
1 files changed, 311 insertions, 185 deletions
diff --git a/drivers/net/xen-netback/interface.c b/drivers/net/xen-netback/interface.c
index 20e9defa1060..9e97c7ca0ddd 100644
--- a/drivers/net/xen-netback/interface.c
+++ b/drivers/net/xen-netback/interface.c
@@ -43,6 +43,16 @@
43#define XENVIF_QUEUE_LENGTH 32 43#define XENVIF_QUEUE_LENGTH 32
44#define XENVIF_NAPI_WEIGHT 64 44#define XENVIF_NAPI_WEIGHT 64
45 45
46static inline void xenvif_stop_queue(struct xenvif_queue *queue)
47{
48 struct net_device *dev = queue->vif->dev;
49
50 if (!queue->vif->can_queue)
51 return;
52
53 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
54}
55
46int xenvif_schedulable(struct xenvif *vif) 56int xenvif_schedulable(struct xenvif *vif)
47{ 57{
48 return netif_running(vif->dev) && netif_carrier_ok(vif->dev); 58 return netif_running(vif->dev) && netif_carrier_ok(vif->dev);
@@ -50,33 +60,34 @@ int xenvif_schedulable(struct xenvif *vif)
50 60
51static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id) 61static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
52{ 62{
53 struct xenvif *vif = dev_id; 63 struct xenvif_queue *queue = dev_id;
54 64
55 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) 65 if (RING_HAS_UNCONSUMED_REQUESTS(&queue->tx))
56 napi_schedule(&vif->napi); 66 napi_schedule(&queue->napi);
57 67
58 return IRQ_HANDLED; 68 return IRQ_HANDLED;
59} 69}
60 70
61static int xenvif_poll(struct napi_struct *napi, int budget) 71int xenvif_poll(struct napi_struct *napi, int budget)
62{ 72{
63 struct xenvif *vif = container_of(napi, struct xenvif, napi); 73 struct xenvif_queue *queue =
74 container_of(napi, struct xenvif_queue, napi);
64 int work_done; 75 int work_done;
65 76
66 /* This vif is rogue, we pretend we've there is nothing to do 77 /* This vif is rogue, we pretend we've there is nothing to do
67 * for this vif to deschedule it from NAPI. But this interface 78 * for this vif to deschedule it from NAPI. But this interface
68 * will be turned off in thread context later. 79 * will be turned off in thread context later.
69 */ 80 */
70 if (unlikely(vif->disabled)) { 81 if (unlikely(queue->vif->disabled)) {
71 napi_complete(napi); 82 napi_complete(napi);
72 return 0; 83 return 0;
73 } 84 }
74 85
75 work_done = xenvif_tx_action(vif, budget); 86 work_done = xenvif_tx_action(queue, budget);
76 87
77 if (work_done < budget) { 88 if (work_done < budget) {
78 napi_complete(napi); 89 napi_complete(napi);
79 xenvif_napi_schedule_or_enable_events(vif); 90 xenvif_napi_schedule_or_enable_events(queue);
80 } 91 }
81 92
82 return work_done; 93 return work_done;
@@ -84,9 +95,9 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
84 95
85static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id) 96static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
86{ 97{
87 struct xenvif *vif = dev_id; 98 struct xenvif_queue *queue = dev_id;
88 99
89 xenvif_kick_thread(vif); 100 xenvif_kick_thread(queue);
90 101
91 return IRQ_HANDLED; 102 return IRQ_HANDLED;
92} 103}
@@ -99,28 +110,59 @@ static irqreturn_t xenvif_interrupt(int irq, void *dev_id)
99 return IRQ_HANDLED; 110 return IRQ_HANDLED;
100} 111}
101 112
102static void xenvif_wake_queue(unsigned long data) 113int xenvif_queue_stopped(struct xenvif_queue *queue)
114{
115 struct net_device *dev = queue->vif->dev;
116 unsigned int id = queue->id;
117 return netif_tx_queue_stopped(netdev_get_tx_queue(dev, id));
118}
119
120void xenvif_wake_queue(struct xenvif_queue *queue)
121{
122 struct net_device *dev = queue->vif->dev;
123 unsigned int id = queue->id;
124 netif_tx_wake_queue(netdev_get_tx_queue(dev, id));
125}
126
127/* Callback to wake the queue and drain it on timeout */
128static void xenvif_wake_queue_callback(unsigned long data)
103{ 129{
104 struct xenvif *vif = (struct xenvif *)data; 130 struct xenvif_queue *queue = (struct xenvif_queue *)data;
105 131
106 if (netif_queue_stopped(vif->dev)) { 132 if (xenvif_queue_stopped(queue)) {
107 netdev_err(vif->dev, "draining TX queue\n"); 133 netdev_err(queue->vif->dev, "draining TX queue\n");
108 vif->rx_queue_purge = true; 134 queue->rx_queue_purge = true;
109 xenvif_kick_thread(vif); 135 xenvif_kick_thread(queue);
110 netif_wake_queue(vif->dev); 136 xenvif_wake_queue(queue);
111 } 137 }
112} 138}
113 139
114static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev) 140static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
115{ 141{
116 struct xenvif *vif = netdev_priv(dev); 142 struct xenvif *vif = netdev_priv(dev);
143 struct xenvif_queue *queue = NULL;
144 unsigned int num_queues = vif->num_queues;
145 u16 index;
117 int min_slots_needed; 146 int min_slots_needed;
118 147
119 BUG_ON(skb->dev != dev); 148 BUG_ON(skb->dev != dev);
120 149
121 /* Drop the packet if vif is not ready */ 150 /* Drop the packet if queues are not set up */
122 if (vif->task == NULL || 151 if (num_queues < 1)
123 vif->dealloc_task == NULL || 152 goto drop;
153
154 /* Obtain the queue to be used to transmit this packet */
155 index = skb_get_queue_mapping(skb);
156 if (index >= num_queues) {
157 pr_warn_ratelimited("Invalid queue %hu for packet on interface %s\n.",
158 index, vif->dev->name);
159 index %= num_queues;
160 }
161 queue = &vif->queues[index];
162
163 /* Drop the packet if queue is not ready */
164 if (queue->task == NULL ||
165 queue->dealloc_task == NULL ||
124 !xenvif_schedulable(vif)) 166 !xenvif_schedulable(vif))
125 goto drop; 167 goto drop;
126 168
@@ -139,16 +181,16 @@ static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
139 * then turn off the queue to give the ring a chance to 181 * then turn off the queue to give the ring a chance to
140 * drain. 182 * drain.
141 */ 183 */
142 if (!xenvif_rx_ring_slots_available(vif, min_slots_needed)) { 184 if (!xenvif_rx_ring_slots_available(queue, min_slots_needed)) {
143 vif->wake_queue.function = xenvif_wake_queue; 185 queue->wake_queue.function = xenvif_wake_queue_callback;
144 vif->wake_queue.data = (unsigned long)vif; 186 queue->wake_queue.data = (unsigned long)queue;
145 xenvif_stop_queue(vif); 187 xenvif_stop_queue(queue);
146 mod_timer(&vif->wake_queue, 188 mod_timer(&queue->wake_queue,
147 jiffies + rx_drain_timeout_jiffies); 189 jiffies + rx_drain_timeout_jiffies);
148 } 190 }
149 191
150 skb_queue_tail(&vif->rx_queue, skb); 192 skb_queue_tail(&queue->rx_queue, skb);
151 xenvif_kick_thread(vif); 193 xenvif_kick_thread(queue);
152 194
153 return NETDEV_TX_OK; 195 return NETDEV_TX_OK;
154 196
@@ -161,25 +203,65 @@ static int xenvif_start_xmit(struct sk_buff *skb, struct net_device *dev)
161static struct net_device_stats *xenvif_get_stats(struct net_device *dev) 203static struct net_device_stats *xenvif_get_stats(struct net_device *dev)
162{ 204{
163 struct xenvif *vif = netdev_priv(dev); 205 struct xenvif *vif = netdev_priv(dev);
206 struct xenvif_queue *queue = NULL;
207 unsigned int num_queues = vif->num_queues;
208 unsigned long rx_bytes = 0;
209 unsigned long rx_packets = 0;
210 unsigned long tx_bytes = 0;
211 unsigned long tx_packets = 0;
212 unsigned int index;
213
214 if (vif->queues == NULL)
215 goto out;
216
217 /* Aggregate tx and rx stats from each queue */
218 for (index = 0; index < num_queues; ++index) {
219 queue = &vif->queues[index];
220 rx_bytes += queue->stats.rx_bytes;
221 rx_packets += queue->stats.rx_packets;
222 tx_bytes += queue->stats.tx_bytes;
223 tx_packets += queue->stats.tx_packets;
224 }
225
226out:
227 vif->dev->stats.rx_bytes = rx_bytes;
228 vif->dev->stats.rx_packets = rx_packets;
229 vif->dev->stats.tx_bytes = tx_bytes;
230 vif->dev->stats.tx_packets = tx_packets;
231
164 return &vif->dev->stats; 232 return &vif->dev->stats;
165} 233}
166 234
167static void xenvif_up(struct xenvif *vif) 235static void xenvif_up(struct xenvif *vif)
168{ 236{
169 napi_enable(&vif->napi); 237 struct xenvif_queue *queue = NULL;
170 enable_irq(vif->tx_irq); 238 unsigned int num_queues = vif->num_queues;
171 if (vif->tx_irq != vif->rx_irq) 239 unsigned int queue_index;
172 enable_irq(vif->rx_irq); 240
173 xenvif_napi_schedule_or_enable_events(vif); 241 for (queue_index = 0; queue_index < num_queues; ++queue_index) {
242 queue = &vif->queues[queue_index];
243 napi_enable(&queue->napi);
244 enable_irq(queue->tx_irq);
245 if (queue->tx_irq != queue->rx_irq)
246 enable_irq(queue->rx_irq);
247 xenvif_napi_schedule_or_enable_events(queue);
248 }
174} 249}
175 250
176static void xenvif_down(struct xenvif *vif) 251static void xenvif_down(struct xenvif *vif)
177{ 252{
178 napi_disable(&vif->napi); 253 struct xenvif_queue *queue = NULL;
179 disable_irq(vif->tx_irq); 254 unsigned int num_queues = vif->num_queues;
180 if (vif->tx_irq != vif->rx_irq) 255 unsigned int queue_index;
181 disable_irq(vif->rx_irq); 256
182 del_timer_sync(&vif->credit_timeout); 257 for (queue_index = 0; queue_index < num_queues; ++queue_index) {
258 queue = &vif->queues[queue_index];
259 napi_disable(&queue->napi);
260 disable_irq(queue->tx_irq);
261 if (queue->tx_irq != queue->rx_irq)
262 disable_irq(queue->rx_irq);
263 del_timer_sync(&queue->credit_timeout);
264 }
183} 265}
184 266
185static int xenvif_open(struct net_device *dev) 267static int xenvif_open(struct net_device *dev)
@@ -187,7 +269,7 @@ static int xenvif_open(struct net_device *dev)
187 struct xenvif *vif = netdev_priv(dev); 269 struct xenvif *vif = netdev_priv(dev);
188 if (netif_carrier_ok(dev)) 270 if (netif_carrier_ok(dev))
189 xenvif_up(vif); 271 xenvif_up(vif);
190 netif_start_queue(dev); 272 netif_tx_start_all_queues(dev);
191 return 0; 273 return 0;
192} 274}
193 275
@@ -196,7 +278,7 @@ static int xenvif_close(struct net_device *dev)
196 struct xenvif *vif = netdev_priv(dev); 278 struct xenvif *vif = netdev_priv(dev);
197 if (netif_carrier_ok(dev)) 279 if (netif_carrier_ok(dev))
198 xenvif_down(vif); 280 xenvif_down(vif);
199 netif_stop_queue(dev); 281 netif_tx_stop_all_queues(dev);
200 return 0; 282 return 0;
201} 283}
202 284
@@ -236,29 +318,29 @@ static const struct xenvif_stat {
236} xenvif_stats[] = { 318} xenvif_stats[] = {
237 { 319 {
238 "rx_gso_checksum_fixup", 320 "rx_gso_checksum_fixup",
239 offsetof(struct xenvif, rx_gso_checksum_fixup) 321 offsetof(struct xenvif_stats, rx_gso_checksum_fixup)
240 }, 322 },
241 /* If (sent != success + fail), there are probably packets never 323 /* If (sent != success + fail), there are probably packets never
242 * freed up properly! 324 * freed up properly!
243 */ 325 */
244 { 326 {
245 "tx_zerocopy_sent", 327 "tx_zerocopy_sent",
246 offsetof(struct xenvif, tx_zerocopy_sent), 328 offsetof(struct xenvif_stats, tx_zerocopy_sent),
247 }, 329 },
248 { 330 {
249 "tx_zerocopy_success", 331 "tx_zerocopy_success",
250 offsetof(struct xenvif, tx_zerocopy_success), 332 offsetof(struct xenvif_stats, tx_zerocopy_success),
251 }, 333 },
252 { 334 {
253 "tx_zerocopy_fail", 335 "tx_zerocopy_fail",
254 offsetof(struct xenvif, tx_zerocopy_fail) 336 offsetof(struct xenvif_stats, tx_zerocopy_fail)
255 }, 337 },
256 /* Number of packets exceeding MAX_SKB_FRAG slots. You should use 338 /* Number of packets exceeding MAX_SKB_FRAG slots. You should use
257 * a guest with the same MAX_SKB_FRAG 339 * a guest with the same MAX_SKB_FRAG
258 */ 340 */
259 { 341 {
260 "tx_frag_overflow", 342 "tx_frag_overflow",
261 offsetof(struct xenvif, tx_frag_overflow) 343 offsetof(struct xenvif_stats, tx_frag_overflow)
262 }, 344 },
263}; 345};
264 346
@@ -275,11 +357,20 @@ static int xenvif_get_sset_count(struct net_device *dev, int string_set)
275static void xenvif_get_ethtool_stats(struct net_device *dev, 357static void xenvif_get_ethtool_stats(struct net_device *dev,
276 struct ethtool_stats *stats, u64 * data) 358 struct ethtool_stats *stats, u64 * data)
277{ 359{
278 void *vif = netdev_priv(dev); 360 struct xenvif *vif = netdev_priv(dev);
361 unsigned int num_queues = vif->num_queues;
279 int i; 362 int i;
280 363 unsigned int queue_index;
281 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) 364 struct xenvif_stats *vif_stats;
282 data[i] = *(unsigned long *)(vif + xenvif_stats[i].offset); 365
366 for (i = 0; i < ARRAY_SIZE(xenvif_stats); i++) {
367 unsigned long accum = 0;
368 for (queue_index = 0; queue_index < num_queues; ++queue_index) {
369 vif_stats = &vif->queues[queue_index].stats;
370 accum += *(unsigned long *)(vif_stats + xenvif_stats[i].offset);
371 }
372 data[i] = accum;
373 }
283} 374}
284 375
285static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data) 376static void xenvif_get_strings(struct net_device *dev, u32 stringset, u8 * data)
@@ -321,10 +412,14 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
321 struct net_device *dev; 412 struct net_device *dev;
322 struct xenvif *vif; 413 struct xenvif *vif;
323 char name[IFNAMSIZ] = {}; 414 char name[IFNAMSIZ] = {};
324 int i;
325 415
326 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle); 416 snprintf(name, IFNAMSIZ - 1, "vif%u.%u", domid, handle);
327 dev = alloc_netdev(sizeof(struct xenvif), name, ether_setup); 417 /* Allocate a netdev with the max. supported number of queues.
418 * When the guest selects the desired number, it will be updated
419 * via netif_set_real_num_*_queues().
420 */
421 dev = alloc_netdev_mq(sizeof(struct xenvif), name, ether_setup,
422 xenvif_max_queues);
328 if (dev == NULL) { 423 if (dev == NULL) {
329 pr_warn("Could not allocate netdev for %s\n", name); 424 pr_warn("Could not allocate netdev for %s\n", name);
330 return ERR_PTR(-ENOMEM); 425 return ERR_PTR(-ENOMEM);
@@ -334,66 +429,26 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
334 429
335 vif = netdev_priv(dev); 430 vif = netdev_priv(dev);
336 431
337 vif->grant_copy_op = vmalloc(sizeof(struct gnttab_copy) *
338 MAX_GRANT_COPY_OPS);
339 if (vif->grant_copy_op == NULL) {
340 pr_warn("Could not allocate grant copy space for %s\n", name);
341 free_netdev(dev);
342 return ERR_PTR(-ENOMEM);
343 }
344
345 vif->domid = domid; 432 vif->domid = domid;
346 vif->handle = handle; 433 vif->handle = handle;
347 vif->can_sg = 1; 434 vif->can_sg = 1;
348 vif->ip_csum = 1; 435 vif->ip_csum = 1;
349 vif->dev = dev; 436 vif->dev = dev;
350
351 vif->disabled = false; 437 vif->disabled = false;
352 438
353 vif->credit_bytes = vif->remaining_credit = ~0UL; 439 /* Start out with no queues. */
354 vif->credit_usec = 0UL; 440 vif->queues = NULL;
355 init_timer(&vif->credit_timeout); 441 vif->num_queues = 0;
356 vif->credit_window_start = get_jiffies_64();
357
358 init_timer(&vif->wake_queue);
359 442
360 dev->netdev_ops = &xenvif_netdev_ops; 443 dev->netdev_ops = &xenvif_netdev_ops;
361 dev->hw_features = NETIF_F_SG | 444 dev->hw_features = NETIF_F_SG |
362 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 445 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
363 NETIF_F_TSO | NETIF_F_TSO6; 446 NETIF_F_TSO | NETIF_F_TSO6;
364 dev->features = dev->hw_features | NETIF_F_RXCSUM; 447 dev->features = dev->hw_features | NETIF_F_RXCSUM;
365 SET_ETHTOOL_OPS(dev, &xenvif_ethtool_ops); 448 dev->ethtool_ops = &xenvif_ethtool_ops;
366 449
367 dev->tx_queue_len = XENVIF_QUEUE_LENGTH; 450 dev->tx_queue_len = XENVIF_QUEUE_LENGTH;
368 451
369 skb_queue_head_init(&vif->rx_queue);
370 skb_queue_head_init(&vif->tx_queue);
371
372 vif->pending_cons = 0;
373 vif->pending_prod = MAX_PENDING_REQS;
374 for (i = 0; i < MAX_PENDING_REQS; i++)
375 vif->pending_ring[i] = i;
376 spin_lock_init(&vif->callback_lock);
377 spin_lock_init(&vif->response_lock);
378 /* If ballooning is disabled, this will consume real memory, so you
379 * better enable it. The long term solution would be to use just a
380 * bunch of valid page descriptors, without dependency on ballooning
381 */
382 err = alloc_xenballooned_pages(MAX_PENDING_REQS,
383 vif->mmap_pages,
384 false);
385 if (err) {
386 netdev_err(dev, "Could not reserve mmap_pages\n");
387 return ERR_PTR(-ENOMEM);
388 }
389 for (i = 0; i < MAX_PENDING_REQS; i++) {
390 vif->pending_tx_info[i].callback_struct = (struct ubuf_info)
391 { .callback = xenvif_zerocopy_callback,
392 .ctx = NULL,
393 .desc = i };
394 vif->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
395 }
396
397 /* 452 /*
398 * Initialise a dummy MAC address. We choose the numerically 453 * Initialise a dummy MAC address. We choose the numerically
399 * largest non-broadcast address to prevent the address getting 454 * largest non-broadcast address to prevent the address getting
@@ -403,8 +458,6 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
403 memset(dev->dev_addr, 0xFF, ETH_ALEN); 458 memset(dev->dev_addr, 0xFF, ETH_ALEN);
404 dev->dev_addr[0] &= ~0x01; 459 dev->dev_addr[0] &= ~0x01;
405 460
406 netif_napi_add(dev, &vif->napi, xenvif_poll, XENVIF_NAPI_WEIGHT);
407
408 netif_carrier_off(dev); 461 netif_carrier_off(dev);
409 462
410 err = register_netdev(dev); 463 err = register_netdev(dev);
@@ -421,98 +474,147 @@ struct xenvif *xenvif_alloc(struct device *parent, domid_t domid,
421 return vif; 474 return vif;
422} 475}
423 476
424int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref, 477int 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_window_start = get_jiffies_64();
485
486 skb_queue_head_init(&queue->rx_queue);
487 skb_queue_head_init(&queue->tx_queue);
488
489 queue->pending_cons = 0;
490 queue->pending_prod = MAX_PENDING_REQS;
491 for (i = 0; i < MAX_PENDING_REQS; ++i)
492 queue->pending_ring[i] = i;
493
494 spin_lock_init(&queue->callback_lock);
495 spin_lock_init(&queue->response_lock);
496
497 /* If ballooning is disabled, this will consume real memory, so you
498 * better enable it. The long term solution would be to use just a
499 * bunch of valid page descriptors, without dependency on ballooning
500 */
501 err = alloc_xenballooned_pages(MAX_PENDING_REQS,
502 queue->mmap_pages,
503 false);
504 if (err) {
505 netdev_err(queue->vif->dev, "Could not reserve mmap_pages\n");
506 return -ENOMEM;
507 }
508
509 for (i = 0; i < MAX_PENDING_REQS; i++) {
510 queue->pending_tx_info[i].callback_struct = (struct ubuf_info)
511 { .callback = xenvif_zerocopy_callback,
512 .ctx = NULL,
513 .desc = i };
514 queue->grant_tx_handle[i] = NETBACK_INVALID_HANDLE;
515 }
516
517 init_timer(&queue->wake_queue);
518
519 netif_napi_add(queue->vif->dev, &queue->napi, xenvif_poll,
520 XENVIF_NAPI_WEIGHT);
521
522 return 0;
523}
524
525void xenvif_carrier_on(struct xenvif *vif)
526{
527 rtnl_lock();
528 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
529 dev_set_mtu(vif->dev, ETH_DATA_LEN);
530 netdev_update_features(vif->dev);
531 netif_carrier_on(vif->dev);
532 if (netif_running(vif->dev))
533 xenvif_up(vif);
534 rtnl_unlock();
535}
536
537int xenvif_connect(struct xenvif_queue *queue, unsigned long tx_ring_ref,
425 unsigned long rx_ring_ref, unsigned int tx_evtchn, 538 unsigned long rx_ring_ref, unsigned int tx_evtchn,
426 unsigned int rx_evtchn) 539 unsigned int rx_evtchn)
427{ 540{
428 struct task_struct *task; 541 struct task_struct *task;
429 int err = -ENOMEM; 542 int err = -ENOMEM;
430 543
431 BUG_ON(vif->tx_irq); 544 BUG_ON(queue->tx_irq);
432 BUG_ON(vif->task); 545 BUG_ON(queue->task);
433 BUG_ON(vif->dealloc_task); 546 BUG_ON(queue->dealloc_task);
434 547
435 err = xenvif_map_frontend_rings(vif, tx_ring_ref, rx_ring_ref); 548 err = xenvif_map_frontend_rings(queue, tx_ring_ref, rx_ring_ref);
436 if (err < 0) 549 if (err < 0)
437 goto err; 550 goto err;
438 551
439 init_waitqueue_head(&vif->wq); 552 init_waitqueue_head(&queue->wq);
440 init_waitqueue_head(&vif->dealloc_wq); 553 init_waitqueue_head(&queue->dealloc_wq);
441 554
442 if (tx_evtchn == rx_evtchn) { 555 if (tx_evtchn == rx_evtchn) {
443 /* feature-split-event-channels == 0 */ 556 /* feature-split-event-channels == 0 */
444 err = bind_interdomain_evtchn_to_irqhandler( 557 err = bind_interdomain_evtchn_to_irqhandler(
445 vif->domid, tx_evtchn, xenvif_interrupt, 0, 558 queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
446 vif->dev->name, vif); 559 queue->name, queue);
447 if (err < 0) 560 if (err < 0)
448 goto err_unmap; 561 goto err_unmap;
449 vif->tx_irq = vif->rx_irq = err; 562 queue->tx_irq = queue->rx_irq = err;
450 disable_irq(vif->tx_irq); 563 disable_irq(queue->tx_irq);
451 } else { 564 } else {
452 /* feature-split-event-channels == 1 */ 565 /* feature-split-event-channels == 1 */
453 snprintf(vif->tx_irq_name, sizeof(vif->tx_irq_name), 566 snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
454 "%s-tx", vif->dev->name); 567 "%s-tx", queue->name);
455 err = bind_interdomain_evtchn_to_irqhandler( 568 err = bind_interdomain_evtchn_to_irqhandler(
456 vif->domid, tx_evtchn, xenvif_tx_interrupt, 0, 569 queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
457 vif->tx_irq_name, vif); 570 queue->tx_irq_name, queue);
458 if (err < 0) 571 if (err < 0)
459 goto err_unmap; 572 goto err_unmap;
460 vif->tx_irq = err; 573 queue->tx_irq = err;
461 disable_irq(vif->tx_irq); 574 disable_irq(queue->tx_irq);
462 575
463 snprintf(vif->rx_irq_name, sizeof(vif->rx_irq_name), 576 snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
464 "%s-rx", vif->dev->name); 577 "%s-rx", queue->name);
465 err = bind_interdomain_evtchn_to_irqhandler( 578 err = bind_interdomain_evtchn_to_irqhandler(
466 vif->domid, rx_evtchn, xenvif_rx_interrupt, 0, 579 queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
467 vif->rx_irq_name, vif); 580 queue->rx_irq_name, queue);
468 if (err < 0) 581 if (err < 0)
469 goto err_tx_unbind; 582 goto err_tx_unbind;
470 vif->rx_irq = err; 583 queue->rx_irq = err;
471 disable_irq(vif->rx_irq); 584 disable_irq(queue->rx_irq);
472 } 585 }
473 586
474 task = kthread_create(xenvif_kthread_guest_rx, 587 task = kthread_create(xenvif_kthread_guest_rx,
475 (void *)vif, "%s-guest-rx", vif->dev->name); 588 (void *)queue, "%s-guest-rx", queue->name);
476 if (IS_ERR(task)) { 589 if (IS_ERR(task)) {
477 pr_warn("Could not allocate kthread for %s\n", vif->dev->name); 590 pr_warn("Could not allocate kthread for %s\n", queue->name);
478 err = PTR_ERR(task); 591 err = PTR_ERR(task);
479 goto err_rx_unbind; 592 goto err_rx_unbind;
480 } 593 }
481 594 queue->task = task;
482 vif->task = task;
483 595
484 task = kthread_create(xenvif_dealloc_kthread, 596 task = kthread_create(xenvif_dealloc_kthread,
485 (void *)vif, "%s-dealloc", vif->dev->name); 597 (void *)queue, "%s-dealloc", queue->name);
486 if (IS_ERR(task)) { 598 if (IS_ERR(task)) {
487 pr_warn("Could not allocate kthread for %s\n", vif->dev->name); 599 pr_warn("Could not allocate kthread for %s\n", queue->name);
488 err = PTR_ERR(task); 600 err = PTR_ERR(task);
489 goto err_rx_unbind; 601 goto err_rx_unbind;
490 } 602 }
603 queue->dealloc_task = task;
491 604
492 vif->dealloc_task = task; 605 wake_up_process(queue->task);
493 606 wake_up_process(queue->dealloc_task);
494 rtnl_lock();
495 if (!vif->can_sg && vif->dev->mtu > ETH_DATA_LEN)
496 dev_set_mtu(vif->dev, ETH_DATA_LEN);
497 netdev_update_features(vif->dev);
498 netif_carrier_on(vif->dev);
499 if (netif_running(vif->dev))
500 xenvif_up(vif);
501 rtnl_unlock();
502
503 wake_up_process(vif->task);
504 wake_up_process(vif->dealloc_task);
505 607
506 return 0; 608 return 0;
507 609
508err_rx_unbind: 610err_rx_unbind:
509 unbind_from_irqhandler(vif->rx_irq, vif); 611 unbind_from_irqhandler(queue->rx_irq, queue);
510 vif->rx_irq = 0; 612 queue->rx_irq = 0;
511err_tx_unbind: 613err_tx_unbind:
512 unbind_from_irqhandler(vif->tx_irq, vif); 614 unbind_from_irqhandler(queue->tx_irq, queue);
513 vif->tx_irq = 0; 615 queue->tx_irq = 0;
514err_unmap: 616err_unmap:
515 xenvif_unmap_frontend_rings(vif); 617 xenvif_unmap_frontend_rings(queue);
516err: 618err:
517 module_put(THIS_MODULE); 619 module_put(THIS_MODULE);
518 return err; 620 return err;
@@ -529,38 +631,77 @@ void xenvif_carrier_off(struct xenvif *vif)
529 rtnl_unlock(); 631 rtnl_unlock();
530} 632}
531 633
634static void xenvif_wait_unmap_timeout(struct xenvif_queue *queue,
635 unsigned int worst_case_skb_lifetime)
636{
637 int i, unmap_timeout = 0;
638
639 for (i = 0; i < MAX_PENDING_REQS; ++i) {
640 if (queue->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
641 unmap_timeout++;
642 schedule_timeout(msecs_to_jiffies(1000));
643 if (unmap_timeout > worst_case_skb_lifetime &&
644 net_ratelimit())
645 netdev_err(queue->vif->dev,
646 "Page still granted! Index: %x\n",
647 i);
648 i = -1;
649 }
650 }
651}
652
532void xenvif_disconnect(struct xenvif *vif) 653void xenvif_disconnect(struct xenvif *vif)
533{ 654{
655 struct xenvif_queue *queue = NULL;
656 unsigned int num_queues = vif->num_queues;
657 unsigned int queue_index;
658
534 if (netif_carrier_ok(vif->dev)) 659 if (netif_carrier_ok(vif->dev))
535 xenvif_carrier_off(vif); 660 xenvif_carrier_off(vif);
536 661
537 if (vif->task) { 662 for (queue_index = 0; queue_index < num_queues; ++queue_index) {
538 del_timer_sync(&vif->wake_queue); 663 queue = &vif->queues[queue_index];
539 kthread_stop(vif->task);
540 vif->task = NULL;
541 }
542 664
543 if (vif->dealloc_task) { 665 if (queue->task) {
544 kthread_stop(vif->dealloc_task); 666 del_timer_sync(&queue->wake_queue);
545 vif->dealloc_task = NULL; 667 kthread_stop(queue->task);
546 } 668 queue->task = NULL;
669 }
547 670
548 if (vif->tx_irq) { 671 if (queue->dealloc_task) {
549 if (vif->tx_irq == vif->rx_irq) 672 kthread_stop(queue->dealloc_task);
550 unbind_from_irqhandler(vif->tx_irq, vif); 673 queue->dealloc_task = NULL;
551 else {
552 unbind_from_irqhandler(vif->tx_irq, vif);
553 unbind_from_irqhandler(vif->rx_irq, vif);
554 } 674 }
555 vif->tx_irq = 0; 675
676 if (queue->tx_irq) {
677 if (queue->tx_irq == queue->rx_irq)
678 unbind_from_irqhandler(queue->tx_irq, queue);
679 else {
680 unbind_from_irqhandler(queue->tx_irq, queue);
681 unbind_from_irqhandler(queue->rx_irq, queue);
682 }
683 queue->tx_irq = 0;
684 }
685
686 xenvif_unmap_frontend_rings(queue);
556 } 687 }
688}
557 689
558 xenvif_unmap_frontend_rings(vif); 690/* Reverse the relevant parts of xenvif_init_queue().
691 * Used for queue teardown from xenvif_free(), and on the
692 * error handling paths in xenbus.c:connect().
693 */
694void xenvif_deinit_queue(struct xenvif_queue *queue)
695{
696 free_xenballooned_pages(MAX_PENDING_REQS, queue->mmap_pages);
697 netif_napi_del(&queue->napi);
559} 698}
560 699
561void xenvif_free(struct xenvif *vif) 700void xenvif_free(struct xenvif *vif)
562{ 701{
563 int i, unmap_timeout = 0; 702 struct xenvif_queue *queue = NULL;
703 unsigned int num_queues = vif->num_queues;
704 unsigned int queue_index;
564 /* Here we want to avoid timeout messages if an skb can be legitimately 705 /* Here we want to avoid timeout messages if an skb can be legitimately
565 * stuck somewhere else. Realistically this could be an another vif's 706 * stuck somewhere else. Realistically this could be an another vif's
566 * internal or QDisc queue. That another vif also has this 707 * internal or QDisc queue. That another vif also has this
@@ -575,33 +716,18 @@ void xenvif_free(struct xenvif *vif)
575 unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000) * 716 unsigned int worst_case_skb_lifetime = (rx_drain_timeout_msecs/1000) *
576 DIV_ROUND_UP(XENVIF_QUEUE_LENGTH, (XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS)); 717 DIV_ROUND_UP(XENVIF_QUEUE_LENGTH, (XEN_NETIF_RX_RING_SIZE / MAX_SKB_FRAGS));
577 718
578 for (i = 0; i < MAX_PENDING_REQS; ++i) { 719 unregister_netdev(vif->dev);
579 if (vif->grant_tx_handle[i] != NETBACK_INVALID_HANDLE) {
580 unmap_timeout++;
581 schedule_timeout(msecs_to_jiffies(1000));
582 if (unmap_timeout > worst_case_skb_lifetime &&
583 net_ratelimit())
584 netdev_err(vif->dev,
585 "Page still granted! Index: %x\n",
586 i);
587 /* If there are still unmapped pages, reset the loop to
588 * start checking again. We shouldn't exit here until
589 * dealloc thread and NAPI instance release all the
590 * pages. If a kernel bug causes the skbs to stall
591 * somewhere, the interface cannot be brought down
592 * properly.
593 */
594 i = -1;
595 }
596 }
597
598 free_xenballooned_pages(MAX_PENDING_REQS, vif->mmap_pages);
599 720
600 netif_napi_del(&vif->napi); 721 for (queue_index = 0; queue_index < num_queues; ++queue_index) {
722 queue = &vif->queues[queue_index];
723 xenvif_wait_unmap_timeout(queue, worst_case_skb_lifetime);
724 xenvif_deinit_queue(queue);
725 }
601 726
602 unregister_netdev(vif->dev); 727 vfree(vif->queues);
728 vif->queues = NULL;
729 vif->num_queues = 0;
603 730
604 vfree(vif->grant_copy_op);
605 free_netdev(vif->dev); 731 free_netdev(vif->dev);
606 732
607 module_put(THIS_MODULE); 733 module_put(THIS_MODULE);