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