netback.c 47 KB

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