netback.c 47 KB

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