netback.c 47 KB

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