netback.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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)frag->page_offset;
  120. }
  121. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  122. {
  123. frag->page_offset = 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. }
  154. void xenvif_tx_credit_callback(unsigned long data)
  155. {
  156. struct xenvif_queue *queue = (struct xenvif_queue *)data;
  157. tx_add_credit(queue);
  158. xenvif_napi_schedule_or_enable_events(queue);
  159. }
  160. static void xenvif_tx_err(struct xenvif_queue *queue,
  161. struct xen_netif_tx_request *txp,
  162. unsigned int extra_count, RING_IDX end)
  163. {
  164. RING_IDX cons = queue->tx.req_cons;
  165. unsigned long flags;
  166. do {
  167. spin_lock_irqsave(&queue->response_lock, flags);
  168. make_tx_response(queue, txp, extra_count, XEN_NETIF_RSP_ERROR);
  169. push_tx_responses(queue);
  170. spin_unlock_irqrestore(&queue->response_lock, flags);
  171. if (cons == end)
  172. break;
  173. RING_COPY_REQUEST(&queue->tx, cons++, txp);
  174. extra_count = 0; /* only the first frag can have extras */
  175. } while (1);
  176. queue->tx.req_cons = cons;
  177. }
  178. static void xenvif_fatal_tx_err(struct xenvif *vif)
  179. {
  180. netdev_err(vif->dev, "fatal error; disabling device\n");
  181. vif->disabled = true;
  182. /* Disable the vif from queue 0's kthread */
  183. if (vif->num_queues)
  184. xenvif_kick_thread(&vif->queues[0]);
  185. }
  186. static int xenvif_count_requests(struct xenvif_queue *queue,
  187. struct xen_netif_tx_request *first,
  188. unsigned int extra_count,
  189. struct xen_netif_tx_request *txp,
  190. int work_to_do)
  191. {
  192. RING_IDX cons = queue->tx.req_cons;
  193. int slots = 0;
  194. int drop_err = 0;
  195. int more_data;
  196. if (!(first->flags & XEN_NETTXF_more_data))
  197. return 0;
  198. do {
  199. struct xen_netif_tx_request dropped_tx = { 0 };
  200. if (slots >= work_to_do) {
  201. netdev_err(queue->vif->dev,
  202. "Asked for %d slots but exceeds this limit\n",
  203. work_to_do);
  204. xenvif_fatal_tx_err(queue->vif);
  205. return -ENODATA;
  206. }
  207. /* This guest is really using too many slots and
  208. * considered malicious.
  209. */
  210. if (unlikely(slots >= fatal_skb_slots)) {
  211. netdev_err(queue->vif->dev,
  212. "Malicious frontend using %d slots, threshold %u\n",
  213. slots, fatal_skb_slots);
  214. xenvif_fatal_tx_err(queue->vif);
  215. return -E2BIG;
  216. }
  217. /* Xen network protocol had implicit dependency on
  218. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  219. * the historical MAX_SKB_FRAGS value 18 to honor the
  220. * same behavior as before. Any packet using more than
  221. * 18 slots but less than fatal_skb_slots slots is
  222. * dropped
  223. */
  224. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  225. if (net_ratelimit())
  226. netdev_dbg(queue->vif->dev,
  227. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  228. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  229. drop_err = -E2BIG;
  230. }
  231. if (drop_err)
  232. txp = &dropped_tx;
  233. RING_COPY_REQUEST(&queue->tx, cons + slots, txp);
  234. /* If the guest submitted a frame >= 64 KiB then
  235. * first->size overflowed and following slots will
  236. * appear to be larger than the frame.
  237. *
  238. * This cannot be fatal error as there are buggy
  239. * frontends that do this.
  240. *
  241. * Consume all slots and drop the packet.
  242. */
  243. if (!drop_err && txp->size > first->size) {
  244. if (net_ratelimit())
  245. netdev_dbg(queue->vif->dev,
  246. "Invalid tx request, slot size %u > remaining size %u\n",
  247. txp->size, first->size);
  248. drop_err = -EIO;
  249. }
  250. first->size -= txp->size;
  251. slots++;
  252. if (unlikely((txp->offset + txp->size) > XEN_PAGE_SIZE)) {
  253. netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
  254. txp->offset, txp->size);
  255. xenvif_fatal_tx_err(queue->vif);
  256. return -EINVAL;
  257. }
  258. more_data = txp->flags & XEN_NETTXF_more_data;
  259. if (!drop_err)
  260. txp++;
  261. } while (more_data);
  262. if (drop_err) {
  263. xenvif_tx_err(queue, first, extra_count, cons + slots);
  264. return drop_err;
  265. }
  266. return slots;
  267. }
  268. struct xenvif_tx_cb {
  269. u16 pending_idx;
  270. };
  271. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  272. static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
  273. u16 pending_idx,
  274. struct xen_netif_tx_request *txp,
  275. unsigned int extra_count,
  276. struct gnttab_map_grant_ref *mop)
  277. {
  278. queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
  279. gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
  280. GNTMAP_host_map | GNTMAP_readonly,
  281. txp->gref, queue->vif->domid);
  282. memcpy(&queue->pending_tx_info[pending_idx].req, txp,
  283. sizeof(*txp));
  284. queue->pending_tx_info[pending_idx].extra_count = extra_count;
  285. }
  286. static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
  287. {
  288. struct sk_buff *skb =
  289. alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
  290. GFP_ATOMIC | __GFP_NOWARN);
  291. if (unlikely(skb == NULL))
  292. return NULL;
  293. /* Packets passed to netif_rx() must have some headroom. */
  294. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  295. /* Initialize it here to avoid later surprises */
  296. skb_shinfo(skb)->destructor_arg = NULL;
  297. return skb;
  298. }
  299. static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
  300. struct sk_buff *skb,
  301. struct xen_netif_tx_request *txp,
  302. struct gnttab_map_grant_ref *gop,
  303. unsigned int frag_overflow,
  304. struct sk_buff *nskb)
  305. {
  306. struct skb_shared_info *shinfo = skb_shinfo(skb);
  307. skb_frag_t *frags = shinfo->frags;
  308. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  309. int start;
  310. pending_ring_idx_t index;
  311. unsigned int nr_slots;
  312. nr_slots = shinfo->nr_frags;
  313. /* Skip first skb fragment if it is on same page as header fragment. */
  314. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  315. for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
  316. shinfo->nr_frags++, txp++, gop++) {
  317. index = pending_index(queue->pending_cons++);
  318. pending_idx = queue->pending_ring[index];
  319. xenvif_tx_create_map_op(queue, pending_idx, txp, 0, gop);
  320. frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
  321. }
  322. if (frag_overflow) {
  323. shinfo = skb_shinfo(nskb);
  324. frags = shinfo->frags;
  325. for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
  326. shinfo->nr_frags++, txp++, gop++) {
  327. index = pending_index(queue->pending_cons++);
  328. pending_idx = queue->pending_ring[index];
  329. xenvif_tx_create_map_op(queue, pending_idx, txp, 0,
  330. gop);
  331. frag_set_pending_idx(&frags[shinfo->nr_frags],
  332. pending_idx);
  333. }
  334. skb_shinfo(skb)->frag_list = nskb;
  335. }
  336. return gop;
  337. }
  338. static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
  339. u16 pending_idx,
  340. grant_handle_t handle)
  341. {
  342. if (unlikely(queue->grant_tx_handle[pending_idx] !=
  343. NETBACK_INVALID_HANDLE)) {
  344. netdev_err(queue->vif->dev,
  345. "Trying to overwrite active handle! pending_idx: 0x%x\n",
  346. pending_idx);
  347. BUG();
  348. }
  349. queue->grant_tx_handle[pending_idx] = handle;
  350. }
  351. static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
  352. u16 pending_idx)
  353. {
  354. if (unlikely(queue->grant_tx_handle[pending_idx] ==
  355. NETBACK_INVALID_HANDLE)) {
  356. netdev_err(queue->vif->dev,
  357. "Trying to unmap invalid handle! pending_idx: 0x%x\n",
  358. pending_idx);
  359. BUG();
  360. }
  361. queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
  362. }
  363. static int xenvif_tx_check_gop(struct xenvif_queue *queue,
  364. struct sk_buff *skb,
  365. struct gnttab_map_grant_ref **gopp_map,
  366. struct gnttab_copy **gopp_copy)
  367. {
  368. struct gnttab_map_grant_ref *gop_map = *gopp_map;
  369. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  370. /* This always points to the shinfo of the skb being checked, which
  371. * could be either the first or the one on the frag_list
  372. */
  373. struct skb_shared_info *shinfo = skb_shinfo(skb);
  374. /* If this is non-NULL, we are currently checking the frag_list skb, and
  375. * this points to the shinfo of the first one
  376. */
  377. struct skb_shared_info *first_shinfo = NULL;
  378. int nr_frags = shinfo->nr_frags;
  379. const bool sharedslot = nr_frags &&
  380. frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
  381. int i, err;
  382. /* Check status of header. */
  383. err = (*gopp_copy)->status;
  384. if (unlikely(err)) {
  385. if (net_ratelimit())
  386. netdev_dbg(queue->vif->dev,
  387. "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
  388. (*gopp_copy)->status,
  389. pending_idx,
  390. (*gopp_copy)->source.u.ref);
  391. /* The first frag might still have this slot mapped */
  392. if (!sharedslot)
  393. xenvif_idx_release(queue, pending_idx,
  394. XEN_NETIF_RSP_ERROR);
  395. }
  396. (*gopp_copy)++;
  397. check_frags:
  398. for (i = 0; i < nr_frags; i++, gop_map++) {
  399. int j, newerr;
  400. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  401. /* Check error status: if okay then remember grant handle. */
  402. newerr = gop_map->status;
  403. if (likely(!newerr)) {
  404. xenvif_grant_handle_set(queue,
  405. pending_idx,
  406. gop_map->handle);
  407. /* Had a previous error? Invalidate this fragment. */
  408. if (unlikely(err)) {
  409. xenvif_idx_unmap(queue, pending_idx);
  410. /* If the mapping of the first frag was OK, but
  411. * the header's copy failed, and they are
  412. * sharing a slot, send an error
  413. */
  414. if (i == 0 && sharedslot)
  415. xenvif_idx_release(queue, pending_idx,
  416. XEN_NETIF_RSP_ERROR);
  417. else
  418. xenvif_idx_release(queue, pending_idx,
  419. XEN_NETIF_RSP_OKAY);
  420. }
  421. continue;
  422. }
  423. /* Error on this fragment: respond to client with an error. */
  424. if (net_ratelimit())
  425. netdev_dbg(queue->vif->dev,
  426. "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
  427. i,
  428. gop_map->status,
  429. pending_idx,
  430. gop_map->ref);
  431. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
  432. /* Not the first error? Preceding frags already invalidated. */
  433. if (err)
  434. continue;
  435. /* First error: if the header haven't shared a slot with the
  436. * first frag, release it as well.
  437. */
  438. if (!sharedslot)
  439. xenvif_idx_release(queue,
  440. XENVIF_TX_CB(skb)->pending_idx,
  441. XEN_NETIF_RSP_OKAY);
  442. /* Invalidate preceding fragments of this skb. */
  443. for (j = 0; j < i; j++) {
  444. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  445. xenvif_idx_unmap(queue, pending_idx);
  446. xenvif_idx_release(queue, pending_idx,
  447. XEN_NETIF_RSP_OKAY);
  448. }
  449. /* And if we found the error while checking the frag_list, unmap
  450. * the first skb's frags
  451. */
  452. if (first_shinfo) {
  453. for (j = 0; j < first_shinfo->nr_frags; j++) {
  454. pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
  455. xenvif_idx_unmap(queue, pending_idx);
  456. xenvif_idx_release(queue, pending_idx,
  457. XEN_NETIF_RSP_OKAY);
  458. }
  459. }
  460. /* Remember the error: invalidate all subsequent fragments. */
  461. err = newerr;
  462. }
  463. if (skb_has_frag_list(skb) && !first_shinfo) {
  464. first_shinfo = skb_shinfo(skb);
  465. shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
  466. nr_frags = shinfo->nr_frags;
  467. goto check_frags;
  468. }
  469. *gopp_map = gop_map;
  470. return err;
  471. }
  472. static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
  473. {
  474. struct skb_shared_info *shinfo = skb_shinfo(skb);
  475. int nr_frags = shinfo->nr_frags;
  476. int i;
  477. u16 prev_pending_idx = INVALID_PENDING_IDX;
  478. for (i = 0; i < nr_frags; i++) {
  479. skb_frag_t *frag = shinfo->frags + i;
  480. struct xen_netif_tx_request *txp;
  481. struct page *page;
  482. u16 pending_idx;
  483. pending_idx = frag_get_pending_idx(frag);
  484. /* If this is not the first frag, chain it to the previous*/
  485. if (prev_pending_idx == INVALID_PENDING_IDX)
  486. skb_shinfo(skb)->destructor_arg =
  487. &callback_param(queue, pending_idx);
  488. else
  489. callback_param(queue, prev_pending_idx).ctx =
  490. &callback_param(queue, pending_idx);
  491. callback_param(queue, pending_idx).ctx = NULL;
  492. prev_pending_idx = pending_idx;
  493. txp = &queue->pending_tx_info[pending_idx].req;
  494. page = virt_to_page(idx_to_kaddr(queue, pending_idx));
  495. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  496. skb->len += txp->size;
  497. skb->data_len += txp->size;
  498. skb->truesize += txp->size;
  499. /* Take an extra reference to offset network stack's put_page */
  500. get_page(queue->mmap_pages[pending_idx]);
  501. }
  502. }
  503. static int xenvif_get_extras(struct xenvif_queue *queue,
  504. struct xen_netif_extra_info *extras,
  505. unsigned int *extra_count,
  506. int work_to_do)
  507. {
  508. struct xen_netif_extra_info extra;
  509. RING_IDX cons = queue->tx.req_cons;
  510. do {
  511. if (unlikely(work_to_do-- <= 0)) {
  512. netdev_err(queue->vif->dev, "Missing extra info\n");
  513. xenvif_fatal_tx_err(queue->vif);
  514. return -EBADR;
  515. }
  516. RING_COPY_REQUEST(&queue->tx, cons, &extra);
  517. queue->tx.req_cons = ++cons;
  518. (*extra_count)++;
  519. if (unlikely(!extra.type ||
  520. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  521. netdev_err(queue->vif->dev,
  522. "Invalid extra type: %d\n", extra.type);
  523. xenvif_fatal_tx_err(queue->vif);
  524. return -EINVAL;
  525. }
  526. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  527. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  528. return work_to_do;
  529. }
  530. static int xenvif_set_skb_gso(struct xenvif *vif,
  531. struct sk_buff *skb,
  532. struct xen_netif_extra_info *gso)
  533. {
  534. if (!gso->u.gso.size) {
  535. netdev_err(vif->dev, "GSO size must not be zero.\n");
  536. xenvif_fatal_tx_err(vif);
  537. return -EINVAL;
  538. }
  539. switch (gso->u.gso.type) {
  540. case XEN_NETIF_GSO_TYPE_TCPV4:
  541. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  542. break;
  543. case XEN_NETIF_GSO_TYPE_TCPV6:
  544. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  545. break;
  546. default:
  547. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  548. xenvif_fatal_tx_err(vif);
  549. return -EINVAL;
  550. }
  551. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  552. /* gso_segs will be calculated later */
  553. return 0;
  554. }
  555. static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
  556. {
  557. bool recalculate_partial_csum = false;
  558. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  559. * peers can fail to set NETRXF_csum_blank when sending a GSO
  560. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  561. * recalculate the partial checksum.
  562. */
  563. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  564. queue->stats.rx_gso_checksum_fixup++;
  565. skb->ip_summed = CHECKSUM_PARTIAL;
  566. recalculate_partial_csum = true;
  567. }
  568. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  569. if (skb->ip_summed != CHECKSUM_PARTIAL)
  570. return 0;
  571. return skb_checksum_setup(skb, recalculate_partial_csum);
  572. }
  573. static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
  574. {
  575. u64 now = get_jiffies_64();
  576. u64 next_credit = queue->credit_window_start +
  577. msecs_to_jiffies(queue->credit_usec / 1000);
  578. /* Timer could already be pending in rare cases. */
  579. if (timer_pending(&queue->credit_timeout))
  580. return true;
  581. /* Passed the point where we can replenish credit? */
  582. if (time_after_eq64(now, next_credit)) {
  583. queue->credit_window_start = now;
  584. tx_add_credit(queue);
  585. }
  586. /* Still too big to send right now? Set a callback. */
  587. if (size > queue->remaining_credit) {
  588. queue->credit_timeout.data =
  589. (unsigned long)queue;
  590. mod_timer(&queue->credit_timeout,
  591. next_credit);
  592. queue->credit_window_start = next_credit;
  593. return true;
  594. }
  595. return false;
  596. }
  597. /* No locking is required in xenvif_mcast_add/del() as they are
  598. * only ever invoked from NAPI poll. An RCU list is used because
  599. * xenvif_mcast_match() is called asynchronously, during start_xmit.
  600. */
  601. static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
  602. {
  603. struct xenvif_mcast_addr *mcast;
  604. if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
  605. if (net_ratelimit())
  606. netdev_err(vif->dev,
  607. "Too many multicast addresses\n");
  608. return -ENOSPC;
  609. }
  610. mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
  611. if (!mcast)
  612. return -ENOMEM;
  613. ether_addr_copy(mcast->addr, addr);
  614. list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
  615. vif->fe_mcast_count++;
  616. return 0;
  617. }
  618. static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
  619. {
  620. struct xenvif_mcast_addr *mcast;
  621. list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
  622. if (ether_addr_equal(addr, mcast->addr)) {
  623. --vif->fe_mcast_count;
  624. list_del_rcu(&mcast->entry);
  625. kfree_rcu(mcast, rcu);
  626. break;
  627. }
  628. }
  629. }
  630. bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
  631. {
  632. struct xenvif_mcast_addr *mcast;
  633. rcu_read_lock();
  634. list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
  635. if (ether_addr_equal(addr, mcast->addr)) {
  636. rcu_read_unlock();
  637. return true;
  638. }
  639. }
  640. rcu_read_unlock();
  641. return false;
  642. }
  643. void xenvif_mcast_addr_list_free(struct xenvif *vif)
  644. {
  645. /* No need for locking or RCU here. NAPI poll and TX queue
  646. * are stopped.
  647. */
  648. while (!list_empty(&vif->fe_mcast_addr)) {
  649. struct xenvif_mcast_addr *mcast;
  650. mcast = list_first_entry(&vif->fe_mcast_addr,
  651. struct xenvif_mcast_addr,
  652. entry);
  653. --vif->fe_mcast_count;
  654. list_del(&mcast->entry);
  655. kfree(mcast);
  656. }
  657. }
  658. static void xenvif_tx_build_gops(struct xenvif_queue *queue,
  659. int budget,
  660. unsigned *copy_ops,
  661. unsigned *map_ops)
  662. {
  663. struct gnttab_map_grant_ref *gop = queue->tx_map_ops;
  664. struct sk_buff *skb, *nskb;
  665. int ret;
  666. unsigned int frag_overflow;
  667. while (skb_queue_len(&queue->tx_queue) < budget) {
  668. struct xen_netif_tx_request txreq;
  669. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  670. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  671. unsigned int extra_count;
  672. u16 pending_idx;
  673. RING_IDX idx;
  674. int work_to_do;
  675. unsigned int data_len;
  676. pending_ring_idx_t index;
  677. if (queue->tx.sring->req_prod - queue->tx.req_cons >
  678. XEN_NETIF_TX_RING_SIZE) {
  679. netdev_err(queue->vif->dev,
  680. "Impossible number of requests. "
  681. "req_prod %d, req_cons %d, size %ld\n",
  682. queue->tx.sring->req_prod, queue->tx.req_cons,
  683. XEN_NETIF_TX_RING_SIZE);
  684. xenvif_fatal_tx_err(queue->vif);
  685. break;
  686. }
  687. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
  688. if (!work_to_do)
  689. break;
  690. idx = queue->tx.req_cons;
  691. rmb(); /* Ensure that we see the request before we copy it. */
  692. RING_COPY_REQUEST(&queue->tx, idx, &txreq);
  693. /* Credit-based scheduling. */
  694. if (txreq.size > queue->remaining_credit &&
  695. tx_credit_exceeded(queue, txreq.size))
  696. break;
  697. queue->remaining_credit -= txreq.size;
  698. work_to_do--;
  699. queue->tx.req_cons = ++idx;
  700. memset(extras, 0, sizeof(extras));
  701. extra_count = 0;
  702. if (txreq.flags & XEN_NETTXF_extra_info) {
  703. work_to_do = xenvif_get_extras(queue, extras,
  704. &extra_count,
  705. work_to_do);
  706. idx = queue->tx.req_cons;
  707. if (unlikely(work_to_do < 0))
  708. break;
  709. }
  710. if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
  711. struct xen_netif_extra_info *extra;
  712. extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
  713. ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
  714. make_tx_response(queue, &txreq, extra_count,
  715. (ret == 0) ?
  716. XEN_NETIF_RSP_OKAY :
  717. XEN_NETIF_RSP_ERROR);
  718. push_tx_responses(queue);
  719. continue;
  720. }
  721. if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
  722. struct xen_netif_extra_info *extra;
  723. extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
  724. xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
  725. make_tx_response(queue, &txreq, extra_count,
  726. XEN_NETIF_RSP_OKAY);
  727. push_tx_responses(queue);
  728. continue;
  729. }
  730. ret = xenvif_count_requests(queue, &txreq, extra_count,
  731. txfrags, work_to_do);
  732. if (unlikely(ret < 0))
  733. break;
  734. idx += ret;
  735. if (unlikely(txreq.size < ETH_HLEN)) {
  736. netdev_dbg(queue->vif->dev,
  737. "Bad packet size: %d\n", txreq.size);
  738. xenvif_tx_err(queue, &txreq, extra_count, idx);
  739. break;
  740. }
  741. /* No crossing a page as the payload mustn't fragment. */
  742. if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
  743. netdev_err(queue->vif->dev,
  744. "txreq.offset: %u, size: %u, end: %lu\n",
  745. txreq.offset, txreq.size,
  746. (unsigned long)(txreq.offset&~XEN_PAGE_MASK) + txreq.size);
  747. xenvif_fatal_tx_err(queue->vif);
  748. break;
  749. }
  750. index = pending_index(queue->pending_cons);
  751. pending_idx = queue->pending_ring[index];
  752. data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
  753. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  754. XEN_NETBACK_TX_COPY_LEN : txreq.size;
  755. skb = xenvif_alloc_skb(data_len);
  756. if (unlikely(skb == NULL)) {
  757. netdev_dbg(queue->vif->dev,
  758. "Can't allocate a skb in start_xmit.\n");
  759. xenvif_tx_err(queue, &txreq, extra_count, idx);
  760. break;
  761. }
  762. skb_shinfo(skb)->nr_frags = ret;
  763. if (data_len < txreq.size)
  764. skb_shinfo(skb)->nr_frags++;
  765. /* At this point shinfo->nr_frags is in fact the number of
  766. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  767. */
  768. frag_overflow = 0;
  769. nskb = NULL;
  770. if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
  771. frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
  772. BUG_ON(frag_overflow > MAX_SKB_FRAGS);
  773. skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
  774. nskb = xenvif_alloc_skb(0);
  775. if (unlikely(nskb == NULL)) {
  776. kfree_skb(skb);
  777. xenvif_tx_err(queue, &txreq, extra_count, idx);
  778. if (net_ratelimit())
  779. netdev_err(queue->vif->dev,
  780. "Can't allocate the frag_list skb.\n");
  781. break;
  782. }
  783. }
  784. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  785. struct xen_netif_extra_info *gso;
  786. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  787. if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
  788. /* Failure in xenvif_set_skb_gso is fatal. */
  789. kfree_skb(skb);
  790. kfree_skb(nskb);
  791. break;
  792. }
  793. }
  794. if (extras[XEN_NETIF_EXTRA_TYPE_HASH - 1].type) {
  795. struct xen_netif_extra_info *extra;
  796. enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
  797. extra = &extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
  798. switch (extra->u.hash.type) {
  799. case _XEN_NETIF_CTRL_HASH_TYPE_IPV4:
  800. case _XEN_NETIF_CTRL_HASH_TYPE_IPV6:
  801. type = PKT_HASH_TYPE_L3;
  802. break;
  803. case _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP:
  804. case _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP:
  805. type = PKT_HASH_TYPE_L4;
  806. break;
  807. default:
  808. break;
  809. }
  810. if (type != PKT_HASH_TYPE_NONE)
  811. skb_set_hash(skb,
  812. *(u32 *)extra->u.hash.value,
  813. type);
  814. }
  815. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  816. __skb_put(skb, data_len);
  817. queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
  818. queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
  819. queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
  820. queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
  821. virt_to_gfn(skb->data);
  822. queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
  823. queue->tx_copy_ops[*copy_ops].dest.offset =
  824. offset_in_page(skb->data) & ~XEN_PAGE_MASK;
  825. queue->tx_copy_ops[*copy_ops].len = data_len;
  826. queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
  827. (*copy_ops)++;
  828. if (data_len < txreq.size) {
  829. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  830. pending_idx);
  831. xenvif_tx_create_map_op(queue, pending_idx, &txreq,
  832. extra_count, gop);
  833. gop++;
  834. } else {
  835. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  836. INVALID_PENDING_IDX);
  837. memcpy(&queue->pending_tx_info[pending_idx].req,
  838. &txreq, sizeof(txreq));
  839. queue->pending_tx_info[pending_idx].extra_count =
  840. extra_count;
  841. }
  842. queue->pending_cons++;
  843. gop = xenvif_get_requests(queue, skb, txfrags, gop,
  844. frag_overflow, nskb);
  845. __skb_queue_tail(&queue->tx_queue, skb);
  846. queue->tx.req_cons = idx;
  847. if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
  848. (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
  849. break;
  850. }
  851. (*map_ops) = gop - queue->tx_map_ops;
  852. return;
  853. }
  854. /* Consolidate skb with a frag_list into a brand new one with local pages on
  855. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  856. */
  857. static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
  858. {
  859. unsigned int offset = skb_headlen(skb);
  860. skb_frag_t frags[MAX_SKB_FRAGS];
  861. int i, f;
  862. struct ubuf_info *uarg;
  863. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  864. queue->stats.tx_zerocopy_sent += 2;
  865. queue->stats.tx_frag_overflow++;
  866. xenvif_fill_frags(queue, nskb);
  867. /* Subtract frags size, we will correct it later */
  868. skb->truesize -= skb->data_len;
  869. skb->len += nskb->len;
  870. skb->data_len += nskb->len;
  871. /* create a brand new frags array and coalesce there */
  872. for (i = 0; offset < skb->len; i++) {
  873. struct page *page;
  874. unsigned int len;
  875. BUG_ON(i >= MAX_SKB_FRAGS);
  876. page = alloc_page(GFP_ATOMIC);
  877. if (!page) {
  878. int j;
  879. skb->truesize += skb->data_len;
  880. for (j = 0; j < i; j++)
  881. put_page(frags[j].page.p);
  882. return -ENOMEM;
  883. }
  884. if (offset + PAGE_SIZE < skb->len)
  885. len = PAGE_SIZE;
  886. else
  887. len = skb->len - offset;
  888. if (skb_copy_bits(skb, offset, page_address(page), len))
  889. BUG();
  890. offset += len;
  891. frags[i].page.p = page;
  892. frags[i].page_offset = 0;
  893. skb_frag_size_set(&frags[i], len);
  894. }
  895. /* Copied all the bits from the frag list -- free it. */
  896. skb_frag_list_init(skb);
  897. xenvif_skb_zerocopy_prepare(queue, nskb);
  898. kfree_skb(nskb);
  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. if (xenvif_handle_frag_list(queue, skb)) {
  958. if (net_ratelimit())
  959. netdev_err(queue->vif->dev,
  960. "Not enough memory to consolidate frag_list!\n");
  961. xenvif_skb_zerocopy_prepare(queue, skb);
  962. kfree_skb(skb);
  963. continue;
  964. }
  965. }
  966. skb->dev = queue->vif->dev;
  967. skb->protocol = eth_type_trans(skb, skb->dev);
  968. skb_reset_network_header(skb);
  969. if (checksum_setup(queue, skb)) {
  970. netdev_dbg(queue->vif->dev,
  971. "Can't setup checksum in net_tx_action\n");
  972. /* We have to set this flag to trigger the callback */
  973. if (skb_shinfo(skb)->destructor_arg)
  974. xenvif_skb_zerocopy_prepare(queue, skb);
  975. kfree_skb(skb);
  976. continue;
  977. }
  978. skb_probe_transport_header(skb, 0);
  979. /* If the packet is GSO then we will have just set up the
  980. * transport header offset in checksum_setup so it's now
  981. * straightforward to calculate gso_segs.
  982. */
  983. if (skb_is_gso(skb)) {
  984. int mss = skb_shinfo(skb)->gso_size;
  985. int hdrlen = skb_transport_header(skb) -
  986. skb_mac_header(skb) +
  987. tcp_hdrlen(skb);
  988. skb_shinfo(skb)->gso_segs =
  989. DIV_ROUND_UP(skb->len - hdrlen, mss);
  990. }
  991. queue->stats.rx_bytes += skb->len;
  992. queue->stats.rx_packets++;
  993. work_done++;
  994. /* Set this flag right before netif_receive_skb, otherwise
  995. * someone might think this packet already left netback, and
  996. * do a skb_copy_ubufs while we are still in control of the
  997. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  998. */
  999. if (skb_shinfo(skb)->destructor_arg) {
  1000. xenvif_skb_zerocopy_prepare(queue, skb);
  1001. queue->stats.tx_zerocopy_sent++;
  1002. }
  1003. netif_receive_skb(skb);
  1004. }
  1005. return work_done;
  1006. }
  1007. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1008. {
  1009. unsigned long flags;
  1010. pending_ring_idx_t index;
  1011. struct xenvif_queue *queue = ubuf_to_queue(ubuf);
  1012. /* This is the only place where we grab this lock, to protect callbacks
  1013. * from each other.
  1014. */
  1015. spin_lock_irqsave(&queue->callback_lock, flags);
  1016. do {
  1017. u16 pending_idx = ubuf->desc;
  1018. ubuf = (struct ubuf_info *) ubuf->ctx;
  1019. BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
  1020. MAX_PENDING_REQS);
  1021. index = pending_index(queue->dealloc_prod);
  1022. queue->dealloc_ring[index] = pending_idx;
  1023. /* Sync with xenvif_tx_dealloc_action:
  1024. * insert idx then incr producer.
  1025. */
  1026. smp_wmb();
  1027. queue->dealloc_prod++;
  1028. } while (ubuf);
  1029. spin_unlock_irqrestore(&queue->callback_lock, flags);
  1030. if (likely(zerocopy_success))
  1031. queue->stats.tx_zerocopy_success++;
  1032. else
  1033. queue->stats.tx_zerocopy_fail++;
  1034. xenvif_skb_zerocopy_complete(queue);
  1035. }
  1036. static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
  1037. {
  1038. struct gnttab_unmap_grant_ref *gop;
  1039. pending_ring_idx_t dc, dp;
  1040. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1041. unsigned int i = 0;
  1042. dc = queue->dealloc_cons;
  1043. gop = queue->tx_unmap_ops;
  1044. /* Free up any grants we have finished using */
  1045. do {
  1046. dp = queue->dealloc_prod;
  1047. /* Ensure we see all indices enqueued by all
  1048. * xenvif_zerocopy_callback().
  1049. */
  1050. smp_rmb();
  1051. while (dc != dp) {
  1052. BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
  1053. pending_idx =
  1054. queue->dealloc_ring[pending_index(dc++)];
  1055. pending_idx_release[gop - queue->tx_unmap_ops] =
  1056. pending_idx;
  1057. queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
  1058. queue->mmap_pages[pending_idx];
  1059. gnttab_set_unmap_op(gop,
  1060. idx_to_kaddr(queue, pending_idx),
  1061. GNTMAP_host_map,
  1062. queue->grant_tx_handle[pending_idx]);
  1063. xenvif_grant_handle_reset(queue, pending_idx);
  1064. ++gop;
  1065. }
  1066. } while (dp != queue->dealloc_prod);
  1067. queue->dealloc_cons = dc;
  1068. if (gop - queue->tx_unmap_ops > 0) {
  1069. int ret;
  1070. ret = gnttab_unmap_refs(queue->tx_unmap_ops,
  1071. NULL,
  1072. queue->pages_to_unmap,
  1073. gop - queue->tx_unmap_ops);
  1074. if (ret) {
  1075. netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
  1076. gop - queue->tx_unmap_ops, ret);
  1077. for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
  1078. if (gop[i].status != GNTST_okay)
  1079. netdev_err(queue->vif->dev,
  1080. " host_addr: 0x%llx handle: 0x%x status: %d\n",
  1081. gop[i].host_addr,
  1082. gop[i].handle,
  1083. gop[i].status);
  1084. }
  1085. BUG();
  1086. }
  1087. }
  1088. for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
  1089. xenvif_idx_release(queue, pending_idx_release[i],
  1090. XEN_NETIF_RSP_OKAY);
  1091. }
  1092. /* Called after netfront has transmitted */
  1093. int xenvif_tx_action(struct xenvif_queue *queue, int budget)
  1094. {
  1095. unsigned nr_mops, nr_cops = 0;
  1096. int work_done, ret;
  1097. if (unlikely(!tx_work_todo(queue)))
  1098. return 0;
  1099. xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
  1100. if (nr_cops == 0)
  1101. return 0;
  1102. gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
  1103. if (nr_mops != 0) {
  1104. ret = gnttab_map_refs(queue->tx_map_ops,
  1105. NULL,
  1106. queue->pages_to_map,
  1107. nr_mops);
  1108. BUG_ON(ret);
  1109. }
  1110. work_done = xenvif_tx_submit(queue);
  1111. return work_done;
  1112. }
  1113. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  1114. u8 status)
  1115. {
  1116. struct pending_tx_info *pending_tx_info;
  1117. pending_ring_idx_t index;
  1118. unsigned long flags;
  1119. pending_tx_info = &queue->pending_tx_info[pending_idx];
  1120. spin_lock_irqsave(&queue->response_lock, flags);
  1121. make_tx_response(queue, &pending_tx_info->req,
  1122. pending_tx_info->extra_count, status);
  1123. /* Release the pending index before pusing the Tx response so
  1124. * its available before a new Tx request is pushed by the
  1125. * frontend.
  1126. */
  1127. index = pending_index(queue->pending_prod++);
  1128. queue->pending_ring[index] = pending_idx;
  1129. push_tx_responses(queue);
  1130. spin_unlock_irqrestore(&queue->response_lock, flags);
  1131. }
  1132. static void make_tx_response(struct xenvif_queue *queue,
  1133. struct xen_netif_tx_request *txp,
  1134. unsigned int extra_count,
  1135. s8 st)
  1136. {
  1137. RING_IDX i = queue->tx.rsp_prod_pvt;
  1138. struct xen_netif_tx_response *resp;
  1139. resp = RING_GET_RESPONSE(&queue->tx, i);
  1140. resp->id = txp->id;
  1141. resp->status = st;
  1142. while (extra_count-- != 0)
  1143. RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1144. queue->tx.rsp_prod_pvt = ++i;
  1145. }
  1146. static void push_tx_responses(struct xenvif_queue *queue)
  1147. {
  1148. int notify;
  1149. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
  1150. if (notify)
  1151. notify_remote_via_irq(queue->tx_irq);
  1152. }
  1153. void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
  1154. {
  1155. int ret;
  1156. struct gnttab_unmap_grant_ref tx_unmap_op;
  1157. gnttab_set_unmap_op(&tx_unmap_op,
  1158. idx_to_kaddr(queue, pending_idx),
  1159. GNTMAP_host_map,
  1160. queue->grant_tx_handle[pending_idx]);
  1161. xenvif_grant_handle_reset(queue, pending_idx);
  1162. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1163. &queue->mmap_pages[pending_idx], 1);
  1164. if (ret) {
  1165. netdev_err(queue->vif->dev,
  1166. "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
  1167. ret,
  1168. pending_idx,
  1169. tx_unmap_op.host_addr,
  1170. tx_unmap_op.handle,
  1171. tx_unmap_op.status);
  1172. BUG();
  1173. }
  1174. }
  1175. static inline int tx_work_todo(struct xenvif_queue *queue)
  1176. {
  1177. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
  1178. return 1;
  1179. return 0;
  1180. }
  1181. static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
  1182. {
  1183. return queue->dealloc_cons != queue->dealloc_prod;
  1184. }
  1185. void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue)
  1186. {
  1187. if (queue->tx.sring)
  1188. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1189. queue->tx.sring);
  1190. if (queue->rx.sring)
  1191. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1192. queue->rx.sring);
  1193. }
  1194. int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
  1195. grant_ref_t tx_ring_ref,
  1196. grant_ref_t rx_ring_ref)
  1197. {
  1198. void *addr;
  1199. struct xen_netif_tx_sring *txs;
  1200. struct xen_netif_rx_sring *rxs;
  1201. int err = -ENOMEM;
  1202. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1203. &tx_ring_ref, 1, &addr);
  1204. if (err)
  1205. goto err;
  1206. txs = (struct xen_netif_tx_sring *)addr;
  1207. BACK_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
  1208. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1209. &rx_ring_ref, 1, &addr);
  1210. if (err)
  1211. goto err;
  1212. rxs = (struct xen_netif_rx_sring *)addr;
  1213. BACK_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
  1214. return 0;
  1215. err:
  1216. xenvif_unmap_frontend_data_rings(queue);
  1217. return err;
  1218. }
  1219. static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
  1220. {
  1221. /* Dealloc thread must remain running until all inflight
  1222. * packets complete.
  1223. */
  1224. return kthread_should_stop() &&
  1225. !atomic_read(&queue->inflight_packets);
  1226. }
  1227. int xenvif_dealloc_kthread(void *data)
  1228. {
  1229. struct xenvif_queue *queue = data;
  1230. for (;;) {
  1231. wait_event_interruptible(queue->dealloc_wq,
  1232. tx_dealloc_work_todo(queue) ||
  1233. xenvif_dealloc_kthread_should_stop(queue));
  1234. if (xenvif_dealloc_kthread_should_stop(queue))
  1235. break;
  1236. xenvif_tx_dealloc_action(queue);
  1237. cond_resched();
  1238. }
  1239. /* Unmap anything remaining*/
  1240. if (tx_dealloc_work_todo(queue))
  1241. xenvif_tx_dealloc_action(queue);
  1242. return 0;
  1243. }
  1244. static void make_ctrl_response(struct xenvif *vif,
  1245. const struct xen_netif_ctrl_request *req,
  1246. u32 status, u32 data)
  1247. {
  1248. RING_IDX idx = vif->ctrl.rsp_prod_pvt;
  1249. struct xen_netif_ctrl_response rsp = {
  1250. .id = req->id,
  1251. .type = req->type,
  1252. .status = status,
  1253. .data = data,
  1254. };
  1255. *RING_GET_RESPONSE(&vif->ctrl, idx) = rsp;
  1256. vif->ctrl.rsp_prod_pvt = ++idx;
  1257. }
  1258. static void push_ctrl_response(struct xenvif *vif)
  1259. {
  1260. int notify;
  1261. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->ctrl, notify);
  1262. if (notify)
  1263. notify_remote_via_irq(vif->ctrl_irq);
  1264. }
  1265. static void process_ctrl_request(struct xenvif *vif,
  1266. const struct xen_netif_ctrl_request *req)
  1267. {
  1268. u32 status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
  1269. u32 data = 0;
  1270. switch (req->type) {
  1271. case XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM:
  1272. status = xenvif_set_hash_alg(vif, req->data[0]);
  1273. break;
  1274. case XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS:
  1275. status = xenvif_get_hash_flags(vif, &data);
  1276. break;
  1277. case XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS:
  1278. status = xenvif_set_hash_flags(vif, req->data[0]);
  1279. break;
  1280. case XEN_NETIF_CTRL_TYPE_SET_HASH_KEY:
  1281. status = xenvif_set_hash_key(vif, req->data[0],
  1282. req->data[1]);
  1283. break;
  1284. case XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE:
  1285. status = XEN_NETIF_CTRL_STATUS_SUCCESS;
  1286. data = XEN_NETBK_MAX_HASH_MAPPING_SIZE;
  1287. break;
  1288. case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE:
  1289. status = xenvif_set_hash_mapping_size(vif,
  1290. req->data[0]);
  1291. break;
  1292. case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING:
  1293. status = xenvif_set_hash_mapping(vif, req->data[0],
  1294. req->data[1],
  1295. req->data[2]);
  1296. break;
  1297. default:
  1298. break;
  1299. }
  1300. make_ctrl_response(vif, req, status, data);
  1301. push_ctrl_response(vif);
  1302. }
  1303. static void xenvif_ctrl_action(struct xenvif *vif)
  1304. {
  1305. for (;;) {
  1306. RING_IDX req_prod, req_cons;
  1307. req_prod = vif->ctrl.sring->req_prod;
  1308. req_cons = vif->ctrl.req_cons;
  1309. /* Make sure we can see requests before we process them. */
  1310. rmb();
  1311. if (req_cons == req_prod)
  1312. break;
  1313. while (req_cons != req_prod) {
  1314. struct xen_netif_ctrl_request req;
  1315. RING_COPY_REQUEST(&vif->ctrl, req_cons, &req);
  1316. req_cons++;
  1317. process_ctrl_request(vif, &req);
  1318. }
  1319. vif->ctrl.req_cons = req_cons;
  1320. vif->ctrl.sring->req_event = req_cons + 1;
  1321. }
  1322. }
  1323. static bool xenvif_ctrl_work_todo(struct xenvif *vif)
  1324. {
  1325. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->ctrl)))
  1326. return 1;
  1327. return 0;
  1328. }
  1329. irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
  1330. {
  1331. struct xenvif *vif = data;
  1332. while (xenvif_ctrl_work_todo(vif))
  1333. xenvif_ctrl_action(vif);
  1334. return IRQ_HANDLED;
  1335. }
  1336. static int __init netback_init(void)
  1337. {
  1338. int rc = 0;
  1339. if (!xen_domain())
  1340. return -ENODEV;
  1341. /* Allow as many queues as there are CPUs but max. 8 if user has not
  1342. * specified a value.
  1343. */
  1344. if (xenvif_max_queues == 0)
  1345. xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
  1346. num_online_cpus());
  1347. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1348. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1349. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1350. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1351. }
  1352. rc = xenvif_xenbus_init();
  1353. if (rc)
  1354. goto failed_init;
  1355. #ifdef CONFIG_DEBUG_FS
  1356. xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
  1357. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  1358. pr_warn("Init of debugfs returned %ld!\n",
  1359. PTR_ERR(xen_netback_dbg_root));
  1360. #endif /* CONFIG_DEBUG_FS */
  1361. return 0;
  1362. failed_init:
  1363. return rc;
  1364. }
  1365. module_init(netback_init);
  1366. static void __exit netback_fini(void)
  1367. {
  1368. #ifdef CONFIG_DEBUG_FS
  1369. if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
  1370. debugfs_remove_recursive(xen_netback_dbg_root);
  1371. #endif /* CONFIG_DEBUG_FS */
  1372. xenvif_xenbus_fini();
  1373. }
  1374. module_exit(netback_fini);
  1375. MODULE_LICENSE("Dual BSD/GPL");
  1376. MODULE_ALIAS("xen-backend:vif");