rx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Copyright (c) 2016 Citrix Systems Inc.
  3. * Copyright (c) 2002-2005, K A Fraser
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation; or, when distributed
  8. * separately from the Linux kernel or incorporated into other
  9. * software packages, subject to the following license:
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this source file (the "Software"), to deal in the Software without
  13. * restriction, including without limitation the rights to use, copy, modify,
  14. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  15. * and to permit persons to whom the Software is furnished to do so, subject to
  16. * the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  27. * IN THE SOFTWARE.
  28. */
  29. #include "common.h"
  30. #include <linux/kthread.h>
  31. #include <xen/xen.h>
  32. #include <xen/events.h>
  33. static bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue)
  34. {
  35. RING_IDX prod, cons;
  36. struct sk_buff *skb;
  37. int needed;
  38. skb = skb_peek(&queue->rx_queue);
  39. if (!skb)
  40. return false;
  41. needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE);
  42. if (skb_is_gso(skb))
  43. needed++;
  44. if (skb->sw_hash)
  45. needed++;
  46. do {
  47. prod = queue->rx.sring->req_prod;
  48. cons = queue->rx.req_cons;
  49. if (prod - cons >= needed)
  50. return true;
  51. queue->rx.sring->req_event = prod + 1;
  52. /* Make sure event is visible before we check prod
  53. * again.
  54. */
  55. mb();
  56. } while (queue->rx.sring->req_prod != prod);
  57. return false;
  58. }
  59. void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(&queue->rx_queue.lock, flags);
  63. __skb_queue_tail(&queue->rx_queue, skb);
  64. queue->rx_queue_len += skb->len;
  65. if (queue->rx_queue_len > queue->rx_queue_max) {
  66. struct net_device *dev = queue->vif->dev;
  67. netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
  68. }
  69. spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
  70. }
  71. static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
  72. {
  73. struct sk_buff *skb;
  74. spin_lock_irq(&queue->rx_queue.lock);
  75. skb = __skb_dequeue(&queue->rx_queue);
  76. if (skb)
  77. queue->rx_queue_len -= skb->len;
  78. spin_unlock_irq(&queue->rx_queue.lock);
  79. return skb;
  80. }
  81. static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
  82. {
  83. spin_lock_irq(&queue->rx_queue.lock);
  84. if (queue->rx_queue_len < queue->rx_queue_max) {
  85. struct net_device *dev = queue->vif->dev;
  86. netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
  87. }
  88. spin_unlock_irq(&queue->rx_queue.lock);
  89. }
  90. static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
  91. {
  92. struct sk_buff *skb;
  93. while ((skb = xenvif_rx_dequeue(queue)) != NULL)
  94. kfree_skb(skb);
  95. }
  96. static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
  97. {
  98. struct sk_buff *skb;
  99. for (;;) {
  100. skb = skb_peek(&queue->rx_queue);
  101. if (!skb)
  102. break;
  103. if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
  104. break;
  105. xenvif_rx_dequeue(queue);
  106. kfree_skb(skb);
  107. }
  108. }
  109. struct netrx_pending_operations {
  110. unsigned int copy_prod, copy_cons;
  111. unsigned int meta_prod, meta_cons;
  112. struct gnttab_copy *copy;
  113. struct xenvif_rx_meta *meta;
  114. int copy_off;
  115. grant_ref_t copy_gref;
  116. };
  117. static struct xenvif_rx_meta *get_next_rx_buffer(
  118. struct xenvif_queue *queue,
  119. struct netrx_pending_operations *npo)
  120. {
  121. struct xenvif_rx_meta *meta;
  122. struct xen_netif_rx_request req;
  123. RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req);
  124. meta = npo->meta + npo->meta_prod++;
  125. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  126. meta->gso_size = 0;
  127. meta->size = 0;
  128. meta->id = req.id;
  129. npo->copy_off = 0;
  130. npo->copy_gref = req.gref;
  131. return meta;
  132. }
  133. struct gop_frag_copy {
  134. struct xenvif_queue *queue;
  135. struct netrx_pending_operations *npo;
  136. struct xenvif_rx_meta *meta;
  137. int head;
  138. int gso_type;
  139. int protocol;
  140. int hash_present;
  141. struct page *page;
  142. };
  143. static void xenvif_setup_copy_gop(unsigned long gfn,
  144. unsigned int offset,
  145. unsigned int *len,
  146. struct gop_frag_copy *info)
  147. {
  148. struct gnttab_copy *copy_gop;
  149. struct xen_page_foreign *foreign;
  150. /* Convenient aliases */
  151. struct xenvif_queue *queue = info->queue;
  152. struct netrx_pending_operations *npo = info->npo;
  153. struct page *page = info->page;
  154. WARN_ON(npo->copy_off > MAX_BUFFER_OFFSET);
  155. if (npo->copy_off == MAX_BUFFER_OFFSET)
  156. info->meta = get_next_rx_buffer(queue, npo);
  157. if (npo->copy_off + *len > MAX_BUFFER_OFFSET)
  158. *len = MAX_BUFFER_OFFSET - npo->copy_off;
  159. copy_gop = npo->copy + npo->copy_prod++;
  160. copy_gop->flags = GNTCOPY_dest_gref;
  161. copy_gop->len = *len;
  162. foreign = xen_page_foreign(page);
  163. if (foreign) {
  164. copy_gop->source.domid = foreign->domid;
  165. copy_gop->source.u.ref = foreign->gref;
  166. copy_gop->flags |= GNTCOPY_source_gref;
  167. } else {
  168. copy_gop->source.domid = DOMID_SELF;
  169. copy_gop->source.u.gmfn = gfn;
  170. }
  171. copy_gop->source.offset = offset;
  172. copy_gop->dest.domid = queue->vif->domid;
  173. copy_gop->dest.offset = npo->copy_off;
  174. copy_gop->dest.u.ref = npo->copy_gref;
  175. npo->copy_off += *len;
  176. info->meta->size += *len;
  177. if (!info->head)
  178. return;
  179. /* Leave a gap for the GSO descriptor. */
  180. if ((1 << info->gso_type) & queue->vif->gso_mask)
  181. queue->rx.req_cons++;
  182. /* Leave a gap for the hash extra segment. */
  183. if (info->hash_present)
  184. queue->rx.req_cons++;
  185. info->head = 0; /* There must be something in this buffer now */
  186. }
  187. static void xenvif_gop_frag_copy_grant(unsigned long gfn,
  188. unsigned int offset,
  189. unsigned int len,
  190. void *data)
  191. {
  192. unsigned int bytes;
  193. while (len) {
  194. bytes = len;
  195. xenvif_setup_copy_gop(gfn, offset, &bytes, data);
  196. offset += bytes;
  197. len -= bytes;
  198. }
  199. }
  200. /* Set up the grant operations for this fragment. If it's a flipping
  201. * interface, we also set up the unmap request from here.
  202. */
  203. static void xenvif_gop_frag_copy(struct xenvif_queue *queue,
  204. struct sk_buff *skb,
  205. struct netrx_pending_operations *npo,
  206. struct page *page, unsigned long size,
  207. unsigned long offset, int *head)
  208. {
  209. struct gop_frag_copy info = {
  210. .queue = queue,
  211. .npo = npo,
  212. .head = *head,
  213. .gso_type = XEN_NETIF_GSO_TYPE_NONE,
  214. /* xenvif_set_skb_hash() will have either set a s/w
  215. * hash or cleared the hash depending on
  216. * whether the the frontend wants a hash for this skb.
  217. */
  218. .hash_present = skb->sw_hash,
  219. };
  220. unsigned long bytes;
  221. if (skb_is_gso(skb)) {
  222. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  223. info.gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  224. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  225. info.gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  226. }
  227. /* Data must not cross a page boundary. */
  228. WARN_ON(size + offset > (PAGE_SIZE << compound_order(page)));
  229. info.meta = npo->meta + npo->meta_prod - 1;
  230. /* Skip unused frames from start of page */
  231. page += offset >> PAGE_SHIFT;
  232. offset &= ~PAGE_MASK;
  233. while (size > 0) {
  234. WARN_ON(offset >= PAGE_SIZE);
  235. bytes = PAGE_SIZE - offset;
  236. if (bytes > size)
  237. bytes = size;
  238. info.page = page;
  239. gnttab_foreach_grant_in_range(page, offset, bytes,
  240. xenvif_gop_frag_copy_grant,
  241. &info);
  242. size -= bytes;
  243. offset = 0;
  244. /* Next page */
  245. if (size) {
  246. WARN_ON(!PageCompound(page));
  247. page++;
  248. }
  249. }
  250. *head = info.head;
  251. }
  252. /* Prepare an SKB to be transmitted to the frontend.
  253. *
  254. * This function is responsible for allocating grant operations, meta
  255. * structures, etc.
  256. *
  257. * It returns the number of meta structures consumed. The number of
  258. * ring slots used is always equal to the number of meta slots used
  259. * plus the number of GSO descriptors used. Currently, we use either
  260. * zero GSO descriptors (for non-GSO packets) or one descriptor (for
  261. * frontend-side LRO).
  262. */
  263. static int xenvif_gop_skb(struct sk_buff *skb,
  264. struct netrx_pending_operations *npo,
  265. struct xenvif_queue *queue)
  266. {
  267. struct xenvif *vif = netdev_priv(skb->dev);
  268. int nr_frags = skb_shinfo(skb)->nr_frags;
  269. int i;
  270. struct xen_netif_rx_request req;
  271. struct xenvif_rx_meta *meta;
  272. unsigned char *data;
  273. int head = 1;
  274. int old_meta_prod;
  275. int gso_type;
  276. old_meta_prod = npo->meta_prod;
  277. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  278. if (skb_is_gso(skb)) {
  279. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  280. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  281. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  282. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  283. }
  284. RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req);
  285. meta = npo->meta + npo->meta_prod++;
  286. if ((1 << gso_type) & vif->gso_mask) {
  287. meta->gso_type = gso_type;
  288. meta->gso_size = skb_shinfo(skb)->gso_size;
  289. } else {
  290. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  291. meta->gso_size = 0;
  292. }
  293. meta->size = 0;
  294. meta->id = req.id;
  295. npo->copy_off = 0;
  296. npo->copy_gref = req.gref;
  297. data = skb->data;
  298. while (data < skb_tail_pointer(skb)) {
  299. unsigned int offset = offset_in_page(data);
  300. unsigned int len = PAGE_SIZE - offset;
  301. if (data + len > skb_tail_pointer(skb))
  302. len = skb_tail_pointer(skb) - data;
  303. xenvif_gop_frag_copy(queue, skb, npo,
  304. virt_to_page(data), len, offset, &head);
  305. data += len;
  306. }
  307. for (i = 0; i < nr_frags; i++) {
  308. xenvif_gop_frag_copy(queue, skb, npo,
  309. skb_frag_page(&skb_shinfo(skb)->frags[i]),
  310. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  311. skb_shinfo(skb)->frags[i].page_offset,
  312. &head);
  313. }
  314. return npo->meta_prod - old_meta_prod;
  315. }
  316. /* This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
  317. * used to set up the operations on the top of
  318. * netrx_pending_operations, which have since been done. Check that
  319. * they didn't give any errors and advance over them.
  320. */
  321. static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
  322. struct netrx_pending_operations *npo)
  323. {
  324. struct gnttab_copy *copy_op;
  325. int status = XEN_NETIF_RSP_OKAY;
  326. int i;
  327. for (i = 0; i < nr_meta_slots; i++) {
  328. copy_op = npo->copy + npo->copy_cons++;
  329. if (copy_op->status != GNTST_okay) {
  330. netdev_dbg(vif->dev,
  331. "Bad status %d from copy to DOM%d.\n",
  332. copy_op->status, vif->domid);
  333. status = XEN_NETIF_RSP_ERROR;
  334. }
  335. }
  336. return status;
  337. }
  338. static struct xen_netif_rx_response *make_rx_response(
  339. struct xenvif_queue *queue, u16 id, s8 st, u16 offset, u16 size,
  340. u16 flags)
  341. {
  342. RING_IDX i = queue->rx.rsp_prod_pvt;
  343. struct xen_netif_rx_response *resp;
  344. resp = RING_GET_RESPONSE(&queue->rx, i);
  345. resp->offset = offset;
  346. resp->flags = flags;
  347. resp->id = id;
  348. resp->status = (s16)size;
  349. if (st < 0)
  350. resp->status = (s16)st;
  351. queue->rx.rsp_prod_pvt = ++i;
  352. return resp;
  353. }
  354. static void xenvif_add_frag_responses(struct xenvif_queue *queue,
  355. int status,
  356. struct xenvif_rx_meta *meta,
  357. int nr_meta_slots)
  358. {
  359. int i;
  360. unsigned long offset;
  361. /* No fragments used */
  362. if (nr_meta_slots <= 1)
  363. return;
  364. nr_meta_slots--;
  365. for (i = 0; i < nr_meta_slots; i++) {
  366. int flags;
  367. if (i == nr_meta_slots - 1)
  368. flags = 0;
  369. else
  370. flags = XEN_NETRXF_more_data;
  371. offset = 0;
  372. make_rx_response(queue, meta[i].id, status, offset,
  373. meta[i].size, flags);
  374. }
  375. }
  376. static void xenvif_rx_action(struct xenvif_queue *queue)
  377. {
  378. struct xenvif *vif = queue->vif;
  379. s8 status;
  380. u16 flags;
  381. struct xen_netif_rx_response *resp;
  382. struct sk_buff_head rxq;
  383. struct sk_buff *skb;
  384. LIST_HEAD(notify);
  385. int ret;
  386. unsigned long offset;
  387. bool need_to_notify = false;
  388. struct netrx_pending_operations npo = {
  389. .copy = queue->grant_copy_op,
  390. .meta = queue->meta,
  391. };
  392. skb_queue_head_init(&rxq);
  393. while (xenvif_rx_ring_slots_available(queue) &&
  394. (skb = xenvif_rx_dequeue(queue)) != NULL) {
  395. queue->last_rx_time = jiffies;
  396. XENVIF_RX_CB(skb)->meta_slots_used =
  397. xenvif_gop_skb(skb, &npo, queue);
  398. __skb_queue_tail(&rxq, skb);
  399. }
  400. WARN_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
  401. if (!npo.copy_prod)
  402. goto done;
  403. WARN_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
  404. gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
  405. while ((skb = __skb_dequeue(&rxq)) != NULL) {
  406. struct xen_netif_extra_info *extra = NULL;
  407. queue->stats.tx_bytes += skb->len;
  408. queue->stats.tx_packets++;
  409. status = xenvif_check_gop(vif,
  410. XENVIF_RX_CB(skb)->meta_slots_used,
  411. &npo);
  412. if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
  413. flags = 0;
  414. else
  415. flags = XEN_NETRXF_more_data;
  416. if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
  417. flags |= XEN_NETRXF_csum_blank |
  418. XEN_NETRXF_data_validated;
  419. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  420. /* remote but checksummed. */
  421. flags |= XEN_NETRXF_data_validated;
  422. offset = 0;
  423. resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
  424. status, offset,
  425. queue->meta[npo.meta_cons].size,
  426. flags);
  427. if ((1 << queue->meta[npo.meta_cons].gso_type) &
  428. vif->gso_mask) {
  429. extra = (struct xen_netif_extra_info *)
  430. RING_GET_RESPONSE(&queue->rx,
  431. queue->rx.rsp_prod_pvt++);
  432. resp->flags |= XEN_NETRXF_extra_info;
  433. extra->u.gso.type = queue->meta[npo.meta_cons].gso_type;
  434. extra->u.gso.size = queue->meta[npo.meta_cons].gso_size;
  435. extra->u.gso.pad = 0;
  436. extra->u.gso.features = 0;
  437. extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
  438. extra->flags = 0;
  439. }
  440. if (skb->sw_hash) {
  441. /* Since the skb got here via xenvif_select_queue()
  442. * we know that the hash has been re-calculated
  443. * according to a configuration set by the frontend
  444. * and therefore we know that it is legitimate to
  445. * pass it to the frontend.
  446. */
  447. if (resp->flags & XEN_NETRXF_extra_info)
  448. extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
  449. else
  450. resp->flags |= XEN_NETRXF_extra_info;
  451. extra = (struct xen_netif_extra_info *)
  452. RING_GET_RESPONSE(&queue->rx,
  453. queue->rx.rsp_prod_pvt++);
  454. extra->u.hash.algorithm =
  455. XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
  456. if (skb->l4_hash)
  457. extra->u.hash.type =
  458. skb->protocol == htons(ETH_P_IP) ?
  459. _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
  460. _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
  461. else
  462. extra->u.hash.type =
  463. skb->protocol == htons(ETH_P_IP) ?
  464. _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
  465. _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
  466. *(uint32_t *)extra->u.hash.value =
  467. skb_get_hash_raw(skb);
  468. extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
  469. extra->flags = 0;
  470. }
  471. xenvif_add_frag_responses(queue, status,
  472. queue->meta + npo.meta_cons + 1,
  473. XENVIF_RX_CB(skb)->meta_slots_used);
  474. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
  475. need_to_notify |= !!ret;
  476. npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
  477. dev_kfree_skb(skb);
  478. }
  479. done:
  480. if (need_to_notify)
  481. notify_remote_via_irq(queue->rx_irq);
  482. }
  483. static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
  484. {
  485. RING_IDX prod, cons;
  486. prod = queue->rx.sring->req_prod;
  487. cons = queue->rx.req_cons;
  488. return !queue->stalled &&
  489. prod - cons < 1 &&
  490. time_after(jiffies,
  491. queue->last_rx_time + queue->vif->stall_timeout);
  492. }
  493. static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
  494. {
  495. RING_IDX prod, cons;
  496. prod = queue->rx.sring->req_prod;
  497. cons = queue->rx.req_cons;
  498. return queue->stalled && prod - cons >= 1;
  499. }
  500. static bool xenvif_have_rx_work(struct xenvif_queue *queue)
  501. {
  502. return xenvif_rx_ring_slots_available(queue) ||
  503. (queue->vif->stall_timeout &&
  504. (xenvif_rx_queue_stalled(queue) ||
  505. xenvif_rx_queue_ready(queue))) ||
  506. kthread_should_stop() ||
  507. queue->vif->disabled;
  508. }
  509. static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
  510. {
  511. struct sk_buff *skb;
  512. long timeout;
  513. skb = skb_peek(&queue->rx_queue);
  514. if (!skb)
  515. return MAX_SCHEDULE_TIMEOUT;
  516. timeout = XENVIF_RX_CB(skb)->expires - jiffies;
  517. return timeout < 0 ? 0 : timeout;
  518. }
  519. /* Wait until the guest Rx thread has work.
  520. *
  521. * The timeout needs to be adjusted based on the current head of the
  522. * queue (and not just the head at the beginning). In particular, if
  523. * the queue is initially empty an infinite timeout is used and this
  524. * needs to be reduced when a skb is queued.
  525. *
  526. * This cannot be done with wait_event_timeout() because it only
  527. * calculates the timeout once.
  528. */
  529. static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
  530. {
  531. DEFINE_WAIT(wait);
  532. if (xenvif_have_rx_work(queue))
  533. return;
  534. for (;;) {
  535. long ret;
  536. prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
  537. if (xenvif_have_rx_work(queue))
  538. break;
  539. ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
  540. if (!ret)
  541. break;
  542. }
  543. finish_wait(&queue->wq, &wait);
  544. }
  545. static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
  546. {
  547. struct xenvif *vif = queue->vif;
  548. queue->stalled = true;
  549. /* At least one queue has stalled? Disable the carrier. */
  550. spin_lock(&vif->lock);
  551. if (vif->stalled_queues++ == 0) {
  552. netdev_info(vif->dev, "Guest Rx stalled");
  553. netif_carrier_off(vif->dev);
  554. }
  555. spin_unlock(&vif->lock);
  556. }
  557. static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
  558. {
  559. struct xenvif *vif = queue->vif;
  560. queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
  561. queue->stalled = false;
  562. /* All queues are ready? Enable the carrier. */
  563. spin_lock(&vif->lock);
  564. if (--vif->stalled_queues == 0) {
  565. netdev_info(vif->dev, "Guest Rx ready");
  566. netif_carrier_on(vif->dev);
  567. }
  568. spin_unlock(&vif->lock);
  569. }
  570. int xenvif_kthread_guest_rx(void *data)
  571. {
  572. struct xenvif_queue *queue = data;
  573. struct xenvif *vif = queue->vif;
  574. if (!vif->stall_timeout)
  575. xenvif_queue_carrier_on(queue);
  576. for (;;) {
  577. xenvif_wait_for_rx_work(queue);
  578. if (kthread_should_stop())
  579. break;
  580. /* This frontend is found to be rogue, disable it in
  581. * kthread context. Currently this is only set when
  582. * netback finds out frontend sends malformed packet,
  583. * but we cannot disable the interface in softirq
  584. * context so we defer it here, if this thread is
  585. * associated with queue 0.
  586. */
  587. if (unlikely(vif->disabled && queue->id == 0)) {
  588. xenvif_carrier_off(vif);
  589. break;
  590. }
  591. if (!skb_queue_empty(&queue->rx_queue))
  592. xenvif_rx_action(queue);
  593. /* If the guest hasn't provided any Rx slots for a
  594. * while it's probably not responsive, drop the
  595. * carrier so packets are dropped earlier.
  596. */
  597. if (vif->stall_timeout) {
  598. if (xenvif_rx_queue_stalled(queue))
  599. xenvif_queue_carrier_off(queue);
  600. else if (xenvif_rx_queue_ready(queue))
  601. xenvif_queue_carrier_on(queue);
  602. }
  603. /* Queued packets may have foreign pages from other
  604. * domains. These cannot be queued indefinitely as
  605. * this would starve guests of grant refs and transmit
  606. * slots.
  607. */
  608. xenvif_rx_queue_drop_expired(queue);
  609. xenvif_rx_queue_maybe_wake(queue);
  610. cond_resched();
  611. }
  612. /* Bin any remaining skbs */
  613. xenvif_rx_queue_purge(queue);
  614. return 0;
  615. }