netback.c 44 KB

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