rx.c 14 KB

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