netback.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  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(unsigned long data)
  156. {
  157. struct xenvif_queue *queue = (struct xenvif_queue *)data;
  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. queue->credit_timeout.data =
  592. (unsigned long)queue;
  593. mod_timer(&queue->credit_timeout,
  594. next_credit);
  595. queue->credit_window_start = next_credit;
  596. queue->rate_limited = true;
  597. return true;
  598. }
  599. return false;
  600. }
  601. /* No locking is required in xenvif_mcast_add/del() as they are
  602. * only ever invoked from NAPI poll. An RCU list is used because
  603. * xenvif_mcast_match() is called asynchronously, during start_xmit.
  604. */
  605. static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
  606. {
  607. struct xenvif_mcast_addr *mcast;
  608. if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
  609. if (net_ratelimit())
  610. netdev_err(vif->dev,
  611. "Too many multicast addresses\n");
  612. return -ENOSPC;
  613. }
  614. mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
  615. if (!mcast)
  616. return -ENOMEM;
  617. ether_addr_copy(mcast->addr, addr);
  618. list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
  619. vif->fe_mcast_count++;
  620. return 0;
  621. }
  622. static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
  623. {
  624. struct xenvif_mcast_addr *mcast;
  625. list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
  626. if (ether_addr_equal(addr, mcast->addr)) {
  627. --vif->fe_mcast_count;
  628. list_del_rcu(&mcast->entry);
  629. kfree_rcu(mcast, rcu);
  630. break;
  631. }
  632. }
  633. }
  634. bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
  635. {
  636. struct xenvif_mcast_addr *mcast;
  637. rcu_read_lock();
  638. list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
  639. if (ether_addr_equal(addr, mcast->addr)) {
  640. rcu_read_unlock();
  641. return true;
  642. }
  643. }
  644. rcu_read_unlock();
  645. return false;
  646. }
  647. void xenvif_mcast_addr_list_free(struct xenvif *vif)
  648. {
  649. /* No need for locking or RCU here. NAPI poll and TX queue
  650. * are stopped.
  651. */
  652. while (!list_empty(&vif->fe_mcast_addr)) {
  653. struct xenvif_mcast_addr *mcast;
  654. mcast = list_first_entry(&vif->fe_mcast_addr,
  655. struct xenvif_mcast_addr,
  656. entry);
  657. --vif->fe_mcast_count;
  658. list_del(&mcast->entry);
  659. kfree(mcast);
  660. }
  661. }
  662. static void xenvif_tx_build_gops(struct xenvif_queue *queue,
  663. int budget,
  664. unsigned *copy_ops,
  665. unsigned *map_ops)
  666. {
  667. struct gnttab_map_grant_ref *gop = queue->tx_map_ops;
  668. struct sk_buff *skb, *nskb;
  669. int ret;
  670. unsigned int frag_overflow;
  671. while (skb_queue_len(&queue->tx_queue) < budget) {
  672. struct xen_netif_tx_request txreq;
  673. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  674. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  675. unsigned int extra_count;
  676. u16 pending_idx;
  677. RING_IDX idx;
  678. int work_to_do;
  679. unsigned int data_len;
  680. pending_ring_idx_t index;
  681. if (queue->tx.sring->req_prod - queue->tx.req_cons >
  682. XEN_NETIF_TX_RING_SIZE) {
  683. netdev_err(queue->vif->dev,
  684. "Impossible number of requests. "
  685. "req_prod %d, req_cons %d, size %ld\n",
  686. queue->tx.sring->req_prod, queue->tx.req_cons,
  687. XEN_NETIF_TX_RING_SIZE);
  688. xenvif_fatal_tx_err(queue->vif);
  689. break;
  690. }
  691. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
  692. if (!work_to_do)
  693. break;
  694. idx = queue->tx.req_cons;
  695. rmb(); /* Ensure that we see the request before we copy it. */
  696. RING_COPY_REQUEST(&queue->tx, idx, &txreq);
  697. /* Credit-based scheduling. */
  698. if (txreq.size > queue->remaining_credit &&
  699. tx_credit_exceeded(queue, txreq.size))
  700. break;
  701. queue->remaining_credit -= txreq.size;
  702. work_to_do--;
  703. queue->tx.req_cons = ++idx;
  704. memset(extras, 0, sizeof(extras));
  705. extra_count = 0;
  706. if (txreq.flags & XEN_NETTXF_extra_info) {
  707. work_to_do = xenvif_get_extras(queue, extras,
  708. &extra_count,
  709. work_to_do);
  710. idx = queue->tx.req_cons;
  711. if (unlikely(work_to_do < 0))
  712. break;
  713. }
  714. if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
  715. struct xen_netif_extra_info *extra;
  716. extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
  717. ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
  718. make_tx_response(queue, &txreq, extra_count,
  719. (ret == 0) ?
  720. XEN_NETIF_RSP_OKAY :
  721. XEN_NETIF_RSP_ERROR);
  722. push_tx_responses(queue);
  723. continue;
  724. }
  725. if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
  726. struct xen_netif_extra_info *extra;
  727. extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
  728. xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
  729. make_tx_response(queue, &txreq, extra_count,
  730. XEN_NETIF_RSP_OKAY);
  731. push_tx_responses(queue);
  732. continue;
  733. }
  734. ret = xenvif_count_requests(queue, &txreq, extra_count,
  735. txfrags, work_to_do);
  736. if (unlikely(ret < 0))
  737. break;
  738. idx += ret;
  739. if (unlikely(txreq.size < ETH_HLEN)) {
  740. netdev_dbg(queue->vif->dev,
  741. "Bad packet size: %d\n", txreq.size);
  742. xenvif_tx_err(queue, &txreq, extra_count, idx);
  743. break;
  744. }
  745. /* No crossing a page as the payload mustn't fragment. */
  746. if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
  747. netdev_err(queue->vif->dev,
  748. "txreq.offset: %u, size: %u, end: %lu\n",
  749. txreq.offset, txreq.size,
  750. (unsigned long)(txreq.offset&~XEN_PAGE_MASK) + txreq.size);
  751. xenvif_fatal_tx_err(queue->vif);
  752. break;
  753. }
  754. index = pending_index(queue->pending_cons);
  755. pending_idx = queue->pending_ring[index];
  756. data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
  757. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  758. XEN_NETBACK_TX_COPY_LEN : txreq.size;
  759. skb = xenvif_alloc_skb(data_len);
  760. if (unlikely(skb == NULL)) {
  761. netdev_dbg(queue->vif->dev,
  762. "Can't allocate a skb in start_xmit.\n");
  763. xenvif_tx_err(queue, &txreq, extra_count, idx);
  764. break;
  765. }
  766. skb_shinfo(skb)->nr_frags = ret;
  767. if (data_len < txreq.size)
  768. skb_shinfo(skb)->nr_frags++;
  769. /* At this point shinfo->nr_frags is in fact the number of
  770. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  771. */
  772. frag_overflow = 0;
  773. nskb = NULL;
  774. if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
  775. frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
  776. BUG_ON(frag_overflow > MAX_SKB_FRAGS);
  777. skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
  778. nskb = xenvif_alloc_skb(0);
  779. if (unlikely(nskb == NULL)) {
  780. kfree_skb(skb);
  781. xenvif_tx_err(queue, &txreq, extra_count, idx);
  782. if (net_ratelimit())
  783. netdev_err(queue->vif->dev,
  784. "Can't allocate the frag_list skb.\n");
  785. break;
  786. }
  787. }
  788. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  789. struct xen_netif_extra_info *gso;
  790. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  791. if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
  792. /* Failure in xenvif_set_skb_gso is fatal. */
  793. kfree_skb(skb);
  794. kfree_skb(nskb);
  795. break;
  796. }
  797. }
  798. if (extras[XEN_NETIF_EXTRA_TYPE_HASH - 1].type) {
  799. struct xen_netif_extra_info *extra;
  800. enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
  801. extra = &extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
  802. switch (extra->u.hash.type) {
  803. case _XEN_NETIF_CTRL_HASH_TYPE_IPV4:
  804. case _XEN_NETIF_CTRL_HASH_TYPE_IPV6:
  805. type = PKT_HASH_TYPE_L3;
  806. break;
  807. case _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP:
  808. case _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP:
  809. type = PKT_HASH_TYPE_L4;
  810. break;
  811. default:
  812. break;
  813. }
  814. if (type != PKT_HASH_TYPE_NONE)
  815. skb_set_hash(skb,
  816. *(u32 *)extra->u.hash.value,
  817. type);
  818. }
  819. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  820. __skb_put(skb, data_len);
  821. queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
  822. queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
  823. queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
  824. queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
  825. virt_to_gfn(skb->data);
  826. queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
  827. queue->tx_copy_ops[*copy_ops].dest.offset =
  828. offset_in_page(skb->data) & ~XEN_PAGE_MASK;
  829. queue->tx_copy_ops[*copy_ops].len = data_len;
  830. queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
  831. (*copy_ops)++;
  832. if (data_len < txreq.size) {
  833. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  834. pending_idx);
  835. xenvif_tx_create_map_op(queue, pending_idx, &txreq,
  836. extra_count, gop);
  837. gop++;
  838. } else {
  839. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  840. INVALID_PENDING_IDX);
  841. memcpy(&queue->pending_tx_info[pending_idx].req,
  842. &txreq, sizeof(txreq));
  843. queue->pending_tx_info[pending_idx].extra_count =
  844. extra_count;
  845. }
  846. queue->pending_cons++;
  847. gop = xenvif_get_requests(queue, skb, txfrags, gop,
  848. frag_overflow, nskb);
  849. __skb_queue_tail(&queue->tx_queue, skb);
  850. queue->tx.req_cons = idx;
  851. if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
  852. (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
  853. break;
  854. }
  855. (*map_ops) = gop - queue->tx_map_ops;
  856. return;
  857. }
  858. /* Consolidate skb with a frag_list into a brand new one with local pages on
  859. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  860. */
  861. static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
  862. {
  863. unsigned int offset = skb_headlen(skb);
  864. skb_frag_t frags[MAX_SKB_FRAGS];
  865. int i, f;
  866. struct ubuf_info *uarg;
  867. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  868. queue->stats.tx_zerocopy_sent += 2;
  869. queue->stats.tx_frag_overflow++;
  870. xenvif_fill_frags(queue, nskb);
  871. /* Subtract frags size, we will correct it later */
  872. skb->truesize -= skb->data_len;
  873. skb->len += nskb->len;
  874. skb->data_len += nskb->len;
  875. /* create a brand new frags array and coalesce there */
  876. for (i = 0; offset < skb->len; i++) {
  877. struct page *page;
  878. unsigned int len;
  879. BUG_ON(i >= MAX_SKB_FRAGS);
  880. page = alloc_page(GFP_ATOMIC);
  881. if (!page) {
  882. int j;
  883. skb->truesize += skb->data_len;
  884. for (j = 0; j < i; j++)
  885. put_page(frags[j].page.p);
  886. return -ENOMEM;
  887. }
  888. if (offset + PAGE_SIZE < skb->len)
  889. len = PAGE_SIZE;
  890. else
  891. len = skb->len - offset;
  892. if (skb_copy_bits(skb, offset, page_address(page), len))
  893. BUG();
  894. offset += len;
  895. frags[i].page.p = page;
  896. frags[i].page_offset = 0;
  897. skb_frag_size_set(&frags[i], len);
  898. }
  899. /* Copied all the bits from the frag list -- free it. */
  900. skb_frag_list_init(skb);
  901. xenvif_skb_zerocopy_prepare(queue, nskb);
  902. kfree_skb(nskb);
  903. /* Release all the original (foreign) frags. */
  904. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  905. skb_frag_unref(skb, f);
  906. uarg = skb_shinfo(skb)->destructor_arg;
  907. /* increase inflight counter to offset decrement in callback */
  908. atomic_inc(&queue->inflight_packets);
  909. uarg->callback(uarg, true);
  910. skb_shinfo(skb)->destructor_arg = NULL;
  911. /* Fill the skb with the new (local) frags. */
  912. memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
  913. skb_shinfo(skb)->nr_frags = i;
  914. skb->truesize += i * PAGE_SIZE;
  915. return 0;
  916. }
  917. static int xenvif_tx_submit(struct xenvif_queue *queue)
  918. {
  919. struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
  920. struct gnttab_copy *gop_copy = queue->tx_copy_ops;
  921. struct sk_buff *skb;
  922. int work_done = 0;
  923. while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
  924. struct xen_netif_tx_request *txp;
  925. u16 pending_idx;
  926. unsigned data_len;
  927. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  928. txp = &queue->pending_tx_info[pending_idx].req;
  929. /* Check the remap error code. */
  930. if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
  931. /* If there was an error, xenvif_tx_check_gop is
  932. * expected to release all the frags which were mapped,
  933. * so kfree_skb shouldn't do it again
  934. */
  935. skb_shinfo(skb)->nr_frags = 0;
  936. if (skb_has_frag_list(skb)) {
  937. struct sk_buff *nskb =
  938. skb_shinfo(skb)->frag_list;
  939. skb_shinfo(nskb)->nr_frags = 0;
  940. }
  941. kfree_skb(skb);
  942. continue;
  943. }
  944. data_len = skb->len;
  945. callback_param(queue, pending_idx).ctx = NULL;
  946. if (data_len < txp->size) {
  947. /* Append the packet payload as a fragment. */
  948. txp->offset += data_len;
  949. txp->size -= data_len;
  950. } else {
  951. /* Schedule a response immediately. */
  952. xenvif_idx_release(queue, pending_idx,
  953. XEN_NETIF_RSP_OKAY);
  954. }
  955. if (txp->flags & XEN_NETTXF_csum_blank)
  956. skb->ip_summed = CHECKSUM_PARTIAL;
  957. else if (txp->flags & XEN_NETTXF_data_validated)
  958. skb->ip_summed = CHECKSUM_UNNECESSARY;
  959. xenvif_fill_frags(queue, skb);
  960. if (unlikely(skb_has_frag_list(skb))) {
  961. if (xenvif_handle_frag_list(queue, skb)) {
  962. if (net_ratelimit())
  963. netdev_err(queue->vif->dev,
  964. "Not enough memory to consolidate frag_list!\n");
  965. xenvif_skb_zerocopy_prepare(queue, skb);
  966. kfree_skb(skb);
  967. continue;
  968. }
  969. }
  970. skb->dev = queue->vif->dev;
  971. skb->protocol = eth_type_trans(skb, skb->dev);
  972. skb_reset_network_header(skb);
  973. if (checksum_setup(queue, skb)) {
  974. netdev_dbg(queue->vif->dev,
  975. "Can't setup checksum in net_tx_action\n");
  976. /* We have to set this flag to trigger the callback */
  977. if (skb_shinfo(skb)->destructor_arg)
  978. xenvif_skb_zerocopy_prepare(queue, skb);
  979. kfree_skb(skb);
  980. continue;
  981. }
  982. skb_probe_transport_header(skb, 0);
  983. /* If the packet is GSO then we will have just set up the
  984. * transport header offset in checksum_setup so it's now
  985. * straightforward to calculate gso_segs.
  986. */
  987. if (skb_is_gso(skb)) {
  988. int mss = skb_shinfo(skb)->gso_size;
  989. int hdrlen = skb_transport_header(skb) -
  990. skb_mac_header(skb) +
  991. tcp_hdrlen(skb);
  992. skb_shinfo(skb)->gso_segs =
  993. DIV_ROUND_UP(skb->len - hdrlen, mss);
  994. }
  995. queue->stats.rx_bytes += skb->len;
  996. queue->stats.rx_packets++;
  997. work_done++;
  998. /* Set this flag right before netif_receive_skb, otherwise
  999. * someone might think this packet already left netback, and
  1000. * do a skb_copy_ubufs while we are still in control of the
  1001. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1002. */
  1003. if (skb_shinfo(skb)->destructor_arg) {
  1004. xenvif_skb_zerocopy_prepare(queue, skb);
  1005. queue->stats.tx_zerocopy_sent++;
  1006. }
  1007. netif_receive_skb(skb);
  1008. }
  1009. return work_done;
  1010. }
  1011. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1012. {
  1013. unsigned long flags;
  1014. pending_ring_idx_t index;
  1015. struct xenvif_queue *queue = ubuf_to_queue(ubuf);
  1016. /* This is the only place where we grab this lock, to protect callbacks
  1017. * from each other.
  1018. */
  1019. spin_lock_irqsave(&queue->callback_lock, flags);
  1020. do {
  1021. u16 pending_idx = ubuf->desc;
  1022. ubuf = (struct ubuf_info *) ubuf->ctx;
  1023. BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
  1024. MAX_PENDING_REQS);
  1025. index = pending_index(queue->dealloc_prod);
  1026. queue->dealloc_ring[index] = pending_idx;
  1027. /* Sync with xenvif_tx_dealloc_action:
  1028. * insert idx then incr producer.
  1029. */
  1030. smp_wmb();
  1031. queue->dealloc_prod++;
  1032. } while (ubuf);
  1033. spin_unlock_irqrestore(&queue->callback_lock, flags);
  1034. if (likely(zerocopy_success))
  1035. queue->stats.tx_zerocopy_success++;
  1036. else
  1037. queue->stats.tx_zerocopy_fail++;
  1038. xenvif_skb_zerocopy_complete(queue);
  1039. }
  1040. static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
  1041. {
  1042. struct gnttab_unmap_grant_ref *gop;
  1043. pending_ring_idx_t dc, dp;
  1044. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1045. unsigned int i = 0;
  1046. dc = queue->dealloc_cons;
  1047. gop = queue->tx_unmap_ops;
  1048. /* Free up any grants we have finished using */
  1049. do {
  1050. dp = queue->dealloc_prod;
  1051. /* Ensure we see all indices enqueued by all
  1052. * xenvif_zerocopy_callback().
  1053. */
  1054. smp_rmb();
  1055. while (dc != dp) {
  1056. BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
  1057. pending_idx =
  1058. queue->dealloc_ring[pending_index(dc++)];
  1059. pending_idx_release[gop - queue->tx_unmap_ops] =
  1060. pending_idx;
  1061. queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
  1062. queue->mmap_pages[pending_idx];
  1063. gnttab_set_unmap_op(gop,
  1064. idx_to_kaddr(queue, pending_idx),
  1065. GNTMAP_host_map,
  1066. queue->grant_tx_handle[pending_idx]);
  1067. xenvif_grant_handle_reset(queue, pending_idx);
  1068. ++gop;
  1069. }
  1070. } while (dp != queue->dealloc_prod);
  1071. queue->dealloc_cons = dc;
  1072. if (gop - queue->tx_unmap_ops > 0) {
  1073. int ret;
  1074. ret = gnttab_unmap_refs(queue->tx_unmap_ops,
  1075. NULL,
  1076. queue->pages_to_unmap,
  1077. gop - queue->tx_unmap_ops);
  1078. if (ret) {
  1079. netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
  1080. gop - queue->tx_unmap_ops, ret);
  1081. for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
  1082. if (gop[i].status != GNTST_okay)
  1083. netdev_err(queue->vif->dev,
  1084. " host_addr: 0x%llx handle: 0x%x status: %d\n",
  1085. gop[i].host_addr,
  1086. gop[i].handle,
  1087. gop[i].status);
  1088. }
  1089. BUG();
  1090. }
  1091. }
  1092. for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
  1093. xenvif_idx_release(queue, pending_idx_release[i],
  1094. XEN_NETIF_RSP_OKAY);
  1095. }
  1096. /* Called after netfront has transmitted */
  1097. int xenvif_tx_action(struct xenvif_queue *queue, int budget)
  1098. {
  1099. unsigned nr_mops, nr_cops = 0;
  1100. int work_done, ret;
  1101. if (unlikely(!tx_work_todo(queue)))
  1102. return 0;
  1103. xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
  1104. if (nr_cops == 0)
  1105. return 0;
  1106. gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
  1107. if (nr_mops != 0) {
  1108. ret = gnttab_map_refs(queue->tx_map_ops,
  1109. NULL,
  1110. queue->pages_to_map,
  1111. nr_mops);
  1112. BUG_ON(ret);
  1113. }
  1114. work_done = xenvif_tx_submit(queue);
  1115. return work_done;
  1116. }
  1117. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  1118. u8 status)
  1119. {
  1120. struct pending_tx_info *pending_tx_info;
  1121. pending_ring_idx_t index;
  1122. unsigned long flags;
  1123. pending_tx_info = &queue->pending_tx_info[pending_idx];
  1124. spin_lock_irqsave(&queue->response_lock, flags);
  1125. make_tx_response(queue, &pending_tx_info->req,
  1126. pending_tx_info->extra_count, status);
  1127. /* Release the pending index before pusing the Tx response so
  1128. * its available before a new Tx request is pushed by the
  1129. * frontend.
  1130. */
  1131. index = pending_index(queue->pending_prod++);
  1132. queue->pending_ring[index] = pending_idx;
  1133. push_tx_responses(queue);
  1134. spin_unlock_irqrestore(&queue->response_lock, flags);
  1135. }
  1136. static void make_tx_response(struct xenvif_queue *queue,
  1137. struct xen_netif_tx_request *txp,
  1138. unsigned int extra_count,
  1139. s8 st)
  1140. {
  1141. RING_IDX i = queue->tx.rsp_prod_pvt;
  1142. struct xen_netif_tx_response *resp;
  1143. resp = RING_GET_RESPONSE(&queue->tx, i);
  1144. resp->id = txp->id;
  1145. resp->status = st;
  1146. while (extra_count-- != 0)
  1147. RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1148. queue->tx.rsp_prod_pvt = ++i;
  1149. }
  1150. static void push_tx_responses(struct xenvif_queue *queue)
  1151. {
  1152. int notify;
  1153. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
  1154. if (notify)
  1155. notify_remote_via_irq(queue->tx_irq);
  1156. }
  1157. void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
  1158. {
  1159. int ret;
  1160. struct gnttab_unmap_grant_ref tx_unmap_op;
  1161. gnttab_set_unmap_op(&tx_unmap_op,
  1162. idx_to_kaddr(queue, pending_idx),
  1163. GNTMAP_host_map,
  1164. queue->grant_tx_handle[pending_idx]);
  1165. xenvif_grant_handle_reset(queue, pending_idx);
  1166. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1167. &queue->mmap_pages[pending_idx], 1);
  1168. if (ret) {
  1169. netdev_err(queue->vif->dev,
  1170. "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
  1171. ret,
  1172. pending_idx,
  1173. tx_unmap_op.host_addr,
  1174. tx_unmap_op.handle,
  1175. tx_unmap_op.status);
  1176. BUG();
  1177. }
  1178. }
  1179. static inline int tx_work_todo(struct xenvif_queue *queue)
  1180. {
  1181. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
  1182. return 1;
  1183. return 0;
  1184. }
  1185. static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
  1186. {
  1187. return queue->dealloc_cons != queue->dealloc_prod;
  1188. }
  1189. void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue)
  1190. {
  1191. if (queue->tx.sring)
  1192. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1193. queue->tx.sring);
  1194. if (queue->rx.sring)
  1195. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1196. queue->rx.sring);
  1197. }
  1198. int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
  1199. grant_ref_t tx_ring_ref,
  1200. grant_ref_t rx_ring_ref)
  1201. {
  1202. void *addr;
  1203. struct xen_netif_tx_sring *txs;
  1204. struct xen_netif_rx_sring *rxs;
  1205. int err = -ENOMEM;
  1206. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1207. &tx_ring_ref, 1, &addr);
  1208. if (err)
  1209. goto err;
  1210. txs = (struct xen_netif_tx_sring *)addr;
  1211. BACK_RING_INIT(&queue->tx, txs, XEN_PAGE_SIZE);
  1212. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1213. &rx_ring_ref, 1, &addr);
  1214. if (err)
  1215. goto err;
  1216. rxs = (struct xen_netif_rx_sring *)addr;
  1217. BACK_RING_INIT(&queue->rx, rxs, XEN_PAGE_SIZE);
  1218. return 0;
  1219. err:
  1220. xenvif_unmap_frontend_data_rings(queue);
  1221. return err;
  1222. }
  1223. static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
  1224. {
  1225. /* Dealloc thread must remain running until all inflight
  1226. * packets complete.
  1227. */
  1228. return kthread_should_stop() &&
  1229. !atomic_read(&queue->inflight_packets);
  1230. }
  1231. int xenvif_dealloc_kthread(void *data)
  1232. {
  1233. struct xenvif_queue *queue = data;
  1234. for (;;) {
  1235. wait_event_interruptible(queue->dealloc_wq,
  1236. tx_dealloc_work_todo(queue) ||
  1237. xenvif_dealloc_kthread_should_stop(queue));
  1238. if (xenvif_dealloc_kthread_should_stop(queue))
  1239. break;
  1240. xenvif_tx_dealloc_action(queue);
  1241. cond_resched();
  1242. }
  1243. /* Unmap anything remaining*/
  1244. if (tx_dealloc_work_todo(queue))
  1245. xenvif_tx_dealloc_action(queue);
  1246. return 0;
  1247. }
  1248. static void make_ctrl_response(struct xenvif *vif,
  1249. const struct xen_netif_ctrl_request *req,
  1250. u32 status, u32 data)
  1251. {
  1252. RING_IDX idx = vif->ctrl.rsp_prod_pvt;
  1253. struct xen_netif_ctrl_response rsp = {
  1254. .id = req->id,
  1255. .type = req->type,
  1256. .status = status,
  1257. .data = data,
  1258. };
  1259. *RING_GET_RESPONSE(&vif->ctrl, idx) = rsp;
  1260. vif->ctrl.rsp_prod_pvt = ++idx;
  1261. }
  1262. static void push_ctrl_response(struct xenvif *vif)
  1263. {
  1264. int notify;
  1265. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->ctrl, notify);
  1266. if (notify)
  1267. notify_remote_via_irq(vif->ctrl_irq);
  1268. }
  1269. static void process_ctrl_request(struct xenvif *vif,
  1270. const struct xen_netif_ctrl_request *req)
  1271. {
  1272. u32 status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
  1273. u32 data = 0;
  1274. switch (req->type) {
  1275. case XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM:
  1276. status = xenvif_set_hash_alg(vif, req->data[0]);
  1277. break;
  1278. case XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS:
  1279. status = xenvif_get_hash_flags(vif, &data);
  1280. break;
  1281. case XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS:
  1282. status = xenvif_set_hash_flags(vif, req->data[0]);
  1283. break;
  1284. case XEN_NETIF_CTRL_TYPE_SET_HASH_KEY:
  1285. status = xenvif_set_hash_key(vif, req->data[0],
  1286. req->data[1]);
  1287. break;
  1288. case XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE:
  1289. status = XEN_NETIF_CTRL_STATUS_SUCCESS;
  1290. data = XEN_NETBK_MAX_HASH_MAPPING_SIZE;
  1291. break;
  1292. case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE:
  1293. status = xenvif_set_hash_mapping_size(vif,
  1294. req->data[0]);
  1295. break;
  1296. case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING:
  1297. status = xenvif_set_hash_mapping(vif, req->data[0],
  1298. req->data[1],
  1299. req->data[2]);
  1300. break;
  1301. default:
  1302. break;
  1303. }
  1304. make_ctrl_response(vif, req, status, data);
  1305. push_ctrl_response(vif);
  1306. }
  1307. static void xenvif_ctrl_action(struct xenvif *vif)
  1308. {
  1309. for (;;) {
  1310. RING_IDX req_prod, req_cons;
  1311. req_prod = vif->ctrl.sring->req_prod;
  1312. req_cons = vif->ctrl.req_cons;
  1313. /* Make sure we can see requests before we process them. */
  1314. rmb();
  1315. if (req_cons == req_prod)
  1316. break;
  1317. while (req_cons != req_prod) {
  1318. struct xen_netif_ctrl_request req;
  1319. RING_COPY_REQUEST(&vif->ctrl, req_cons, &req);
  1320. req_cons++;
  1321. process_ctrl_request(vif, &req);
  1322. }
  1323. vif->ctrl.req_cons = req_cons;
  1324. vif->ctrl.sring->req_event = req_cons + 1;
  1325. }
  1326. }
  1327. static bool xenvif_ctrl_work_todo(struct xenvif *vif)
  1328. {
  1329. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->ctrl)))
  1330. return 1;
  1331. return 0;
  1332. }
  1333. irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
  1334. {
  1335. struct xenvif *vif = data;
  1336. while (xenvif_ctrl_work_todo(vif))
  1337. xenvif_ctrl_action(vif);
  1338. return IRQ_HANDLED;
  1339. }
  1340. static int __init netback_init(void)
  1341. {
  1342. int rc = 0;
  1343. if (!xen_domain())
  1344. return -ENODEV;
  1345. /* Allow as many queues as there are CPUs but max. 8 if user has not
  1346. * specified a value.
  1347. */
  1348. if (xenvif_max_queues == 0)
  1349. xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
  1350. num_online_cpus());
  1351. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1352. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1353. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1354. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1355. }
  1356. rc = xenvif_xenbus_init();
  1357. if (rc)
  1358. goto failed_init;
  1359. #ifdef CONFIG_DEBUG_FS
  1360. xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
  1361. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  1362. pr_warn("Init of debugfs returned %ld!\n",
  1363. PTR_ERR(xen_netback_dbg_root));
  1364. #endif /* CONFIG_DEBUG_FS */
  1365. return 0;
  1366. failed_init:
  1367. return rc;
  1368. }
  1369. module_init(netback_init);
  1370. static void __exit netback_fini(void)
  1371. {
  1372. #ifdef CONFIG_DEBUG_FS
  1373. if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
  1374. debugfs_remove_recursive(xen_netback_dbg_root);
  1375. #endif /* CONFIG_DEBUG_FS */
  1376. xenvif_xenbus_fini();
  1377. }
  1378. module_exit(netback_fini);
  1379. MODULE_LICENSE("Dual BSD/GPL");
  1380. MODULE_ALIAS("xen-backend:vif");