/drivers/net/xen-netback/netback.c

http://github.com/mirrors/linux · C · 1692 lines · 1211 code · 297 blank · 184 comment · 166 complexity · 2c224580d5dd7ee5dcd3433389b846dd MD5 · raw file

  1. /*
  2. * Back-end of the driver for virtual network devices. This portion of the
  3. * driver exports a 'unified' network-device interface that can be accessed
  4. * by any operating system that implements a compatible front end. A
  5. * reference front-end implementation can be found in:
  6. * drivers/net/xen-netfront.c
  7. *
  8. * Copyright (c) 2002-2005, K A Fraser
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation; or, when distributed
  13. * separately from the Linux kernel or incorporated into other
  14. * software packages, subject to the following license:
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this source file (the "Software"), to deal in the Software without
  18. * restriction, including without limitation the rights to use, copy, modify,
  19. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  20. * and to permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. * IN THE SOFTWARE.
  33. */
  34. #include "common.h"
  35. #include <linux/kthread.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/udp.h>
  38. #include <linux/highmem.h>
  39. #include <net/tcp.h>
  40. #include <xen/xen.h>
  41. #include <xen/events.h>
  42. #include <xen/interface/memory.h>
  43. #include <xen/page.h>
  44. #include <asm/xen/hypercall.h>
  45. /* Provide an option to disable split event channels at load time as
  46. * event channels are limited resource. Split event channels are
  47. * enabled by default.
  48. */
  49. bool separate_tx_rx_irq = true;
  50. module_param(separate_tx_rx_irq, bool, 0644);
  51. /* The time that packets can stay on the guest Rx internal queue
  52. * before they are dropped.
  53. */
  54. unsigned int rx_drain_timeout_msecs = 10000;
  55. module_param(rx_drain_timeout_msecs, uint, 0444);
  56. /* The length of time before the frontend is considered unresponsive
  57. * because it isn't providing Rx slots.
  58. */
  59. unsigned int rx_stall_timeout_msecs = 60000;
  60. module_param(rx_stall_timeout_msecs, uint, 0444);
  61. #define MAX_QUEUES_DEFAULT 8
  62. unsigned int xenvif_max_queues;
  63. module_param_named(max_queues, xenvif_max_queues, uint, 0644);
  64. MODULE_PARM_DESC(max_queues,
  65. "Maximum number of queues per virtual interface");
  66. /*
  67. * This is the maximum slots a skb can have. If a guest sends a skb
  68. * which exceeds this limit it is considered malicious.
  69. */
  70. #define FATAL_SKB_SLOTS_DEFAULT 20
  71. static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
  72. module_param(fatal_skb_slots, uint, 0444);
  73. /* The amount to copy out of the first guest Tx slot into the skb's
  74. * linear area. If the first slot has more data, it will be mapped
  75. * and put into the first frag.
  76. *
  77. * This is sized to avoid pulling headers from the frags for most
  78. * TCP/IP packets.
  79. */
  80. #define XEN_NETBACK_TX_COPY_LEN 128
  81. /* This is the maximum number of flows in the hash cache. */
  82. #define XENVIF_HASH_CACHE_SIZE_DEFAULT 64
  83. unsigned int xenvif_hash_cache_size = XENVIF_HASH_CACHE_SIZE_DEFAULT;
  84. module_param_named(hash_cache_size, xenvif_hash_cache_size, uint, 0644);
  85. MODULE_PARM_DESC(hash_cache_size, "Number of flows in the hash cache");
  86. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  87. u8 status);
  88. static void make_tx_response(struct xenvif_queue *queue,
  89. struct xen_netif_tx_request *txp,
  90. unsigned int extra_count,
  91. s8 st);
  92. static void push_tx_responses(struct xenvif_queue *queue);
  93. static inline int tx_work_todo(struct xenvif_queue *queue);
  94. static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
  95. u16 idx)
  96. {
  97. return page_to_pfn(queue->mmap_pages[idx]);
  98. }
  99. static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
  100. u16 idx)
  101. {
  102. return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
  103. }
  104. #define callback_param(vif, pending_idx) \
  105. (vif->pending_tx_info[pending_idx].callback_struct)
  106. /* Find the containing VIF's structure from a pointer in pending_tx_info array
  107. */
  108. static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
  109. {
  110. u16 pending_idx = ubuf->desc;
  111. struct pending_tx_info *temp =
  112. container_of(ubuf, struct pending_tx_info, callback_struct);
  113. return container_of(temp - pending_idx,
  114. struct xenvif_queue,
  115. pending_tx_info[0]);
  116. }
  117. static u16 frag_get_pending_idx(skb_frag_t *frag)
  118. {
  119. return (u16)skb_frag_off(frag);
  120. }
  121. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  122. {
  123. skb_frag_off_set(frag, pending_idx);
  124. }
  125. static inline pending_ring_idx_t pending_index(unsigned i)
  126. {
  127. return i & (MAX_PENDING_REQS-1);
  128. }
  129. void xenvif_kick_thread(struct xenvif_queue *queue)
  130. {
  131. wake_up(&queue->wq);
  132. }
  133. void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
  134. {
  135. int more_to_do;
  136. RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
  137. if (more_to_do)
  138. napi_schedule(&queue->napi);
  139. }
  140. static void tx_add_credit(struct xenvif_queue *queue)
  141. {
  142. unsigned long max_burst, max_credit;
  143. /*
  144. * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
  145. * Otherwise the interface can seize up due to insufficient credit.
  146. */
  147. max_burst = max(131072UL, queue->credit_bytes);
  148. /* Take care that adding a new chunk of credit doesn't wrap to zero. */
  149. max_credit = queue->remaining_credit + queue->credit_bytes;
  150. if (max_credit < queue->remaining_credit)
  151. max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
  152. queue->remaining_credit = min(max_credit, max_burst);
  153. queue->rate_limited = false;
  154. }
  155. void xenvif_tx_credit_callback(struct timer_list *t)
  156. {
  157. struct xenvif_queue *queue = from_timer(queue, t, credit_timeout);
  158. tx_add_credit(queue);
  159. xenvif_napi_schedule_or_enable_events(queue);
  160. }
  161. static void xenvif_tx_err(struct xenvif_queue *queue,
  162. struct xen_netif_tx_request *txp,
  163. unsigned int extra_count, RING_IDX end)
  164. {
  165. RING_IDX cons = queue->tx.req_cons;
  166. unsigned long flags;
  167. do {
  168. spin_lock_irqsave(&queue->response_lock, flags);
  169. make_tx_response(queue, txp, extra_count, XEN_NETIF_RSP_ERROR);
  170. push_tx_responses(queue);
  171. spin_unlock_irqrestore(&queue->response_lock, flags);
  172. if (cons == end)
  173. break;
  174. RING_COPY_REQUEST(&queue->tx, cons++, txp);
  175. extra_count = 0; /* only the first frag can have extras */
  176. } while (1);
  177. queue->tx.req_cons = cons;
  178. }
  179. static void xenvif_fatal_tx_err(struct xenvif *vif)
  180. {
  181. netdev_err(vif->dev, "fatal error; disabling device\n");
  182. vif->disabled = true;
  183. /* Disable the vif from queue 0's kthread */
  184. if (vif->num_queues)
  185. xenvif_kick_thread(&vif->queues[0]);
  186. }
  187. static int xenvif_count_requests(struct xenvif_queue *queue,
  188. struct xen_netif_tx_request *first,
  189. unsigned int extra_count,
  190. struct xen_netif_tx_request *txp,
  191. int work_to_do)
  192. {
  193. RING_IDX cons = queue->tx.req_cons;
  194. int slots = 0;
  195. int drop_err = 0;
  196. int more_data;
  197. if (!(first->flags & XEN_NETTXF_more_data))
  198. return 0;
  199. do {
  200. struct xen_netif_tx_request dropped_tx = { 0 };
  201. if (slots >= work_to_do) {
  202. netdev_err(queue->vif->dev,
  203. "Asked for %d slots but exceeds this limit\n",
  204. work_to_do);
  205. xenvif_fatal_tx_err(queue->vif);
  206. return -ENODATA;
  207. }
  208. /* This guest is really using too many slots and
  209. * considered malicious.
  210. */
  211. if (unlikely(slots >= fatal_skb_slots)) {
  212. netdev_err(queue->vif->dev,
  213. "Malicious frontend using %d slots, threshold %u\n",
  214. slots, fatal_skb_slots);
  215. xenvif_fatal_tx_err(queue->vif);
  216. return -E2BIG;
  217. }
  218. /* Xen network protocol had implicit dependency on
  219. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  220. * the historical MAX_SKB_FRAGS value 18 to honor the
  221. * same behavior as before. Any packet using more than
  222. * 18 slots but less than fatal_skb_slots slots is
  223. * dropped
  224. */
  225. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  226. if (net_ratelimit())
  227. netdev_dbg(queue->vif->dev,
  228. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  229. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  230. drop_err = -E2BIG;
  231. }
  232. if (drop_err)
  233. txp = &dropped_tx;
  234. RING_COPY_REQUEST(&queue->tx, cons + slots, txp);
  235. /* If the guest submitted a frame >= 64 KiB then
  236. * first->size overflowed and following slots will
  237. * appear to be larger than the frame.
  238. *
  239. * This cannot be fatal error as there are buggy
  240. * frontends that do this.
  241. *
  242. * Consume all slots and drop the packet.
  243. */
  244. if (!drop_err && txp->size > first->size) {
  245. if (net_ratelimit())
  246. netdev_dbg(queue->vif->dev,
  247. "Invalid tx request, slot size %u > remaining size %u\n",
  248. txp->size, first->size);
  249. drop_err = -EIO;
  250. }
  251. first->size -= txp->size;
  252. slots++;
  253. if (unlikely((txp->offset + txp->size) > XEN_PAGE_SIZE)) {
  254. netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
  255. txp->offset, txp->size);
  256. xenvif_fatal_tx_err(queue->vif);
  257. return -EINVAL;
  258. }
  259. more_data = txp->flags & XEN_NETTXF_more_data;
  260. if (!drop_err)
  261. txp++;
  262. } while (more_data);
  263. if (drop_err) {
  264. xenvif_tx_err(queue, first, extra_count, cons + slots);
  265. return drop_err;
  266. }
  267. return slots;
  268. }
  269. struct xenvif_tx_cb {
  270. u16 pending_idx;
  271. };
  272. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  273. static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
  274. u16 pending_idx,
  275. struct xen_netif_tx_request *txp,
  276. unsigned int extra_count,
  277. struct gnttab_map_grant_ref *mop)
  278. {
  279. queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
  280. gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
  281. GNTMAP_host_map | GNTMAP_readonly,
  282. txp->gref, queue->vif->domid);
  283. memcpy(&queue->pending_tx_info[pending_idx].req, txp,
  284. sizeof(*txp));
  285. queue->pending_tx_info[pending_idx].extra_count = extra_count;
  286. }
  287. static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
  288. {
  289. struct sk_buff *skb =
  290. alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
  291. GFP_ATOMIC | __GFP_NOWARN);
  292. if (unlikely(skb == NULL))
  293. return NULL;
  294. /* Packets passed to netif_rx() must have some headroom. */
  295. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  296. /* Initialize it here to avoid later surprises */
  297. skb_shinfo(skb)->destructor_arg = NULL;
  298. return skb;
  299. }
  300. static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
  301. struct sk_buff *skb,
  302. struct xen_netif_tx_request *txp,
  303. struct gnttab_map_grant_ref *gop,
  304. unsigned int frag_overflow,
  305. struct sk_buff *nskb)
  306. {
  307. struct skb_shared_info *shinfo = skb_shinfo(skb);
  308. skb_frag_t *frags = shinfo->frags;
  309. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  310. int start;
  311. pending_ring_idx_t index;
  312. unsigned int nr_slots;
  313. nr_slots = shinfo->nr_frags;
  314. /* Skip first skb fragment if it is on same page as header fragment. */
  315. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  316. for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
  317. shinfo->nr_frags++, txp++, gop++) {
  318. index = pending_index(queue->pending_cons++);
  319. pending_idx = queue->pending_ring[index];
  320. xenvif_tx_create_map_op(queue, pending_idx, txp, 0, gop);
  321. frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
  322. }
  323. if (frag_overflow) {
  324. shinfo = skb_shinfo(nskb);
  325. frags = shinfo->frags;
  326. for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
  327. shinfo->nr_frags++, txp++, gop++) {
  328. index = pending_index(queue->pending_cons++);
  329. pending_idx = queue->pending_ring[index];
  330. xenvif_tx_create_map_op(queue, pending_idx, txp, 0,
  331. gop);
  332. frag_set_pending_idx(&frags[shinfo->nr_frags],
  333. pending_idx);
  334. }
  335. skb_shinfo(skb)->frag_list = nskb;
  336. }
  337. return gop;
  338. }
  339. static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
  340. u16 pending_idx,
  341. grant_handle_t handle)
  342. {
  343. if (unlikely(queue->grant_tx_handle[pending_idx] !=
  344. NETBACK_INVALID_HANDLE)) {
  345. netdev_err(queue->vif->dev,
  346. "Trying to overwrite active handle! pending_idx: 0x%x\n",
  347. pending_idx);
  348. BUG();
  349. }
  350. queue->grant_tx_handle[pending_idx] = handle;
  351. }
  352. static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
  353. u16 pending_idx)
  354. {
  355. if (unlikely(queue->grant_tx_handle[pending_idx] ==
  356. NETBACK_INVALID_HANDLE)) {
  357. netdev_err(queue->vif->dev,
  358. "Trying to unmap invalid handle! pending_idx: 0x%x\n",
  359. pending_idx);
  360. BUG();
  361. }
  362. queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
  363. }
  364. static int xenvif_tx_check_gop(struct xenvif_queue *queue,
  365. struct sk_buff *skb,
  366. struct gnttab_map_grant_ref **gopp_map,
  367. struct gnttab_copy **gopp_copy)
  368. {
  369. struct gnttab_map_grant_ref *gop_map = *gopp_map;
  370. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  371. /* This always points to the shinfo of the skb being checked, which
  372. * could be either the first or the one on the frag_list
  373. */
  374. struct skb_shared_info *shinfo = skb_shinfo(skb);
  375. /* If this is non-NULL, we are currently checking the frag_list skb, and
  376. * this points to the shinfo of the first one
  377. */
  378. struct skb_shared_info *first_shinfo = NULL;
  379. int nr_frags = shinfo->nr_frags;
  380. const bool sharedslot = nr_frags &&
  381. frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
  382. int i, err;
  383. /* Check status of header. */
  384. err = (*gopp_copy)->status;
  385. if (unlikely(err)) {
  386. if (net_ratelimit())
  387. netdev_dbg(queue->vif->dev,
  388. "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
  389. (*gopp_copy)->status,
  390. pending_idx,
  391. (*gopp_copy)->source.u.ref);
  392. /* The first frag might still have this slot mapped */
  393. if (!sharedslot)
  394. xenvif_idx_release(queue, pending_idx,
  395. XEN_NETIF_RSP_ERROR);
  396. }
  397. (*gopp_copy)++;
  398. check_frags:
  399. for (i = 0; i < nr_frags; i++, gop_map++) {
  400. int j, newerr;
  401. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  402. /* Check error status: if okay then remember grant handle. */
  403. newerr = gop_map->status;
  404. if (likely(!newerr)) {
  405. xenvif_grant_handle_set(queue,
  406. pending_idx,
  407. gop_map->handle);
  408. /* Had a previous error? Invalidate this fragment. */
  409. if (unlikely(err)) {
  410. xenvif_idx_unmap(queue, pending_idx);
  411. /* If the mapping of the first frag was OK, but
  412. * the header's copy failed, and they are
  413. * sharing a slot, send an error
  414. */
  415. if (i == 0 && sharedslot)
  416. xenvif_idx_release(queue, pending_idx,
  417. XEN_NETIF_RSP_ERROR);
  418. else
  419. xenvif_idx_release(queue, pending_idx,
  420. XEN_NETIF_RSP_OKAY);
  421. }
  422. continue;
  423. }
  424. /* Error on this fragment: respond to client with an error. */
  425. if (net_ratelimit())
  426. netdev_dbg(queue->vif->dev,
  427. "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
  428. i,
  429. gop_map->status,
  430. pending_idx,
  431. gop_map->ref);
  432. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
  433. /* Not the first error? Preceding frags already invalidated. */
  434. if (err)
  435. continue;
  436. /* First error: if the header haven't shared a slot with the
  437. * first frag, release it as well.
  438. */
  439. if (!sharedslot)
  440. xenvif_idx_release(queue,
  441. XENVIF_TX_CB(skb)->pending_idx,
  442. XEN_NETIF_RSP_OKAY);
  443. /* Invalidate preceding fragments of this skb. */
  444. for (j = 0; j < i; j++) {
  445. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  446. xenvif_idx_unmap(queue, pending_idx);
  447. xenvif_idx_release(queue, pending_idx,
  448. XEN_NETIF_RSP_OKAY);
  449. }
  450. /* And if we found the error while checking the frag_list, unmap
  451. * the first skb's frags
  452. */
  453. if (first_shinfo) {
  454. for (j = 0; j < first_shinfo->nr_frags; j++) {
  455. pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
  456. xenvif_idx_unmap(queue, pending_idx);
  457. xenvif_idx_release(queue, pending_idx,
  458. XEN_NETIF_RSP_OKAY);
  459. }
  460. }
  461. /* Remember the error: invalidate all subsequent fragments. */
  462. err = newerr;
  463. }
  464. if (skb_has_frag_list(skb) && !first_shinfo) {
  465. first_shinfo = skb_shinfo(skb);
  466. shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
  467. nr_frags = shinfo->nr_frags;
  468. goto check_frags;
  469. }
  470. *gopp_map = gop_map;
  471. return err;
  472. }
  473. static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
  474. {
  475. struct skb_shared_info *shinfo = skb_shinfo(skb);
  476. int nr_frags = shinfo->nr_frags;
  477. int i;
  478. u16 prev_pending_idx = INVALID_PENDING_IDX;
  479. for (i = 0; i < nr_frags; i++) {
  480. skb_frag_t *frag = shinfo->frags + i;
  481. struct xen_netif_tx_request *txp;
  482. struct page *page;
  483. u16 pending_idx;
  484. pending_idx = frag_get_pending_idx(frag);
  485. /* If this is not the first frag, chain it to the previous*/
  486. if (prev_pending_idx == INVALID_PENDING_IDX)
  487. skb_shinfo(skb)->destructor_arg =
  488. &callback_param(queue, pending_idx);
  489. else
  490. callback_param(queue, prev_pending_idx).ctx =
  491. &callback_param(queue, pending_idx);
  492. callback_param(queue, pending_idx).ctx = NULL;
  493. prev_pending_idx = pending_idx;
  494. txp = &queue->pending_tx_info[pending_idx].req;
  495. page = virt_to_page(idx_to_kaddr(queue, pending_idx));
  496. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  497. skb->len += txp->size;
  498. skb->data_len += txp->size;
  499. skb->truesize += txp->size;
  500. /* Take an extra reference to offset network stack's put_page */
  501. get_page(queue->mmap_pages[pending_idx]);
  502. }
  503. }
  504. static int xenvif_get_extras(struct xenvif_queue *queue,
  505. struct xen_netif_extra_info *extras,
  506. unsigned int *extra_count,
  507. int work_to_do)
  508. {
  509. struct xen_netif_extra_info extra;
  510. RING_IDX cons = queue->tx.req_cons;
  511. do {
  512. if (unlikely(work_to_do-- <= 0)) {
  513. netdev_err(queue->vif->dev, "Missing extra info\n");
  514. xenvif_fatal_tx_err(queue->vif);
  515. return -EBADR;
  516. }
  517. RING_COPY_REQUEST(&queue->tx, cons, &extra);
  518. queue->tx.req_cons = ++cons;
  519. (*extra_count)++;
  520. if (unlikely(!extra.type ||
  521. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  522. netdev_err(queue->vif->dev,
  523. "Invalid extra type: %d\n", extra.type);
  524. xenvif_fatal_tx_err(queue->vif);
  525. return -EINVAL;
  526. }
  527. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  528. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  529. return work_to_do;
  530. }
  531. static int xenvif_set_skb_gso(struct xenvif *vif,
  532. struct sk_buff *skb,
  533. struct xen_netif_extra_info *gso)
  534. {
  535. if (!gso->u.gso.size) {
  536. netdev_err(vif->dev, "GSO size must not be zero.\n");
  537. xenvif_fatal_tx_err(vif);
  538. return -EINVAL;
  539. }
  540. switch (gso->u.gso.type) {
  541. case XEN_NETIF_GSO_TYPE_TCPV4:
  542. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  543. break;
  544. case XEN_NETIF_GSO_TYPE_TCPV6:
  545. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  546. break;
  547. default:
  548. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  549. xenvif_fatal_tx_err(vif);
  550. return -EINVAL;
  551. }
  552. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  553. /* gso_segs will be calculated later */
  554. return 0;
  555. }
  556. static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
  557. {
  558. bool recalculate_partial_csum = false;
  559. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  560. * peers can fail to set NETRXF_csum_blank when sending a GSO
  561. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  562. * recalculate the partial checksum.
  563. */
  564. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  565. queue->stats.rx_gso_checksum_fixup++;
  566. skb->ip_summed = CHECKSUM_PARTIAL;
  567. recalculate_partial_csum = true;
  568. }
  569. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  570. if (skb->ip_summed != CHECKSUM_PARTIAL)
  571. return 0;
  572. return skb_checksum_setup(skb, recalculate_partial_csum);
  573. }
  574. static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
  575. {
  576. u64 now = get_jiffies_64();
  577. u64 next_credit = queue->credit_window_start +
  578. msecs_to_jiffies(queue->credit_usec / 1000);
  579. /* Timer could already be pending in rare cases. */
  580. if (timer_pending(&queue->credit_timeout)) {
  581. queue->rate_limited = true;
  582. return true;
  583. }
  584. /* Passed the point where we can replenish credit? */
  585. if (time_after_eq64(now, next_credit)) {
  586. queue->credit_window_start = now;
  587. tx_add_credit(queue);
  588. }
  589. /* Still too big to send right now? Set a callback. */
  590. if (size > queue->remaining_credit) {
  591. mod_timer(&queue->credit_timeout,
  592. next_credit);
  593. queue->credit_window_start = next_credit;
  594. queue->rate_limited = true;
  595. return true;
  596. }
  597. return false;
  598. }
  599. /* No locking is required in xenvif_mcast_add/del() as they are
  600. * only ever invoked from NAPI poll. An RCU list is used because
  601. * xenvif_mcast_match() is called asynchronously, during start_xmit.
  602. */
  603. static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
  604. {
  605. struct xenvif_mcast_addr *mcast;
  606. if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
  607. if (net_ratelimit())
  608. netdev_err(vif->dev,
  609. "Too many multicast addresses\n");
  610. return -ENOSPC;
  611. }
  612. mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
  613. if (!mcast)
  614. return -ENOMEM;
  615. ether_addr_copy(mcast->addr, addr);
  616. list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
  617. vif->fe_mcast_count++;
  618. return 0;
  619. }
  620. static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
  621. {
  622. struct xenvif_mcast_addr *mcast;
  623. list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
  624. if (ether_addr_equal(addr, mcast->addr)) {
  625. --vif->fe_mcast_count;
  626. list_del_rcu(&mcast->entry);
  627. kfree_rcu(mcast, rcu);
  628. break;
  629. }
  630. }
  631. }
  632. bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
  633. {
  634. struct xenvif_mcast_addr *mcast;
  635. rcu_read_lock();
  636. list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
  637. if (ether_addr_equal(addr, mcast->addr)) {
  638. rcu_read_unlock();
  639. return true;
  640. }
  641. }
  642. rcu_read_unlock();
  643. return false;
  644. }
  645. void xenvif_mcast_addr_list_free(struct xenvif *vif)
  646. {
  647. /* No need for locking or RCU here. NAPI poll and TX queue
  648. * are stopped.
  649. */
  650. while (!list_empty(&vif->fe_mcast_addr)) {
  651. struct xenvif_mcast_addr *mcast;
  652. mcast = list_first_entry(&vif->fe_mcast_addr,
  653. struct xenvif_mcast_addr,
  654. entry);
  655. --vif->fe_mcast_count;
  656. list_del(&mcast->entry);
  657. kfree(mcast);
  658. }
  659. }
  660. static void xenvif_tx_build_gops(struct xenvif_queue *queue,
  661. int budget,
  662. unsigned *copy_ops,
  663. unsigned *map_ops)
  664. {
  665. struct gnttab_map_grant_ref *gop = queue->tx_map_ops;
  666. struct sk_buff *skb, *nskb;
  667. int ret;
  668. unsigned int frag_overflow;
  669. while (skb_queue_len(&queue->tx_queue) < budget) {
  670. struct xen_netif_tx_request txreq;
  671. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  672. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  673. unsigned int extra_count;
  674. u16 pending_idx;
  675. RING_IDX idx;
  676. int work_to_do;
  677. unsigned int data_len;
  678. pending_ring_idx_t index;
  679. if (queue->tx.sring->req_prod - queue->tx.req_cons >
  680. XEN_NETIF_TX_RING_SIZE) {
  681. netdev_err(queue->vif->dev,
  682. "Impossible number of requests. "
  683. "req_prod %d, req_cons %d, size %ld\n",
  684. queue->tx.sring->req_prod, queue->tx.req_cons,
  685. XEN_NETIF_TX_RING_SIZE);
  686. xenvif_fatal_tx_err(queue->vif);
  687. break;
  688. }
  689. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
  690. if (!work_to_do)
  691. break;
  692. idx = queue->tx.req_cons;
  693. rmb(); /* Ensure that we see the request before we copy it. */
  694. RING_COPY_REQUEST(&queue->tx, idx, &txreq);
  695. /* Credit-based scheduling. */
  696. if (txreq.size > queue->remaining_credit &&
  697. tx_credit_exceeded(queue, txreq.size))
  698. break;
  699. queue->remaining_credit -= txreq.size;
  700. work_to_do--;
  701. queue->tx.req_cons = ++idx;
  702. memset(extras, 0, sizeof(extras));
  703. extra_count = 0;
  704. if (txreq.flags & XEN_NETTXF_extra_info) {
  705. work_to_do = xenvif_get_extras(queue, extras,
  706. &extra_count,
  707. work_to_do);
  708. idx = queue->tx.req_cons;
  709. if (unlikely(work_to_do < 0))
  710. break;
  711. }
  712. if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
  713. struct xen_netif_extra_info *extra;
  714. extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
  715. ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
  716. make_tx_response(queue, &txreq, extra_count,
  717. (ret == 0) ?
  718. XEN_NETIF_RSP_OKAY :
  719. XEN_NETIF_RSP_ERROR);
  720. push_tx_responses(queue);
  721. continue;
  722. }
  723. if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
  724. struct xen_netif_extra_info *extra;
  725. extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
  726. xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
  727. make_tx_response(queue, &txreq, extra_count,
  728. XEN_NETIF_RSP_OKAY);
  729. push_tx_responses(queue);
  730. continue;
  731. }
  732. ret = xenvif_count_requests(queue, &txreq, extra_count,
  733. txfrags, work_to_do);
  734. if (unlikely(ret < 0))
  735. break;
  736. idx += ret;
  737. if (unlikely(txreq.size < ETH_HLEN)) {
  738. netdev_dbg(queue->vif->dev,
  739. "Bad packet size: %d\n", txreq.size);
  740. xenvif_tx_err(queue, &txreq, extra_count, idx);
  741. break;
  742. }
  743. /* No crossing a page as the payload mustn't fragment. */
  744. if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
  745. netdev_err(queue->vif->dev,
  746. "txreq.offset: %u, size: %u, end: %lu\n",
  747. txreq.offset, txreq.size,
  748. (unsigned long)(txreq.offset&~XEN_PAGE_MASK) + txreq.size);
  749. xenvif_fatal_tx_err(queue->vif);
  750. break;
  751. }
  752. index = pending_index(queue->pending_cons);
  753. pending_idx = queue->pending_ring[index];
  754. data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
  755. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  756. XEN_NETBACK_TX_COPY_LEN : txreq.size;
  757. skb = xenvif_alloc_skb(data_len);
  758. if (unlikely(skb == NULL)) {
  759. netdev_dbg(queue->vif->dev,
  760. "Can't allocate a skb in start_xmit.\n");
  761. xenvif_tx_err(queue, &txreq, extra_count, idx);
  762. break;
  763. }
  764. skb_shinfo(skb)->nr_frags = ret;
  765. if (data_len < txreq.size)
  766. skb_shinfo(skb)->nr_frags++;
  767. /* At this point shinfo->nr_frags is in fact the number of
  768. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  769. */
  770. frag_overflow = 0;
  771. nskb = NULL;
  772. if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
  773. frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
  774. BUG_ON(frag_overflow > MAX_SKB_FRAGS);
  775. skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
  776. nskb = xenvif_alloc_skb(0);
  777. if (unlikely(nskb == NULL)) {
  778. skb_shinfo(skb)->nr_frags = 0;
  779. kfree_skb(skb);
  780. xenvif_tx_err(queue, &txreq, extra_count, idx);
  781. if (net_ratelimit())
  782. netdev_err(queue->vif->dev,
  783. "Can't allocate the frag_list skb.\n");
  784. break;
  785. }
  786. }
  787. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  788. struct xen_netif_extra_info *gso;
  789. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  790. if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
  791. /* Failure in xenvif_set_skb_gso is fatal. */
  792. skb_shinfo(skb)->nr_frags = 0;
  793. kfree_skb(skb);
  794. kfree_skb(nskb);
  795. break;
  796. }
  797. }
  798. if (extras[XEN_NETIF_EXTRA_TYPE_HASH - 1].type) {
  799. struct xen_netif_extra_info *extra;
  800. enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
  801. extra = &extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
  802. switch (extra->u.hash.type) {
  803. case _XEN_NETIF_CTRL_HASH_TYPE_IPV4:
  804. case _XEN_NETIF_CTRL_HASH_TYPE_IPV6:
  805. type = PKT_HASH_TYPE_L3;
  806. break;
  807. case _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP:
  808. case _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP:
  809. type = PKT_HASH_TYPE_L4;
  810. break;
  811. default:
  812. break;
  813. }
  814. if (type != PKT_HASH_TYPE_NONE)
  815. skb_set_hash(skb,
  816. *(u32 *)extra->u.hash.value,
  817. type);
  818. }
  819. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  820. __skb_put(skb, data_len);
  821. queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
  822. queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
  823. queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
  824. queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
  825. virt_to_gfn(skb->data);
  826. queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
  827. queue->tx_copy_ops[*copy_ops].dest.offset =
  828. offset_in_page(skb->data) & ~XEN_PAGE_MASK;
  829. queue->tx_copy_ops[*copy_ops].len = data_len;
  830. queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
  831. (*copy_ops)++;
  832. if (data_len < txreq.size) {
  833. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  834. pending_idx);
  835. xenvif_tx_create_map_op(queue, pending_idx, &txreq,
  836. extra_count, gop);
  837. gop++;
  838. } else {
  839. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  840. INVALID_PENDING_IDX);
  841. memcpy(&queue->pending_tx_info[pending_idx].req,
  842. &txreq, sizeof(txreq));
  843. queue->pending_tx_info[pending_idx].extra_count =
  844. extra_count;
  845. }
  846. queue->pending_cons++;
  847. gop = xenvif_get_requests(queue, skb, txfrags, gop,
  848. frag_overflow, nskb);
  849. __skb_queue_tail(&queue->tx_queue, skb);
  850. queue->tx.req_cons = idx;
  851. if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
  852. (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
  853. break;
  854. }
  855. (*map_ops) = gop - queue->tx_map_ops;
  856. return;
  857. }
  858. /* Consolidate skb with a frag_list into a brand new one with local pages on
  859. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  860. */
  861. static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
  862. {
  863. unsigned int offset = skb_headlen(skb);
  864. skb_frag_t frags[MAX_SKB_FRAGS];
  865. int i, f;
  866. struct ubuf_info *uarg;
  867. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  868. queue->stats.tx_zerocopy_sent += 2;
  869. queue->stats.tx_frag_overflow++;
  870. xenvif_fill_frags(queue, nskb);
  871. /* Subtract frags size, we will correct it later */
  872. skb->truesize -= skb->data_len;
  873. skb->len += nskb->len;
  874. skb->data_len += nskb->len;
  875. /* create a brand new frags array and coalesce there */
  876. for (i = 0; offset < skb->len; i++) {
  877. struct page *page;
  878. unsigned int len;
  879. BUG_ON(i >= MAX_SKB_FRAGS);
  880. page = alloc_page(GFP_ATOMIC);
  881. if (!page) {
  882. int j;
  883. skb->truesize += skb->data_len;
  884. for (j = 0; j < i; j++)
  885. put_page(skb_frag_page(&frags[j]));
  886. return -ENOMEM;
  887. }
  888. if (offset + PAGE_SIZE < skb->len)
  889. len = PAGE_SIZE;
  890. else
  891. len = skb->len - offset;
  892. if (skb_copy_bits(skb, offset, page_address(page), len))
  893. BUG();
  894. offset += len;
  895. __skb_frag_set_page(&frags[i], page);
  896. skb_frag_off_set(&frags[i], 0);
  897. skb_frag_size_set(&frags[i], len);
  898. }
  899. /* Release all the original (foreign) frags. */
  900. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  901. skb_frag_unref(skb, f);
  902. uarg = skb_shinfo(skb)->destructor_arg;
  903. /* increase inflight counter to offset decrement in callback */
  904. atomic_inc(&queue->inflight_packets);
  905. uarg->callback(uarg, true);
  906. skb_shinfo(skb)->destructor_arg = NULL;
  907. /* Fill the skb with the new (local) frags. */
  908. memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
  909. skb_shinfo(skb)->nr_frags = i;
  910. skb->truesize += i * PAGE_SIZE;
  911. return 0;
  912. }
  913. static int xenvif_tx_submit(struct xenvif_queue *queue)
  914. {
  915. struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
  916. struct gnttab_copy *gop_copy = queue->tx_copy_ops;
  917. struct sk_buff *skb;
  918. int work_done = 0;
  919. while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
  920. struct xen_netif_tx_request *txp;
  921. u16 pending_idx;
  922. unsigned data_len;
  923. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  924. txp = &queue->pending_tx_info[pending_idx].req;
  925. /* Check the remap error code. */
  926. if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
  927. /* If there was an error, xenvif_tx_check_gop is
  928. * expected to release all the frags which were mapped,
  929. * so kfree_skb shouldn't do it again
  930. */
  931. skb_shinfo(skb)->nr_frags = 0;
  932. if (skb_has_frag_list(skb)) {
  933. struct sk_buff *nskb =
  934. skb_shinfo(skb)->frag_list;
  935. skb_shinfo(nskb)->nr_frags = 0;
  936. }
  937. kfree_skb(skb);
  938. continue;
  939. }
  940. data_len = skb->len;
  941. callback_param(queue, pending_idx).ctx = NULL;
  942. if (data_len < txp->size) {
  943. /* Append the packet payload as a fragment. */
  944. txp->offset += data_len;
  945. txp->size -= data_len;
  946. } else {
  947. /* Schedule a response immediately. */
  948. xenvif_idx_release(queue, pending_idx,
  949. XEN_NETIF_RSP_OKAY);
  950. }
  951. if (txp->flags & XEN_NETTXF_csum_blank)
  952. skb->ip_summed = CHECKSUM_PARTIAL;
  953. else if (txp->flags & XEN_NETTXF_data_validated)
  954. skb->ip_summed = CHECKSUM_UNNECESSARY;
  955. xenvif_fill_frags(queue, skb);
  956. if (unlikely(skb_has_frag_list(skb))) {
  957. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  958. xenvif_skb_zerocopy_prepare(queue, nskb);
  959. if (xenvif_handle_frag_list(queue, skb)) {
  960. if (net_ratelimit())
  961. netdev_err(queue->vif->dev,
  962. "Not enough memory to consolidate frag_list!\n");
  963. xenvif_skb_zerocopy_prepare(queue, skb);
  964. kfree_skb(skb);
  965. continue;
  966. }
  967. /* Copied all the bits from the frag list -- free it. */
  968. skb_frag_list_init(skb);
  969. kfree_skb(nskb);
  970. }
  971. skb->dev = queue->vif->dev;
  972. skb->protocol = eth_type_trans(skb, skb->dev);
  973. skb_reset_network_header(skb);
  974. if (checksum_setup(queue, skb)) {
  975. netdev_dbg(queue->vif->dev,
  976. "Can't setup checksum in net_tx_action\n");
  977. /* We have to set this flag to trigger the callback */
  978. if (skb_shinfo(skb)->destructor_arg)
  979. xenvif_skb_zerocopy_prepare(queue, skb);
  980. kfree_skb(skb);
  981. continue;
  982. }
  983. skb_probe_transport_header(skb);
  984. /* If the packet is GSO then we will have just set up the
  985. * transport header offset in checksum_setup so it's now
  986. * straightforward to calculate gso_segs.
  987. */
  988. if (skb_is_gso(skb)) {
  989. int mss, hdrlen;
  990. /* GSO implies having the L4 header. */
  991. WARN_ON_ONCE(!skb_transport_header_was_set(skb));
  992. if (unlikely(!skb_transport_header_was_set(skb))) {
  993. kfree_skb(skb);
  994. continue;
  995. }
  996. mss = skb_shinfo(skb)->gso_size;
  997. hdrlen = skb_transport_header(skb) -
  998. skb_mac_header(skb) +
  999. tcp_hdrlen(skb);
  1000. skb_shinfo(skb)->gso_segs =
  1001. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1002. }
  1003. queue->stats.rx_bytes += skb->len;
  1004. queue->stats.rx_packets++;
  1005. work_done++;
  1006. /* Set this flag right before netif_receive_skb, otherwise
  1007. * someone might think this packet already left netback, and
  1008. * do a skb_copy_ubufs while we are still in control of the
  1009. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1010. */
  1011. if (skb_shinfo(skb)->destructor_arg) {
  1012. xenvif_skb_zerocopy_prepare(queue, skb);
  1013. queue->stats.tx_zerocopy_sent++;
  1014. }
  1015. netif_receive_skb(skb);
  1016. }
  1017. return work_done;
  1018. }
  1019. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1020. {
  1021. unsigned long flags;
  1022. pending_ring_idx_t index;
  1023. struct xenvif_queue *queue = ubuf_to_queue(ubuf);
  1024. /* This is the only place where we grab this lock, to protect callbacks
  1025. * from each other.
  1026. */
  1027. spin_lock_irqsave(&queue->callback_lock, flags);
  1028. do {
  1029. u16 pending_idx = ubuf->desc;
  1030. ubuf = (struct ubuf_info *) ubuf->ctx;
  1031. BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
  1032. MAX_PENDING_REQS);
  1033. index = pending_index(queue->dealloc_prod);
  1034. queue->dealloc_ring[index] = pending_idx;
  1035. /* Sync with xenvif_tx_dealloc_action:
  1036. * insert idx then incr producer.
  1037. */
  1038. smp_wmb();
  1039. queue->dealloc_prod++;
  1040. } while (ubuf);
  1041. spin_unlock_irqrestore(&queue->callback_lock, flags);
  1042. if (likely(zerocopy_success))
  1043. queue->stats.tx_zerocopy_success++;
  1044. else
  1045. queue->stats.tx_zerocopy_fail++;
  1046. xenvif_skb_zerocopy_complete(queue);
  1047. }
  1048. static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
  1049. {
  1050. struct gnttab_unmap_grant_ref *gop;
  1051. pending_ring_idx_t dc, dp;
  1052. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1053. unsigned int i = 0;
  1054. dc = queue->dealloc_cons;
  1055. gop = queue->tx_unmap_ops;
  1056. /* Free up any grants we have finished using */
  1057. do {
  1058. dp = queue->dealloc_prod;
  1059. /* Ensure we see all indices enqueued by all
  1060. * xenvif_zerocopy_callback().
  1061. */
  1062. smp_rmb();
  1063. while (dc != dp) {
  1064. BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
  1065. pending_idx =
  1066. queue->dealloc_ring[pending_index(dc++)];
  1067. pending_idx_release[gop - queue->tx_unmap_ops] =
  1068. pending_idx;
  1069. queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
  1070. queue->mmap_pages[pending_idx];
  1071. gnttab_set_unmap_op(gop,
  1072. idx_to_kaddr(queue, pending_idx),
  1073. GNTMAP_host_map,
  1074. queue->grant_tx_handle[pending_idx]);
  1075. xenvif_grant_handle_reset(queue, pending_idx);
  1076. ++gop;
  1077. }
  1078. } while (dp != queue->dealloc_prod);
  1079. queue->dealloc_cons = dc;
  1080. if (gop - queue->tx_unmap_ops > 0) {
  1081. int ret;
  1082. ret = gnttab_unmap_refs(queue->tx_unmap_ops,
  1083. NULL,
  1084. queue->pages_to_unmap,
  1085. gop - queue->tx_unmap_ops);
  1086. if (ret) {
  1087. netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
  1088. gop - queue->tx_unmap_ops, ret);
  1089. for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
  1090. if (gop[i].status != GNTST_okay)
  1091. netdev_err(queue->vif->dev,
  1092. " host_addr: 0x%llx handle: 0x%x status: %d\n",
  1093. gop[i].host_addr,
  1094. gop[i].handle,
  1095. gop[i].status);
  1096. }
  1097. BUG();
  1098. }
  1099. }
  1100. for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
  1101. xenvif_idx_release(queue, pending_idx_release[i],
  1102. XEN_NETIF_RSP_OKAY);
  1103. }
  1104. /* Called after netfront has transmitted */
  1105. int xenvif_tx_action(struct xenvif_queue *queue, int budget)
  1106. {
  1107. unsigned nr_mops, nr_cops = 0;
  1108. int work_done, ret;
  1109. if (unlikely(!tx_work_todo(queue)))
  1110. return 0;
  1111. xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
  1112. if (nr_cops == 0)
  1113. return 0;
  1114. gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
  1115. if (nr_mops != 0) {
  1116. ret = gnttab_map_refs(queue->tx_map_ops,
  1117. NULL,
  1118. queue->pages_to_map,
  1119. nr_mops);
  1120. BUG_ON(ret);
  1121. }
  1122. work_done = xenvif_tx_submit(queue);
  1123. return work_done;
  1124. }
  1125. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  1126. u8 status)
  1127. {
  1128. struct pending_tx_info *pending_tx_info;
  1129. pending_ring_idx_t index;
  1130. unsigned long flags;
  1131. pending_tx_info = &queue->pending_tx_info[pending_idx];
  1132. spin_lock_irqsave(&queue->response_lock, flags);
  1133. make_tx_response(queue, &pending_tx_info->req,
  1134. pending_tx_info->extra_count, status);
  1135. /* Release the pending index before pusing the Tx response so
  1136. * its available before a new Tx request is pushed by the
  1137. * frontend.
  1138. */
  1139. index = pending_index(queue->pending_prod++);
  1140. queue->pending_ring[index] = pending_idx;
  1141. push_tx_responses(queue);
  1142. spin_unlock_irqrestore(&queue->response_lock, flags);
  1143. }
  1144. static void make_tx_response(struct xenvif_queue *queue,
  1145. struct xen_netif_tx_request *txp,
  1146. unsigned int extra_count,
  1147. s8 st)
  1148. {
  1149. RING_IDX i = queue->tx.rsp_prod_pvt;
  1150. struct xen_netif_tx_response *resp;
  1151. resp = RING_GET_RESPONSE(&queue->tx, i);
  1152. resp->id = txp->id;
  1153. resp->status = st;
  1154. while (extra_count-- != 0)
  1155. RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1156. queue->tx.rsp_prod_pvt = ++i;
  1157. }
  1158. static void push_tx_responses(struct xenvif_queue *queue)
  1159. {
  1160. int notify;
  1161. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
  1162. if (notify)
  1163. notify_remote_via_irq(queue->tx_irq);
  1164. }
  1165. void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
  1166. {
  1167. int ret;
  1168. struct gnttab_unmap_grant_ref tx_unmap_op;
  1169. gnttab_set_unmap_op(&tx_unmap_op,
  1170. idx_to_kaddr(queue, pending_idx),
  1171. GNTMAP_host_map,
  1172. queue->grant_tx_handle[pending_idx]);
  1173. xenvif_grant_handle_reset(queue, pending_idx);
  1174. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1175. &queue->mmap_pages[pending_idx], 1);
  1176. if (ret) {
  1177. netdev_err(queue->vif->dev,
  1178. "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
  1179. ret,
  1180. pending_idx,
  1181. tx_unmap_op.host_addr,
  1182. tx_unmap_op.handle,
  1183. tx_unmap_op.status);
  1184. BUG();
  1185. }
  1186. }
  1187. static inline int tx_work_todo(struct xenvif_queue *queue)
  1188. {
  1189. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
  1190. return 1;
  1191. return 0;
  1192. }
  1193. static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
  1194. {
  1195. return queue->dealloc_cons != queue->dealloc_prod;
  1196. }
  1197. void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue)
  1198. {
  1199. if (queue->tx.sring)
  1200. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1201. queue->tx.sring);
  1202. if (queue->rx.sring)
  1203. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1204. queue->rx.sring);
  1205. }
  1206. int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
  1207. grant_ref_t tx_ring_ref,
  1208. grant_ref_t rx_ring_ref)
  1209. {
  1210. void *addr;
  1211. struct xen_netif_tx_sring *txs;
  1212. struct xen_netif_rx_sring *rxs;
  1213. RING_IDX rsp_prod, req_prod;
  1214. int err = -ENOMEM;
  1215. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1216. &tx_ring_ref, 1, &addr);
  1217. if (err)
  1218. goto err;
  1219. txs = (struct xen_netif_tx_sring *)addr;
  1220. rsp_prod = READ_ONCE(txs->rsp_prod);
  1221. req_prod = READ_ONCE(txs->req_prod);
  1222. BACK_RING_ATTACH(&queue->tx, txs, rsp_prod, XEN_PAGE_SIZE);
  1223. err = -EIO;
  1224. if (req_prod - rsp_prod > RING_SIZE(&queue->tx))
  1225. goto err;
  1226. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1227. &rx_ring_ref, 1, &addr);
  1228. if (err)
  1229. goto err;
  1230. rxs = (struct xen_netif_rx_sring *)addr;
  1231. rsp_prod = READ_ONCE(rxs->rsp_prod);
  1232. req_prod = READ_ONCE(rxs->req_prod);
  1233. BACK_RING_ATTACH(&queue->rx, rxs, rsp_prod, XEN_PAGE_SIZE);
  1234. err = -EIO;
  1235. if (req_prod - rsp_prod > RING_SIZE(&queue->rx))
  1236. goto err;
  1237. return 0;
  1238. err:
  1239. xenvif_unmap_frontend_data_rings(queue);
  1240. return err;
  1241. }
  1242. static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
  1243. {
  1244. /* Dealloc thread must remain running until all inflight
  1245. * packets complete.
  1246. */
  1247. return kthread_should_stop() &&
  1248. !atomic_read(&queue->inflight_packets);
  1249. }
  1250. int xenvif_dealloc_kthread(void *data)
  1251. {
  1252. struct xenvif_queue *queue = data;
  1253. for (;;) {
  1254. wait_event_interruptible(queue->dealloc_wq,
  1255. tx_dealloc_work_todo(queue) ||
  1256. xenvif_dealloc_kthread_should_stop(queue));
  1257. if (xenvif_dealloc_kthread_should_stop(queue))
  1258. break;
  1259. xenvif_tx_dealloc_action(queue);
  1260. cond_resched();
  1261. }
  1262. /* Unmap anything remaining*/
  1263. if (tx_dealloc_work_todo(queue))
  1264. xenvif_tx_dealloc_action(queue);
  1265. return 0;
  1266. }
  1267. static void make_ctrl_response(struct xenvif *vif,
  1268. const struct xen_netif_ctrl_request *req,
  1269. u32 status, u32 data)
  1270. {
  1271. RING_IDX idx = vif->ctrl.rsp_prod_pvt;
  1272. struct xen_netif_ctrl_response rsp = {
  1273. .id = req->id,
  1274. .type = req->type,
  1275. .status = status,
  1276. .data = data,
  1277. };
  1278. *RING_GET_RESPONSE(&vif->ctrl, idx) = rsp;
  1279. vif->ctrl.rsp_prod_pvt = ++idx;
  1280. }
  1281. static void push_ctrl_response(struct xenvif *vif)
  1282. {
  1283. int notify;
  1284. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->ctrl, notify);
  1285. if (notify)
  1286. notify_remote_via_irq(vif->ctrl_irq);
  1287. }
  1288. static void process_ctrl_request(struct xenvif *vif,
  1289. const struct xen_netif_ctrl_request *req)
  1290. {
  1291. u32 status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
  1292. u32 data = 0;
  1293. switch (req->type) {
  1294. case XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM:
  1295. status = xenvif_set_hash_alg(vif, req->data[0]);
  1296. break;
  1297. case XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS:
  1298. status = xenvif_get_hash_flags(vif, &data);
  1299. break;
  1300. case XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS:
  1301. status = xenvif_set_hash_flags(vif, req->data[0]);
  1302. break;
  1303. case XEN_NETIF_CTRL_TYPE_SET_HASH_KEY:
  1304. status = xenvif_set_hash_key(vif, req->data[0],
  1305. req->data[1]);
  1306. break;
  1307. case XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE:
  1308. status = XEN_NETIF_CTRL_STATUS_SUCCESS;
  1309. data = XEN_NETBK_MAX_HASH_MAPPING_SIZE;
  1310. break;
  1311. case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE:
  1312. status = xenvif_set_hash_mapping_size(vif,
  1313. req->data[0]);
  1314. break;
  1315. case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING:
  1316. status = xenvif_set_hash_mapping(vif, req->data[0],
  1317. req->data[1],
  1318. req->data[2]);
  1319. break;
  1320. default:
  1321. break;
  1322. }
  1323. make_ctrl_response(vif, req, status, data);
  1324. push_ctrl_response(vif);
  1325. }
  1326. static void xenvif_ctrl_action(struct xenvif *vif)
  1327. {
  1328. for (;;) {
  1329. RING_IDX req_prod, req_cons;
  1330. req_prod = vif->ctrl.sring->req_prod;
  1331. req_cons = vif->ctrl.req_cons;
  1332. /* Make sure we can see requests before we process them. */
  1333. rmb();
  1334. if (req_cons == req_prod)
  1335. break;
  1336. while (req_cons != req_prod) {
  1337. struct xen_netif_ctrl_request req;
  1338. RING_COPY_REQUEST(&vif->ctrl, req_cons, &req);
  1339. req_cons++;
  1340. process_ctrl_request(vif, &req);
  1341. }
  1342. vif->ctrl.req_cons = req_cons;
  1343. vif->ctrl.sring->req_event = req_cons + 1;
  1344. }
  1345. }
  1346. static bool xenvif_ctrl_work_todo(struct xenvif *vif)
  1347. {
  1348. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->ctrl)))
  1349. return true;
  1350. return false;
  1351. }
  1352. irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
  1353. {
  1354. struct xenvif *vif = data;
  1355. while (xenvif_ctrl_work_todo(vif))
  1356. xenvif_ctrl_action(vif);
  1357. return IRQ_HANDLED;
  1358. }
  1359. static int __init netback_init(void)
  1360. {
  1361. int rc = 0;
  1362. if (!xen_domain())
  1363. return -ENODEV;
  1364. /* Allow as many queues as there are CPUs but max. 8 if user has not
  1365. * specified a value.
  1366. */
  1367. if (xenvif_max_queues == 0)
  1368. xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
  1369. num_online_cpus());
  1370. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1371. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1372. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1373. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1374. }
  1375. rc = xenvif_xenbus_init();
  1376. if (rc)
  1377. goto failed_init;
  1378. #ifdef CONFIG_DEBUG_FS
  1379. xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
  1380. #endif /* CONFIG_DEBUG_FS */
  1381. return 0;
  1382. failed_init:
  1383. return rc;
  1384. }
  1385. module_init(netback_init);
  1386. static void __exit netback_fini(void)
  1387. {
  1388. #ifdef CONFIG_DEBUG_FS
  1389. debugfs_remove_recursive(xen_netback_dbg_root);
  1390. #endif /* CONFIG_DEBUG_FS */
  1391. xenvif_xenbus_fini();
  1392. }
  1393. module_exit(netback_fini);
  1394. MODULE_LICENSE("Dual BSD/GPL");
  1395. MODULE_ALIAS("xen-backend:vif");