rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. if (queue->rx_queue_len < queue->rx_queue_max) {
  79. struct netdev_queue *txq;
  80. txq = netdev_get_tx_queue(queue->vif->dev, queue->id);
  81. netif_tx_wake_queue(txq);
  82. }
  83. }
  84. spin_unlock_irq(&queue->rx_queue.lock);
  85. return skb;
  86. }
  87. static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
  88. {
  89. struct sk_buff *skb;
  90. while ((skb = xenvif_rx_dequeue(queue)) != NULL)
  91. kfree_skb(skb);
  92. }
  93. static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
  94. {
  95. struct sk_buff *skb;
  96. for (;;) {
  97. skb = skb_peek(&queue->rx_queue);
  98. if (!skb)
  99. break;
  100. if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
  101. break;
  102. xenvif_rx_dequeue(queue);
  103. kfree_skb(skb);
  104. }
  105. }
  106. static void xenvif_rx_copy_flush(struct xenvif_queue *queue)
  107. {
  108. unsigned int i;
  109. int notify;
  110. gnttab_batch_copy(queue->rx_copy.op, queue->rx_copy.num);
  111. for (i = 0; i < queue->rx_copy.num; i++) {
  112. struct gnttab_copy *op;
  113. op = &queue->rx_copy.op[i];
  114. /* If the copy failed, overwrite the status field in
  115. * the corresponding response.
  116. */
  117. if (unlikely(op->status != GNTST_okay)) {
  118. struct xen_netif_rx_response *rsp;
  119. rsp = RING_GET_RESPONSE(&queue->rx,
  120. queue->rx_copy.idx[i]);
  121. rsp->status = op->status;
  122. }
  123. }
  124. queue->rx_copy.num = 0;
  125. /* Push responses for all completed packets. */
  126. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, notify);
  127. if (notify)
  128. notify_remote_via_irq(queue->rx_irq);
  129. __skb_queue_purge(queue->rx_copy.completed);
  130. }
  131. static void xenvif_rx_copy_add(struct xenvif_queue *queue,
  132. struct xen_netif_rx_request *req,
  133. unsigned int offset, void *data, size_t len)
  134. {
  135. struct gnttab_copy *op;
  136. struct page *page;
  137. struct xen_page_foreign *foreign;
  138. if (queue->rx_copy.num == COPY_BATCH_SIZE)
  139. xenvif_rx_copy_flush(queue);
  140. op = &queue->rx_copy.op[queue->rx_copy.num];
  141. page = virt_to_page(data);
  142. op->flags = GNTCOPY_dest_gref;
  143. foreign = xen_page_foreign(page);
  144. if (foreign) {
  145. op->source.domid = foreign->domid;
  146. op->source.u.ref = foreign->gref;
  147. op->flags |= GNTCOPY_source_gref;
  148. } else {
  149. op->source.u.gmfn = virt_to_gfn(data);
  150. op->source.domid = DOMID_SELF;
  151. }
  152. op->source.offset = xen_offset_in_page(data);
  153. op->dest.u.ref = req->gref;
  154. op->dest.domid = queue->vif->domid;
  155. op->dest.offset = offset;
  156. op->len = len;
  157. queue->rx_copy.idx[queue->rx_copy.num] = queue->rx.req_cons;
  158. queue->rx_copy.num++;
  159. }
  160. static unsigned int xenvif_gso_type(struct sk_buff *skb)
  161. {
  162. if (skb_is_gso(skb)) {
  163. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  164. return XEN_NETIF_GSO_TYPE_TCPV4;
  165. else
  166. return XEN_NETIF_GSO_TYPE_TCPV6;
  167. }
  168. return XEN_NETIF_GSO_TYPE_NONE;
  169. }
  170. struct xenvif_pkt_state {
  171. struct sk_buff *skb;
  172. size_t remaining_len;
  173. struct sk_buff *frag_iter;
  174. int frag; /* frag == -1 => frag_iter->head */
  175. unsigned int frag_offset;
  176. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
  177. unsigned int extra_count;
  178. unsigned int slot;
  179. };
  180. static void xenvif_rx_next_skb(struct xenvif_queue *queue,
  181. struct xenvif_pkt_state *pkt)
  182. {
  183. struct sk_buff *skb;
  184. unsigned int gso_type;
  185. skb = xenvif_rx_dequeue(queue);
  186. queue->stats.tx_bytes += skb->len;
  187. queue->stats.tx_packets++;
  188. /* Reset packet state. */
  189. memset(pkt, 0, sizeof(struct xenvif_pkt_state));
  190. pkt->skb = skb;
  191. pkt->frag_iter = skb;
  192. pkt->remaining_len = skb->len;
  193. pkt->frag = -1;
  194. gso_type = xenvif_gso_type(skb);
  195. if ((1 << gso_type) & queue->vif->gso_mask) {
  196. struct xen_netif_extra_info *extra;
  197. extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  198. extra->u.gso.type = gso_type;
  199. extra->u.gso.size = skb_shinfo(skb)->gso_size;
  200. extra->u.gso.pad = 0;
  201. extra->u.gso.features = 0;
  202. extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
  203. extra->flags = 0;
  204. pkt->extra_count++;
  205. }
  206. if (skb->sw_hash) {
  207. struct xen_netif_extra_info *extra;
  208. extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
  209. extra->u.hash.algorithm =
  210. XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
  211. if (skb->l4_hash)
  212. extra->u.hash.type =
  213. skb->protocol == htons(ETH_P_IP) ?
  214. _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
  215. _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
  216. else
  217. extra->u.hash.type =
  218. skb->protocol == htons(ETH_P_IP) ?
  219. _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
  220. _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
  221. *(uint32_t *)extra->u.hash.value = skb_get_hash_raw(skb);
  222. extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
  223. extra->flags = 0;
  224. pkt->extra_count++;
  225. }
  226. }
  227. static void xenvif_rx_complete(struct xenvif_queue *queue,
  228. struct xenvif_pkt_state *pkt)
  229. {
  230. /* All responses are ready to be pushed. */
  231. queue->rx.rsp_prod_pvt = queue->rx.req_cons;
  232. __skb_queue_tail(queue->rx_copy.completed, pkt->skb);
  233. }
  234. static void xenvif_rx_next_frag(struct xenvif_pkt_state *pkt)
  235. {
  236. struct sk_buff *frag_iter = pkt->frag_iter;
  237. unsigned int nr_frags = skb_shinfo(frag_iter)->nr_frags;
  238. pkt->frag++;
  239. pkt->frag_offset = 0;
  240. if (pkt->frag >= nr_frags) {
  241. if (frag_iter == pkt->skb)
  242. pkt->frag_iter = skb_shinfo(frag_iter)->frag_list;
  243. else
  244. pkt->frag_iter = frag_iter->next;
  245. pkt->frag = -1;
  246. }
  247. }
  248. static void xenvif_rx_next_chunk(struct xenvif_queue *queue,
  249. struct xenvif_pkt_state *pkt,
  250. unsigned int offset, void **data,
  251. size_t *len)
  252. {
  253. struct sk_buff *frag_iter = pkt->frag_iter;
  254. void *frag_data;
  255. size_t frag_len, chunk_len;
  256. BUG_ON(!frag_iter);
  257. if (pkt->frag == -1) {
  258. frag_data = frag_iter->data;
  259. frag_len = skb_headlen(frag_iter);
  260. } else {
  261. skb_frag_t *frag = &skb_shinfo(frag_iter)->frags[pkt->frag];
  262. frag_data = skb_frag_address(frag);
  263. frag_len = skb_frag_size(frag);
  264. }
  265. frag_data += pkt->frag_offset;
  266. frag_len -= pkt->frag_offset;
  267. chunk_len = min_t(size_t, frag_len, XEN_PAGE_SIZE - offset);
  268. chunk_len = min_t(size_t, chunk_len, XEN_PAGE_SIZE -
  269. xen_offset_in_page(frag_data));
  270. pkt->frag_offset += chunk_len;
  271. /* Advance to next frag? */
  272. if (frag_len == chunk_len)
  273. xenvif_rx_next_frag(pkt);
  274. *data = frag_data;
  275. *len = chunk_len;
  276. }
  277. static void xenvif_rx_data_slot(struct xenvif_queue *queue,
  278. struct xenvif_pkt_state *pkt,
  279. struct xen_netif_rx_request *req,
  280. struct xen_netif_rx_response *rsp)
  281. {
  282. unsigned int offset = 0;
  283. unsigned int flags;
  284. do {
  285. size_t len;
  286. void *data;
  287. xenvif_rx_next_chunk(queue, pkt, offset, &data, &len);
  288. xenvif_rx_copy_add(queue, req, offset, data, len);
  289. offset += len;
  290. pkt->remaining_len -= len;
  291. } while (offset < XEN_PAGE_SIZE && pkt->remaining_len > 0);
  292. if (pkt->remaining_len > 0)
  293. flags = XEN_NETRXF_more_data;
  294. else
  295. flags = 0;
  296. if (pkt->slot == 0) {
  297. struct sk_buff *skb = pkt->skb;
  298. if (skb->ip_summed == CHECKSUM_PARTIAL)
  299. flags |= XEN_NETRXF_csum_blank |
  300. XEN_NETRXF_data_validated;
  301. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  302. flags |= XEN_NETRXF_data_validated;
  303. if (pkt->extra_count != 0)
  304. flags |= XEN_NETRXF_extra_info;
  305. }
  306. rsp->offset = 0;
  307. rsp->flags = flags;
  308. rsp->id = req->id;
  309. rsp->status = (s16)offset;
  310. }
  311. static void xenvif_rx_extra_slot(struct xenvif_queue *queue,
  312. struct xenvif_pkt_state *pkt,
  313. struct xen_netif_rx_request *req,
  314. struct xen_netif_rx_response *rsp)
  315. {
  316. struct xen_netif_extra_info *extra = (void *)rsp;
  317. unsigned int i;
  318. pkt->extra_count--;
  319. for (i = 0; i < ARRAY_SIZE(pkt->extras); i++) {
  320. if (pkt->extras[i].type) {
  321. *extra = pkt->extras[i];
  322. if (pkt->extra_count != 0)
  323. extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
  324. pkt->extras[i].type = 0;
  325. return;
  326. }
  327. }
  328. BUG();
  329. }
  330. void xenvif_rx_skb(struct xenvif_queue *queue)
  331. {
  332. struct xenvif_pkt_state pkt;
  333. xenvif_rx_next_skb(queue, &pkt);
  334. queue->last_rx_time = jiffies;
  335. do {
  336. struct xen_netif_rx_request *req;
  337. struct xen_netif_rx_response *rsp;
  338. req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons);
  339. rsp = RING_GET_RESPONSE(&queue->rx, queue->rx.req_cons);
  340. /* Extras must go after the first data slot */
  341. if (pkt.slot != 0 && pkt.extra_count != 0)
  342. xenvif_rx_extra_slot(queue, &pkt, req, rsp);
  343. else
  344. xenvif_rx_data_slot(queue, &pkt, req, rsp);
  345. queue->rx.req_cons++;
  346. pkt.slot++;
  347. } while (pkt.remaining_len > 0 || pkt.extra_count != 0);
  348. xenvif_rx_complete(queue, &pkt);
  349. }
  350. #define RX_BATCH_SIZE 64
  351. void xenvif_rx_action(struct xenvif_queue *queue)
  352. {
  353. struct sk_buff_head completed_skbs;
  354. unsigned int work_done = 0;
  355. __skb_queue_head_init(&completed_skbs);
  356. queue->rx_copy.completed = &completed_skbs;
  357. while (xenvif_rx_ring_slots_available(queue) &&
  358. work_done < RX_BATCH_SIZE) {
  359. xenvif_rx_skb(queue);
  360. work_done++;
  361. }
  362. /* Flush any pending copies and complete all skbs. */
  363. xenvif_rx_copy_flush(queue);
  364. }
  365. static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
  366. {
  367. RING_IDX prod, cons;
  368. prod = queue->rx.sring->req_prod;
  369. cons = queue->rx.req_cons;
  370. return !queue->stalled &&
  371. prod - cons < 1 &&
  372. time_after(jiffies,
  373. queue->last_rx_time + queue->vif->stall_timeout);
  374. }
  375. static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
  376. {
  377. RING_IDX prod, cons;
  378. prod = queue->rx.sring->req_prod;
  379. cons = queue->rx.req_cons;
  380. return queue->stalled && prod - cons >= 1;
  381. }
  382. static bool xenvif_have_rx_work(struct xenvif_queue *queue)
  383. {
  384. return xenvif_rx_ring_slots_available(queue) ||
  385. (queue->vif->stall_timeout &&
  386. (xenvif_rx_queue_stalled(queue) ||
  387. xenvif_rx_queue_ready(queue))) ||
  388. kthread_should_stop() ||
  389. queue->vif->disabled;
  390. }
  391. static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
  392. {
  393. struct sk_buff *skb;
  394. long timeout;
  395. skb = skb_peek(&queue->rx_queue);
  396. if (!skb)
  397. return MAX_SCHEDULE_TIMEOUT;
  398. timeout = XENVIF_RX_CB(skb)->expires - jiffies;
  399. return timeout < 0 ? 0 : timeout;
  400. }
  401. /* Wait until the guest Rx thread has work.
  402. *
  403. * The timeout needs to be adjusted based on the current head of the
  404. * queue (and not just the head at the beginning). In particular, if
  405. * the queue is initially empty an infinite timeout is used and this
  406. * needs to be reduced when a skb is queued.
  407. *
  408. * This cannot be done with wait_event_timeout() because it only
  409. * calculates the timeout once.
  410. */
  411. static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
  412. {
  413. DEFINE_WAIT(wait);
  414. if (xenvif_have_rx_work(queue))
  415. return;
  416. for (;;) {
  417. long ret;
  418. prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
  419. if (xenvif_have_rx_work(queue))
  420. break;
  421. ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
  422. if (!ret)
  423. break;
  424. }
  425. finish_wait(&queue->wq, &wait);
  426. }
  427. static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
  428. {
  429. struct xenvif *vif = queue->vif;
  430. queue->stalled = true;
  431. /* At least one queue has stalled? Disable the carrier. */
  432. spin_lock(&vif->lock);
  433. if (vif->stalled_queues++ == 0) {
  434. netdev_info(vif->dev, "Guest Rx stalled");
  435. netif_carrier_off(vif->dev);
  436. }
  437. spin_unlock(&vif->lock);
  438. }
  439. static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
  440. {
  441. struct xenvif *vif = queue->vif;
  442. queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
  443. queue->stalled = false;
  444. /* All queues are ready? Enable the carrier. */
  445. spin_lock(&vif->lock);
  446. if (--vif->stalled_queues == 0) {
  447. netdev_info(vif->dev, "Guest Rx ready");
  448. netif_carrier_on(vif->dev);
  449. }
  450. spin_unlock(&vif->lock);
  451. }
  452. int xenvif_kthread_guest_rx(void *data)
  453. {
  454. struct xenvif_queue *queue = data;
  455. struct xenvif *vif = queue->vif;
  456. if (!vif->stall_timeout)
  457. xenvif_queue_carrier_on(queue);
  458. for (;;) {
  459. xenvif_wait_for_rx_work(queue);
  460. if (kthread_should_stop())
  461. break;
  462. /* This frontend is found to be rogue, disable it in
  463. * kthread context. Currently this is only set when
  464. * netback finds out frontend sends malformed packet,
  465. * but we cannot disable the interface in softirq
  466. * context so we defer it here, if this thread is
  467. * associated with queue 0.
  468. */
  469. if (unlikely(vif->disabled && queue->id == 0)) {
  470. xenvif_carrier_off(vif);
  471. break;
  472. }
  473. if (!skb_queue_empty(&queue->rx_queue))
  474. xenvif_rx_action(queue);
  475. /* If the guest hasn't provided any Rx slots for a
  476. * while it's probably not responsive, drop the
  477. * carrier so packets are dropped earlier.
  478. */
  479. if (vif->stall_timeout) {
  480. if (xenvif_rx_queue_stalled(queue))
  481. xenvif_queue_carrier_off(queue);
  482. else if (xenvif_rx_queue_ready(queue))
  483. xenvif_queue_carrier_on(queue);
  484. }
  485. /* Queued packets may have foreign pages from other
  486. * domains. These cannot be queued indefinitely as
  487. * this would starve guests of grant refs and transmit
  488. * slots.
  489. */
  490. xenvif_rx_queue_drop_expired(queue);
  491. cond_resched();
  492. }
  493. /* Bin any remaining skbs */
  494. xenvif_rx_queue_purge(queue);
  495. return 0;
  496. }