netback.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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 <asm/xen/hypercall.h>
  44. #include <asm/xen/page.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 = 1;
  50. module_param(separate_tx_rx_irq, bool, 0644);
  51. /*
  52. * This is the maximum slots a skb can have. If a guest sends a skb
  53. * which exceeds this limit it is considered malicious.
  54. */
  55. #define FATAL_SKB_SLOTS_DEFAULT 20
  56. static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
  57. module_param(fatal_skb_slots, uint, 0444);
  58. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  59. u8 status);
  60. static void make_tx_response(struct xenvif *vif,
  61. struct xen_netif_tx_request *txp,
  62. s8 st);
  63. static inline int tx_work_todo(struct xenvif *vif);
  64. static inline int rx_work_todo(struct xenvif *vif);
  65. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  66. u16 id,
  67. s8 st,
  68. u16 offset,
  69. u16 size,
  70. u16 flags);
  71. static inline unsigned long idx_to_pfn(struct xenvif *vif,
  72. u16 idx)
  73. {
  74. return page_to_pfn(vif->mmap_pages[idx]);
  75. }
  76. static inline unsigned long idx_to_kaddr(struct xenvif *vif,
  77. u16 idx)
  78. {
  79. return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
  80. }
  81. /* Find the containing VIF's structure from a pointer in pending_tx_info array
  82. */
  83. static inline struct xenvif* ubuf_to_vif(struct ubuf_info *ubuf)
  84. {
  85. u16 pending_idx = ubuf->desc;
  86. struct pending_tx_info *temp =
  87. container_of(ubuf, struct pending_tx_info, callback_struct);
  88. return container_of(temp - pending_idx,
  89. struct xenvif,
  90. pending_tx_info[0]);
  91. }
  92. /* This is a miniumum size for the linear area to avoid lots of
  93. * calls to __pskb_pull_tail() as we set up checksum offsets. The
  94. * value 128 was chosen as it covers all IPv4 and most likely
  95. * IPv6 headers.
  96. */
  97. #define PKT_PROT_LEN 128
  98. static u16 frag_get_pending_idx(skb_frag_t *frag)
  99. {
  100. return (u16)frag->page_offset;
  101. }
  102. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  103. {
  104. frag->page_offset = pending_idx;
  105. }
  106. static inline pending_ring_idx_t pending_index(unsigned i)
  107. {
  108. return i & (MAX_PENDING_REQS-1);
  109. }
  110. bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
  111. {
  112. RING_IDX prod, cons;
  113. do {
  114. prod = vif->rx.sring->req_prod;
  115. cons = vif->rx.req_cons;
  116. if (prod - cons >= needed)
  117. return true;
  118. vif->rx.sring->req_event = prod + 1;
  119. /* Make sure event is visible before we check prod
  120. * again.
  121. */
  122. mb();
  123. } while (vif->rx.sring->req_prod != prod);
  124. return false;
  125. }
  126. /*
  127. * Returns true if we should start a new receive buffer instead of
  128. * adding 'size' bytes to a buffer which currently contains 'offset'
  129. * bytes.
  130. */
  131. static bool start_new_rx_buffer(int offset, unsigned long size, int head)
  132. {
  133. /* simple case: we have completely filled the current buffer. */
  134. if (offset == MAX_BUFFER_OFFSET)
  135. return true;
  136. /*
  137. * complex case: start a fresh buffer if the current frag
  138. * would overflow the current buffer but only if:
  139. * (i) this frag would fit completely in the next buffer
  140. * and (ii) there is already some data in the current buffer
  141. * and (iii) this is not the head buffer.
  142. *
  143. * Where:
  144. * - (i) stops us splitting a frag into two copies
  145. * unless the frag is too large for a single buffer.
  146. * - (ii) stops us from leaving a buffer pointlessly empty.
  147. * - (iii) stops us leaving the first buffer
  148. * empty. Strictly speaking this is already covered
  149. * by (ii) but is explicitly checked because
  150. * netfront relies on the first buffer being
  151. * non-empty and can crash otherwise.
  152. *
  153. * This means we will effectively linearise small
  154. * frags but do not needlessly split large buffers
  155. * into multiple copies tend to give large frags their
  156. * own buffers as before.
  157. */
  158. if ((offset + size > MAX_BUFFER_OFFSET) &&
  159. (size <= MAX_BUFFER_OFFSET) && offset && !head)
  160. return true;
  161. return false;
  162. }
  163. struct netrx_pending_operations {
  164. unsigned copy_prod, copy_cons;
  165. unsigned meta_prod, meta_cons;
  166. struct gnttab_copy *copy;
  167. struct xenvif_rx_meta *meta;
  168. int copy_off;
  169. grant_ref_t copy_gref;
  170. };
  171. static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
  172. struct netrx_pending_operations *npo)
  173. {
  174. struct xenvif_rx_meta *meta;
  175. struct xen_netif_rx_request *req;
  176. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  177. meta = npo->meta + npo->meta_prod++;
  178. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  179. meta->gso_size = 0;
  180. meta->size = 0;
  181. meta->id = req->id;
  182. npo->copy_off = 0;
  183. npo->copy_gref = req->gref;
  184. return meta;
  185. }
  186. /*
  187. * Set up the grant operations for this fragment. If it's a flipping
  188. * interface, we also set up the unmap request from here.
  189. */
  190. static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
  191. struct netrx_pending_operations *npo,
  192. struct page *page, unsigned long size,
  193. unsigned long offset, int *head,
  194. struct xenvif *foreign_vif,
  195. grant_ref_t foreign_gref)
  196. {
  197. struct gnttab_copy *copy_gop;
  198. struct xenvif_rx_meta *meta;
  199. unsigned long bytes;
  200. int gso_type;
  201. /* Data must not cross a page boundary. */
  202. BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
  203. meta = npo->meta + npo->meta_prod - 1;
  204. /* Skip unused frames from start of page */
  205. page += offset >> PAGE_SHIFT;
  206. offset &= ~PAGE_MASK;
  207. while (size > 0) {
  208. BUG_ON(offset >= PAGE_SIZE);
  209. BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
  210. bytes = PAGE_SIZE - offset;
  211. if (bytes > size)
  212. bytes = size;
  213. if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
  214. /*
  215. * Netfront requires there to be some data in the head
  216. * buffer.
  217. */
  218. BUG_ON(*head);
  219. meta = get_next_rx_buffer(vif, npo);
  220. }
  221. if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
  222. bytes = MAX_BUFFER_OFFSET - npo->copy_off;
  223. copy_gop = npo->copy + npo->copy_prod++;
  224. copy_gop->flags = GNTCOPY_dest_gref;
  225. copy_gop->len = bytes;
  226. if (foreign_vif) {
  227. copy_gop->source.domid = foreign_vif->domid;
  228. copy_gop->source.u.ref = foreign_gref;
  229. copy_gop->flags |= GNTCOPY_source_gref;
  230. } else {
  231. copy_gop->source.domid = DOMID_SELF;
  232. copy_gop->source.u.gmfn =
  233. virt_to_mfn(page_address(page));
  234. }
  235. copy_gop->source.offset = offset;
  236. copy_gop->dest.domid = vif->domid;
  237. copy_gop->dest.offset = npo->copy_off;
  238. copy_gop->dest.u.ref = npo->copy_gref;
  239. npo->copy_off += bytes;
  240. meta->size += bytes;
  241. offset += bytes;
  242. size -= bytes;
  243. /* Next frame */
  244. if (offset == PAGE_SIZE && size) {
  245. BUG_ON(!PageCompound(page));
  246. page++;
  247. offset = 0;
  248. }
  249. /* Leave a gap for the GSO descriptor. */
  250. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  251. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  252. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  253. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  254. else
  255. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  256. if (*head && ((1 << gso_type) & vif->gso_mask))
  257. vif->rx.req_cons++;
  258. *head = 0; /* There must be something in this buffer now. */
  259. }
  260. }
  261. /*
  262. * Prepare an SKB to be transmitted to the frontend.
  263. *
  264. * This function is responsible for allocating grant operations, meta
  265. * structures, etc.
  266. *
  267. * It returns the number of meta structures consumed. The number of
  268. * ring slots used is always equal to the number of meta slots used
  269. * plus the number of GSO descriptors used. Currently, we use either
  270. * zero GSO descriptors (for non-GSO packets) or one descriptor (for
  271. * frontend-side LRO).
  272. */
  273. static int xenvif_gop_skb(struct sk_buff *skb,
  274. struct netrx_pending_operations *npo)
  275. {
  276. struct xenvif *vif = netdev_priv(skb->dev);
  277. int nr_frags = skb_shinfo(skb)->nr_frags;
  278. int i;
  279. struct xen_netif_rx_request *req;
  280. struct xenvif_rx_meta *meta;
  281. unsigned char *data;
  282. int head = 1;
  283. int old_meta_prod;
  284. int gso_type;
  285. int gso_size;
  286. struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
  287. grant_ref_t foreign_grefs[MAX_SKB_FRAGS];
  288. struct xenvif *foreign_vif = NULL;
  289. old_meta_prod = npo->meta_prod;
  290. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  291. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  292. gso_size = skb_shinfo(skb)->gso_size;
  293. } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  294. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  295. gso_size = skb_shinfo(skb)->gso_size;
  296. } else {
  297. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  298. gso_size = 0;
  299. }
  300. /* Set up a GSO prefix descriptor, if necessary */
  301. if ((1 << gso_type) & vif->gso_prefix_mask) {
  302. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  303. meta = npo->meta + npo->meta_prod++;
  304. meta->gso_type = gso_type;
  305. meta->gso_size = gso_size;
  306. meta->size = 0;
  307. meta->id = req->id;
  308. }
  309. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  310. meta = npo->meta + npo->meta_prod++;
  311. if ((1 << gso_type) & vif->gso_mask) {
  312. meta->gso_type = gso_type;
  313. meta->gso_size = gso_size;
  314. } else {
  315. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  316. meta->gso_size = 0;
  317. }
  318. meta->size = 0;
  319. meta->id = req->id;
  320. npo->copy_off = 0;
  321. npo->copy_gref = req->gref;
  322. if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
  323. (ubuf->callback == &xenvif_zerocopy_callback)) {
  324. int i = 0;
  325. foreign_vif = ubuf_to_vif(ubuf);
  326. do {
  327. u16 pending_idx = ubuf->desc;
  328. foreign_grefs[i++] =
  329. foreign_vif->pending_tx_info[pending_idx].req.gref;
  330. ubuf = (struct ubuf_info *) ubuf->ctx;
  331. } while (ubuf);
  332. }
  333. data = skb->data;
  334. while (data < skb_tail_pointer(skb)) {
  335. unsigned int offset = offset_in_page(data);
  336. unsigned int len = PAGE_SIZE - offset;
  337. if (data + len > skb_tail_pointer(skb))
  338. len = skb_tail_pointer(skb) - data;
  339. xenvif_gop_frag_copy(vif, skb, npo,
  340. virt_to_page(data), len, offset, &head,
  341. NULL,
  342. 0);
  343. data += len;
  344. }
  345. for (i = 0; i < nr_frags; i++) {
  346. xenvif_gop_frag_copy(vif, skb, npo,
  347. skb_frag_page(&skb_shinfo(skb)->frags[i]),
  348. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  349. skb_shinfo(skb)->frags[i].page_offset,
  350. &head,
  351. foreign_vif,
  352. foreign_grefs[i]);
  353. }
  354. return npo->meta_prod - old_meta_prod;
  355. }
  356. /*
  357. * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
  358. * used to set up the operations on the top of
  359. * netrx_pending_operations, which have since been done. Check that
  360. * they didn't give any errors and advance over them.
  361. */
  362. static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
  363. struct netrx_pending_operations *npo)
  364. {
  365. struct gnttab_copy *copy_op;
  366. int status = XEN_NETIF_RSP_OKAY;
  367. int i;
  368. for (i = 0; i < nr_meta_slots; i++) {
  369. copy_op = npo->copy + npo->copy_cons++;
  370. if (copy_op->status != GNTST_okay) {
  371. netdev_dbg(vif->dev,
  372. "Bad status %d from copy to DOM%d.\n",
  373. copy_op->status, vif->domid);
  374. status = XEN_NETIF_RSP_ERROR;
  375. }
  376. }
  377. return status;
  378. }
  379. static void xenvif_add_frag_responses(struct xenvif *vif, int status,
  380. struct xenvif_rx_meta *meta,
  381. int nr_meta_slots)
  382. {
  383. int i;
  384. unsigned long offset;
  385. /* No fragments used */
  386. if (nr_meta_slots <= 1)
  387. return;
  388. nr_meta_slots--;
  389. for (i = 0; i < nr_meta_slots; i++) {
  390. int flags;
  391. if (i == nr_meta_slots - 1)
  392. flags = 0;
  393. else
  394. flags = XEN_NETRXF_more_data;
  395. offset = 0;
  396. make_rx_response(vif, meta[i].id, status, offset,
  397. meta[i].size, flags);
  398. }
  399. }
  400. struct xenvif_rx_cb {
  401. int meta_slots_used;
  402. };
  403. #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
  404. void xenvif_kick_thread(struct xenvif *vif)
  405. {
  406. wake_up(&vif->wq);
  407. }
  408. static void xenvif_rx_action(struct xenvif *vif)
  409. {
  410. s8 status;
  411. u16 flags;
  412. struct xen_netif_rx_response *resp;
  413. struct sk_buff_head rxq;
  414. struct sk_buff *skb;
  415. LIST_HEAD(notify);
  416. int ret;
  417. unsigned long offset;
  418. bool need_to_notify = false;
  419. struct netrx_pending_operations npo = {
  420. .copy = vif->grant_copy_op,
  421. .meta = vif->meta,
  422. };
  423. skb_queue_head_init(&rxq);
  424. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
  425. RING_IDX max_slots_needed;
  426. int i;
  427. /* We need a cheap worse case estimate for the number of
  428. * slots we'll use.
  429. */
  430. max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
  431. skb_headlen(skb),
  432. PAGE_SIZE);
  433. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  434. unsigned int size;
  435. size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  436. max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
  437. }
  438. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
  439. skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  440. max_slots_needed++;
  441. /* If the skb may not fit then bail out now */
  442. if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
  443. skb_queue_head(&vif->rx_queue, skb);
  444. need_to_notify = true;
  445. vif->rx_last_skb_slots = max_slots_needed;
  446. break;
  447. } else
  448. vif->rx_last_skb_slots = 0;
  449. XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
  450. BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
  451. __skb_queue_tail(&rxq, skb);
  452. }
  453. BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
  454. if (!npo.copy_prod)
  455. goto done;
  456. BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
  457. gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
  458. while ((skb = __skb_dequeue(&rxq)) != NULL) {
  459. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  460. vif->gso_prefix_mask) {
  461. resp = RING_GET_RESPONSE(&vif->rx,
  462. vif->rx.rsp_prod_pvt++);
  463. resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
  464. resp->offset = vif->meta[npo.meta_cons].gso_size;
  465. resp->id = vif->meta[npo.meta_cons].id;
  466. resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
  467. npo.meta_cons++;
  468. XENVIF_RX_CB(skb)->meta_slots_used--;
  469. }
  470. vif->dev->stats.tx_bytes += skb->len;
  471. vif->dev->stats.tx_packets++;
  472. status = xenvif_check_gop(vif,
  473. XENVIF_RX_CB(skb)->meta_slots_used,
  474. &npo);
  475. if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
  476. flags = 0;
  477. else
  478. flags = XEN_NETRXF_more_data;
  479. if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
  480. flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
  481. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  482. /* remote but checksummed. */
  483. flags |= XEN_NETRXF_data_validated;
  484. offset = 0;
  485. resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
  486. status, offset,
  487. vif->meta[npo.meta_cons].size,
  488. flags);
  489. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  490. vif->gso_mask) {
  491. struct xen_netif_extra_info *gso =
  492. (struct xen_netif_extra_info *)
  493. RING_GET_RESPONSE(&vif->rx,
  494. vif->rx.rsp_prod_pvt++);
  495. resp->flags |= XEN_NETRXF_extra_info;
  496. gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
  497. gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
  498. gso->u.gso.pad = 0;
  499. gso->u.gso.features = 0;
  500. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  501. gso->flags = 0;
  502. }
  503. xenvif_add_frag_responses(vif, status,
  504. vif->meta + npo.meta_cons + 1,
  505. XENVIF_RX_CB(skb)->meta_slots_used);
  506. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
  507. need_to_notify |= !!ret;
  508. npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
  509. dev_kfree_skb(skb);
  510. }
  511. done:
  512. if (need_to_notify)
  513. notify_remote_via_irq(vif->rx_irq);
  514. }
  515. void xenvif_check_rx_xenvif(struct xenvif *vif)
  516. {
  517. int more_to_do;
  518. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
  519. if (more_to_do)
  520. napi_schedule(&vif->napi);
  521. }
  522. static void tx_add_credit(struct xenvif *vif)
  523. {
  524. unsigned long max_burst, max_credit;
  525. /*
  526. * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
  527. * Otherwise the interface can seize up due to insufficient credit.
  528. */
  529. max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
  530. max_burst = min(max_burst, 131072UL);
  531. max_burst = max(max_burst, vif->credit_bytes);
  532. /* Take care that adding a new chunk of credit doesn't wrap to zero. */
  533. max_credit = vif->remaining_credit + vif->credit_bytes;
  534. if (max_credit < vif->remaining_credit)
  535. max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
  536. vif->remaining_credit = min(max_credit, max_burst);
  537. }
  538. static void tx_credit_callback(unsigned long data)
  539. {
  540. struct xenvif *vif = (struct xenvif *)data;
  541. tx_add_credit(vif);
  542. xenvif_check_rx_xenvif(vif);
  543. }
  544. static void xenvif_tx_err(struct xenvif *vif,
  545. struct xen_netif_tx_request *txp, RING_IDX end)
  546. {
  547. RING_IDX cons = vif->tx.req_cons;
  548. unsigned long flags;
  549. do {
  550. spin_lock_irqsave(&vif->response_lock, flags);
  551. make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
  552. spin_unlock_irqrestore(&vif->response_lock, flags);
  553. if (cons == end)
  554. break;
  555. txp = RING_GET_REQUEST(&vif->tx, cons++);
  556. } while (1);
  557. vif->tx.req_cons = cons;
  558. }
  559. static void xenvif_fatal_tx_err(struct xenvif *vif)
  560. {
  561. netdev_err(vif->dev, "fatal error; disabling device\n");
  562. xenvif_carrier_off(vif);
  563. }
  564. static int xenvif_count_requests(struct xenvif *vif,
  565. struct xen_netif_tx_request *first,
  566. struct xen_netif_tx_request *txp,
  567. int work_to_do)
  568. {
  569. RING_IDX cons = vif->tx.req_cons;
  570. int slots = 0;
  571. int drop_err = 0;
  572. int more_data;
  573. if (!(first->flags & XEN_NETTXF_more_data))
  574. return 0;
  575. do {
  576. struct xen_netif_tx_request dropped_tx = { 0 };
  577. if (slots >= work_to_do) {
  578. netdev_err(vif->dev,
  579. "Asked for %d slots but exceeds this limit\n",
  580. work_to_do);
  581. xenvif_fatal_tx_err(vif);
  582. return -ENODATA;
  583. }
  584. /* This guest is really using too many slots and
  585. * considered malicious.
  586. */
  587. if (unlikely(slots >= fatal_skb_slots)) {
  588. netdev_err(vif->dev,
  589. "Malicious frontend using %d slots, threshold %u\n",
  590. slots, fatal_skb_slots);
  591. xenvif_fatal_tx_err(vif);
  592. return -E2BIG;
  593. }
  594. /* Xen network protocol had implicit dependency on
  595. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  596. * the historical MAX_SKB_FRAGS value 18 to honor the
  597. * same behavior as before. Any packet using more than
  598. * 18 slots but less than fatal_skb_slots slots is
  599. * dropped
  600. */
  601. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  602. if (net_ratelimit())
  603. netdev_dbg(vif->dev,
  604. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  605. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  606. drop_err = -E2BIG;
  607. }
  608. if (drop_err)
  609. txp = &dropped_tx;
  610. memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
  611. sizeof(*txp));
  612. /* If the guest submitted a frame >= 64 KiB then
  613. * first->size overflowed and following slots will
  614. * appear to be larger than the frame.
  615. *
  616. * This cannot be fatal error as there are buggy
  617. * frontends that do this.
  618. *
  619. * Consume all slots and drop the packet.
  620. */
  621. if (!drop_err && txp->size > first->size) {
  622. if (net_ratelimit())
  623. netdev_dbg(vif->dev,
  624. "Invalid tx request, slot size %u > remaining size %u\n",
  625. txp->size, first->size);
  626. drop_err = -EIO;
  627. }
  628. first->size -= txp->size;
  629. slots++;
  630. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  631. netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
  632. txp->offset, txp->size);
  633. xenvif_fatal_tx_err(vif);
  634. return -EINVAL;
  635. }
  636. more_data = txp->flags & XEN_NETTXF_more_data;
  637. if (!drop_err)
  638. txp++;
  639. } while (more_data);
  640. if (drop_err) {
  641. xenvif_tx_err(vif, first, cons + slots);
  642. return drop_err;
  643. }
  644. return slots;
  645. }
  646. struct xenvif_tx_cb {
  647. u16 pending_idx;
  648. };
  649. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  650. static inline void xenvif_tx_create_gop(struct xenvif *vif,
  651. u16 pending_idx,
  652. struct xen_netif_tx_request *txp,
  653. struct gnttab_map_grant_ref *gop)
  654. {
  655. vif->pages_to_map[gop-vif->tx_map_ops] = vif->mmap_pages[pending_idx];
  656. gnttab_set_map_op(gop, idx_to_kaddr(vif, pending_idx),
  657. GNTMAP_host_map | GNTMAP_readonly,
  658. txp->gref, vif->domid);
  659. memcpy(&vif->pending_tx_info[pending_idx].req, txp,
  660. sizeof(*txp));
  661. }
  662. static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
  663. {
  664. struct sk_buff *skb =
  665. alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
  666. GFP_ATOMIC | __GFP_NOWARN);
  667. if (unlikely(skb == NULL))
  668. return NULL;
  669. /* Packets passed to netif_rx() must have some headroom. */
  670. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  671. /* Initialize it here to avoid later surprises */
  672. skb_shinfo(skb)->destructor_arg = NULL;
  673. return skb;
  674. }
  675. static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif *vif,
  676. struct sk_buff *skb,
  677. struct xen_netif_tx_request *txp,
  678. struct gnttab_map_grant_ref *gop)
  679. {
  680. struct skb_shared_info *shinfo = skb_shinfo(skb);
  681. skb_frag_t *frags = shinfo->frags;
  682. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  683. int start;
  684. pending_ring_idx_t index;
  685. unsigned int nr_slots, frag_overflow = 0;
  686. /* At this point shinfo->nr_frags is in fact the number of
  687. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  688. */
  689. if (shinfo->nr_frags > MAX_SKB_FRAGS) {
  690. frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
  691. BUG_ON(frag_overflow > MAX_SKB_FRAGS);
  692. shinfo->nr_frags = MAX_SKB_FRAGS;
  693. }
  694. nr_slots = shinfo->nr_frags;
  695. /* Skip first skb fragment if it is on same page as header fragment. */
  696. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  697. for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
  698. shinfo->nr_frags++, txp++, gop++) {
  699. index = pending_index(vif->pending_cons++);
  700. pending_idx = vif->pending_ring[index];
  701. xenvif_tx_create_gop(vif, pending_idx, txp, gop);
  702. frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
  703. }
  704. if (frag_overflow) {
  705. struct sk_buff *nskb = xenvif_alloc_skb(0);
  706. if (unlikely(nskb == NULL)) {
  707. if (net_ratelimit())
  708. netdev_err(vif->dev,
  709. "Can't allocate the frag_list skb.\n");
  710. return NULL;
  711. }
  712. shinfo = skb_shinfo(nskb);
  713. frags = shinfo->frags;
  714. for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
  715. shinfo->nr_frags++, txp++, gop++) {
  716. index = pending_index(vif->pending_cons++);
  717. pending_idx = vif->pending_ring[index];
  718. xenvif_tx_create_gop(vif, pending_idx, txp, gop);
  719. frag_set_pending_idx(&frags[shinfo->nr_frags],
  720. pending_idx);
  721. }
  722. skb_shinfo(skb)->frag_list = nskb;
  723. }
  724. return gop;
  725. }
  726. static inline void xenvif_grant_handle_set(struct xenvif *vif,
  727. u16 pending_idx,
  728. grant_handle_t handle)
  729. {
  730. if (unlikely(vif->grant_tx_handle[pending_idx] !=
  731. NETBACK_INVALID_HANDLE)) {
  732. netdev_err(vif->dev,
  733. "Trying to overwrite active handle! pending_idx: %x\n",
  734. pending_idx);
  735. BUG();
  736. }
  737. vif->grant_tx_handle[pending_idx] = handle;
  738. }
  739. static inline void xenvif_grant_handle_reset(struct xenvif *vif,
  740. u16 pending_idx)
  741. {
  742. if (unlikely(vif->grant_tx_handle[pending_idx] ==
  743. NETBACK_INVALID_HANDLE)) {
  744. netdev_err(vif->dev,
  745. "Trying to unmap invalid handle! pending_idx: %x\n",
  746. pending_idx);
  747. BUG();
  748. }
  749. vif->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
  750. }
  751. static int xenvif_tx_check_gop(struct xenvif *vif,
  752. struct sk_buff *skb,
  753. struct gnttab_map_grant_ref **gopp)
  754. {
  755. struct gnttab_map_grant_ref *gop = *gopp;
  756. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  757. struct skb_shared_info *shinfo = skb_shinfo(skb);
  758. struct pending_tx_info *tx_info;
  759. int nr_frags = shinfo->nr_frags;
  760. int i, err, start;
  761. struct sk_buff *first_skb = NULL;
  762. /* Check status of header. */
  763. err = gop->status;
  764. if (unlikely(err))
  765. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  766. else
  767. xenvif_grant_handle_set(vif, pending_idx , gop->handle);
  768. /* Skip first skb fragment if it is on same page as header fragment. */
  769. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  770. check_frags:
  771. for (i = start; i < nr_frags; i++) {
  772. int j, newerr;
  773. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  774. tx_info = &vif->pending_tx_info[pending_idx];
  775. /* Check error status: if okay then remember grant handle. */
  776. newerr = (++gop)->status;
  777. if (likely(!newerr)) {
  778. xenvif_grant_handle_set(vif, pending_idx , gop->handle);
  779. /* Had a previous error? Invalidate this fragment. */
  780. if (unlikely(err))
  781. xenvif_idx_unmap(vif, pending_idx);
  782. continue;
  783. }
  784. /* Error on this fragment: respond to client with an error. */
  785. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  786. /* Not the first error? Preceding frags already invalidated. */
  787. if (err)
  788. continue;
  789. /* First error: invalidate header and preceding fragments. */
  790. if (!first_skb)
  791. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  792. else
  793. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  794. xenvif_idx_unmap(vif, pending_idx);
  795. for (j = start; j < i; j++) {
  796. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  797. xenvif_idx_unmap(vif, pending_idx);
  798. }
  799. /* Remember the error: invalidate all subsequent fragments. */
  800. err = newerr;
  801. }
  802. if (skb_has_frag_list(skb)) {
  803. first_skb = skb;
  804. skb = shinfo->frag_list;
  805. shinfo = skb_shinfo(skb);
  806. nr_frags = shinfo->nr_frags;
  807. start = 0;
  808. goto check_frags;
  809. }
  810. /* There was a mapping error in the frag_list skb. We have to unmap
  811. * the first skb's frags
  812. */
  813. if (first_skb && err) {
  814. int j;
  815. shinfo = skb_shinfo(first_skb);
  816. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  817. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  818. for (j = start; j < shinfo->nr_frags; j++) {
  819. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  820. xenvif_idx_unmap(vif, pending_idx);
  821. }
  822. }
  823. *gopp = gop + 1;
  824. return err;
  825. }
  826. static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
  827. {
  828. struct skb_shared_info *shinfo = skb_shinfo(skb);
  829. int nr_frags = shinfo->nr_frags;
  830. int i;
  831. u16 prev_pending_idx = INVALID_PENDING_IDX;
  832. if (skb_shinfo(skb)->destructor_arg)
  833. prev_pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  834. for (i = 0; i < nr_frags; i++) {
  835. skb_frag_t *frag = shinfo->frags + i;
  836. struct xen_netif_tx_request *txp;
  837. struct page *page;
  838. u16 pending_idx;
  839. pending_idx = frag_get_pending_idx(frag);
  840. /* If this is not the first frag, chain it to the previous*/
  841. if (unlikely(prev_pending_idx == INVALID_PENDING_IDX))
  842. skb_shinfo(skb)->destructor_arg =
  843. &vif->pending_tx_info[pending_idx].callback_struct;
  844. else if (likely(pending_idx != prev_pending_idx))
  845. vif->pending_tx_info[prev_pending_idx].callback_struct.ctx =
  846. &(vif->pending_tx_info[pending_idx].callback_struct);
  847. vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
  848. prev_pending_idx = pending_idx;
  849. txp = &vif->pending_tx_info[pending_idx].req;
  850. page = virt_to_page(idx_to_kaddr(vif, pending_idx));
  851. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  852. skb->len += txp->size;
  853. skb->data_len += txp->size;
  854. skb->truesize += txp->size;
  855. /* Take an extra reference to offset network stack's put_page */
  856. get_page(vif->mmap_pages[pending_idx]);
  857. }
  858. /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
  859. * overlaps with "index", and "mapping" is not set. I think mapping
  860. * should be set. If delivered to local stack, it would drop this
  861. * skb in sk_filter unless the socket has the right to use it.
  862. */
  863. skb->pfmemalloc = false;
  864. }
  865. static int xenvif_get_extras(struct xenvif *vif,
  866. struct xen_netif_extra_info *extras,
  867. int work_to_do)
  868. {
  869. struct xen_netif_extra_info extra;
  870. RING_IDX cons = vif->tx.req_cons;
  871. do {
  872. if (unlikely(work_to_do-- <= 0)) {
  873. netdev_err(vif->dev, "Missing extra info\n");
  874. xenvif_fatal_tx_err(vif);
  875. return -EBADR;
  876. }
  877. memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
  878. sizeof(extra));
  879. if (unlikely(!extra.type ||
  880. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  881. vif->tx.req_cons = ++cons;
  882. netdev_err(vif->dev,
  883. "Invalid extra type: %d\n", extra.type);
  884. xenvif_fatal_tx_err(vif);
  885. return -EINVAL;
  886. }
  887. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  888. vif->tx.req_cons = ++cons;
  889. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  890. return work_to_do;
  891. }
  892. static int xenvif_set_skb_gso(struct xenvif *vif,
  893. struct sk_buff *skb,
  894. struct xen_netif_extra_info *gso)
  895. {
  896. if (!gso->u.gso.size) {
  897. netdev_err(vif->dev, "GSO size must not be zero.\n");
  898. xenvif_fatal_tx_err(vif);
  899. return -EINVAL;
  900. }
  901. switch (gso->u.gso.type) {
  902. case XEN_NETIF_GSO_TYPE_TCPV4:
  903. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  904. break;
  905. case XEN_NETIF_GSO_TYPE_TCPV6:
  906. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  907. break;
  908. default:
  909. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  910. xenvif_fatal_tx_err(vif);
  911. return -EINVAL;
  912. }
  913. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  914. /* gso_segs will be calculated later */
  915. return 0;
  916. }
  917. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  918. {
  919. bool recalculate_partial_csum = false;
  920. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  921. * peers can fail to set NETRXF_csum_blank when sending a GSO
  922. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  923. * recalculate the partial checksum.
  924. */
  925. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  926. vif->rx_gso_checksum_fixup++;
  927. skb->ip_summed = CHECKSUM_PARTIAL;
  928. recalculate_partial_csum = true;
  929. }
  930. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  931. if (skb->ip_summed != CHECKSUM_PARTIAL)
  932. return 0;
  933. return skb_checksum_setup(skb, recalculate_partial_csum);
  934. }
  935. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  936. {
  937. u64 now = get_jiffies_64();
  938. u64 next_credit = vif->credit_window_start +
  939. msecs_to_jiffies(vif->credit_usec / 1000);
  940. /* Timer could already be pending in rare cases. */
  941. if (timer_pending(&vif->credit_timeout))
  942. return true;
  943. /* Passed the point where we can replenish credit? */
  944. if (time_after_eq64(now, next_credit)) {
  945. vif->credit_window_start = now;
  946. tx_add_credit(vif);
  947. }
  948. /* Still too big to send right now? Set a callback. */
  949. if (size > vif->remaining_credit) {
  950. vif->credit_timeout.data =
  951. (unsigned long)vif;
  952. vif->credit_timeout.function =
  953. tx_credit_callback;
  954. mod_timer(&vif->credit_timeout,
  955. next_credit);
  956. vif->credit_window_start = next_credit;
  957. return true;
  958. }
  959. return false;
  960. }
  961. static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
  962. {
  963. struct gnttab_map_grant_ref *gop = vif->tx_map_ops, *request_gop;
  964. struct sk_buff *skb;
  965. int ret;
  966. while (xenvif_tx_pending_slots_available(vif) &&
  967. (skb_queue_len(&vif->tx_queue) < budget)) {
  968. struct xen_netif_tx_request txreq;
  969. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  970. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  971. u16 pending_idx;
  972. RING_IDX idx;
  973. int work_to_do;
  974. unsigned int data_len;
  975. pending_ring_idx_t index;
  976. if (vif->tx.sring->req_prod - vif->tx.req_cons >
  977. XEN_NETIF_TX_RING_SIZE) {
  978. netdev_err(vif->dev,
  979. "Impossible number of requests. "
  980. "req_prod %d, req_cons %d, size %ld\n",
  981. vif->tx.sring->req_prod, vif->tx.req_cons,
  982. XEN_NETIF_TX_RING_SIZE);
  983. xenvif_fatal_tx_err(vif);
  984. continue;
  985. }
  986. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
  987. if (!work_to_do)
  988. break;
  989. idx = vif->tx.req_cons;
  990. rmb(); /* Ensure that we see the request before we copy it. */
  991. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  992. /* Credit-based scheduling. */
  993. if (txreq.size > vif->remaining_credit &&
  994. tx_credit_exceeded(vif, txreq.size))
  995. break;
  996. vif->remaining_credit -= txreq.size;
  997. work_to_do--;
  998. vif->tx.req_cons = ++idx;
  999. memset(extras, 0, sizeof(extras));
  1000. if (txreq.flags & XEN_NETTXF_extra_info) {
  1001. work_to_do = xenvif_get_extras(vif, extras,
  1002. work_to_do);
  1003. idx = vif->tx.req_cons;
  1004. if (unlikely(work_to_do < 0))
  1005. break;
  1006. }
  1007. ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
  1008. if (unlikely(ret < 0))
  1009. break;
  1010. idx += ret;
  1011. if (unlikely(txreq.size < ETH_HLEN)) {
  1012. netdev_dbg(vif->dev,
  1013. "Bad packet size: %d\n", txreq.size);
  1014. xenvif_tx_err(vif, &txreq, idx);
  1015. break;
  1016. }
  1017. /* No crossing a page as the payload mustn't fragment. */
  1018. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1019. netdev_err(vif->dev,
  1020. "txreq.offset: %x, size: %u, end: %lu\n",
  1021. txreq.offset, txreq.size,
  1022. (txreq.offset&~PAGE_MASK) + txreq.size);
  1023. xenvif_fatal_tx_err(vif);
  1024. break;
  1025. }
  1026. index = pending_index(vif->pending_cons);
  1027. pending_idx = vif->pending_ring[index];
  1028. data_len = (txreq.size > PKT_PROT_LEN &&
  1029. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  1030. PKT_PROT_LEN : txreq.size;
  1031. skb = xenvif_alloc_skb(data_len);
  1032. if (unlikely(skb == NULL)) {
  1033. netdev_dbg(vif->dev,
  1034. "Can't allocate a skb in start_xmit.\n");
  1035. xenvif_tx_err(vif, &txreq, idx);
  1036. break;
  1037. }
  1038. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1039. struct xen_netif_extra_info *gso;
  1040. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1041. if (xenvif_set_skb_gso(vif, skb, gso)) {
  1042. /* Failure in xenvif_set_skb_gso is fatal. */
  1043. kfree_skb(skb);
  1044. break;
  1045. }
  1046. }
  1047. xenvif_tx_create_gop(vif, pending_idx, &txreq, gop);
  1048. gop++;
  1049. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  1050. __skb_put(skb, data_len);
  1051. skb_shinfo(skb)->nr_frags = ret;
  1052. if (data_len < txreq.size) {
  1053. skb_shinfo(skb)->nr_frags++;
  1054. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1055. pending_idx);
  1056. } else {
  1057. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1058. INVALID_PENDING_IDX);
  1059. }
  1060. vif->pending_cons++;
  1061. request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
  1062. if (request_gop == NULL) {
  1063. kfree_skb(skb);
  1064. xenvif_tx_err(vif, &txreq, idx);
  1065. break;
  1066. }
  1067. gop = request_gop;
  1068. __skb_queue_tail(&vif->tx_queue, skb);
  1069. vif->tx.req_cons = idx;
  1070. if ((gop-vif->tx_map_ops) >= ARRAY_SIZE(vif->tx_map_ops))
  1071. break;
  1072. }
  1073. return gop - vif->tx_map_ops;
  1074. }
  1075. /* Consolidate skb with a frag_list into a brand new one with local pages on
  1076. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  1077. */
  1078. static int xenvif_handle_frag_list(struct xenvif *vif, struct sk_buff *skb)
  1079. {
  1080. unsigned int offset = skb_headlen(skb);
  1081. skb_frag_t frags[MAX_SKB_FRAGS];
  1082. int i;
  1083. struct ubuf_info *uarg;
  1084. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  1085. vif->tx_zerocopy_sent += 2;
  1086. vif->tx_frag_overflow++;
  1087. xenvif_fill_frags(vif, nskb);
  1088. /* Subtract frags size, we will correct it later */
  1089. skb->truesize -= skb->data_len;
  1090. skb->len += nskb->len;
  1091. skb->data_len += nskb->len;
  1092. /* create a brand new frags array and coalesce there */
  1093. for (i = 0; offset < skb->len; i++) {
  1094. struct page *page;
  1095. unsigned int len;
  1096. BUG_ON(i >= MAX_SKB_FRAGS);
  1097. page = alloc_page(GFP_ATOMIC|__GFP_COLD);
  1098. if (!page) {
  1099. int j;
  1100. skb->truesize += skb->data_len;
  1101. for (j = 0; j < i; j++)
  1102. put_page(frags[j].page.p);
  1103. return -ENOMEM;
  1104. }
  1105. if (offset + PAGE_SIZE < skb->len)
  1106. len = PAGE_SIZE;
  1107. else
  1108. len = skb->len - offset;
  1109. if (skb_copy_bits(skb, offset, page_address(page), len))
  1110. BUG();
  1111. offset += len;
  1112. frags[i].page.p = page;
  1113. frags[i].page_offset = 0;
  1114. skb_frag_size_set(&frags[i], len);
  1115. }
  1116. /* swap out with old one */
  1117. memcpy(skb_shinfo(skb)->frags,
  1118. frags,
  1119. i * sizeof(skb_frag_t));
  1120. skb_shinfo(skb)->nr_frags = i;
  1121. skb->truesize += i * PAGE_SIZE;
  1122. /* remove traces of mapped pages and frag_list */
  1123. skb_frag_list_init(skb);
  1124. uarg = skb_shinfo(skb)->destructor_arg;
  1125. uarg->callback(uarg, true);
  1126. skb_shinfo(skb)->destructor_arg = NULL;
  1127. skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1128. kfree_skb(nskb);
  1129. return 0;
  1130. }
  1131. static int xenvif_tx_submit(struct xenvif *vif)
  1132. {
  1133. struct gnttab_map_grant_ref *gop = vif->tx_map_ops;
  1134. struct sk_buff *skb;
  1135. int work_done = 0;
  1136. while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
  1137. struct xen_netif_tx_request *txp;
  1138. u16 pending_idx;
  1139. unsigned data_len;
  1140. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  1141. txp = &vif->pending_tx_info[pending_idx].req;
  1142. /* Check the remap error code. */
  1143. if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
  1144. netdev_dbg(vif->dev, "netback grant failed.\n");
  1145. skb_shinfo(skb)->nr_frags = 0;
  1146. kfree_skb(skb);
  1147. continue;
  1148. }
  1149. data_len = skb->len;
  1150. memcpy(skb->data,
  1151. (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
  1152. data_len);
  1153. vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
  1154. if (data_len < txp->size) {
  1155. /* Append the packet payload as a fragment. */
  1156. txp->offset += data_len;
  1157. txp->size -= data_len;
  1158. skb_shinfo(skb)->destructor_arg =
  1159. &vif->pending_tx_info[pending_idx].callback_struct;
  1160. } else {
  1161. /* Schedule a response immediately. */
  1162. xenvif_idx_unmap(vif, pending_idx);
  1163. }
  1164. if (txp->flags & XEN_NETTXF_csum_blank)
  1165. skb->ip_summed = CHECKSUM_PARTIAL;
  1166. else if (txp->flags & XEN_NETTXF_data_validated)
  1167. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1168. xenvif_fill_frags(vif, skb);
  1169. if (unlikely(skb_has_frag_list(skb))) {
  1170. if (xenvif_handle_frag_list(vif, skb)) {
  1171. if (net_ratelimit())
  1172. netdev_err(vif->dev,
  1173. "Not enough memory to consolidate frag_list!\n");
  1174. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1175. kfree_skb(skb);
  1176. continue;
  1177. }
  1178. }
  1179. if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
  1180. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1181. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1182. }
  1183. skb->dev = vif->dev;
  1184. skb->protocol = eth_type_trans(skb, skb->dev);
  1185. skb_reset_network_header(skb);
  1186. if (checksum_setup(vif, skb)) {
  1187. netdev_dbg(vif->dev,
  1188. "Can't setup checksum in net_tx_action\n");
  1189. /* We have to set this flag to trigger the callback */
  1190. if (skb_shinfo(skb)->destructor_arg)
  1191. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1192. kfree_skb(skb);
  1193. continue;
  1194. }
  1195. skb_probe_transport_header(skb, 0);
  1196. /* If the packet is GSO then we will have just set up the
  1197. * transport header offset in checksum_setup so it's now
  1198. * straightforward to calculate gso_segs.
  1199. */
  1200. if (skb_is_gso(skb)) {
  1201. int mss = skb_shinfo(skb)->gso_size;
  1202. int hdrlen = skb_transport_header(skb) -
  1203. skb_mac_header(skb) +
  1204. tcp_hdrlen(skb);
  1205. skb_shinfo(skb)->gso_segs =
  1206. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1207. }
  1208. vif->dev->stats.rx_bytes += skb->len;
  1209. vif->dev->stats.rx_packets++;
  1210. work_done++;
  1211. /* Set this flag right before netif_receive_skb, otherwise
  1212. * someone might think this packet already left netback, and
  1213. * do a skb_copy_ubufs while we are still in control of the
  1214. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1215. */
  1216. if (skb_shinfo(skb)->destructor_arg) {
  1217. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1218. vif->tx_zerocopy_sent++;
  1219. }
  1220. netif_receive_skb(skb);
  1221. }
  1222. return work_done;
  1223. }
  1224. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1225. {
  1226. unsigned long flags;
  1227. pending_ring_idx_t index;
  1228. struct xenvif *vif = ubuf_to_vif(ubuf);
  1229. /* This is the only place where we grab this lock, to protect callbacks
  1230. * from each other.
  1231. */
  1232. spin_lock_irqsave(&vif->callback_lock, flags);
  1233. do {
  1234. u16 pending_idx = ubuf->desc;
  1235. ubuf = (struct ubuf_info *) ubuf->ctx;
  1236. BUG_ON(vif->dealloc_prod - vif->dealloc_cons >=
  1237. MAX_PENDING_REQS);
  1238. index = pending_index(vif->dealloc_prod);
  1239. vif->dealloc_ring[index] = pending_idx;
  1240. /* Sync with xenvif_tx_dealloc_action:
  1241. * insert idx then incr producer.
  1242. */
  1243. smp_wmb();
  1244. vif->dealloc_prod++;
  1245. } while (ubuf);
  1246. wake_up(&vif->dealloc_wq);
  1247. spin_unlock_irqrestore(&vif->callback_lock, flags);
  1248. if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx) &&
  1249. xenvif_tx_pending_slots_available(vif)) {
  1250. local_bh_disable();
  1251. napi_schedule(&vif->napi);
  1252. local_bh_enable();
  1253. }
  1254. if (likely(zerocopy_success))
  1255. vif->tx_zerocopy_success++;
  1256. else
  1257. vif->tx_zerocopy_fail++;
  1258. }
  1259. static inline void xenvif_tx_dealloc_action(struct xenvif *vif)
  1260. {
  1261. struct gnttab_unmap_grant_ref *gop;
  1262. pending_ring_idx_t dc, dp;
  1263. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1264. unsigned int i = 0;
  1265. dc = vif->dealloc_cons;
  1266. gop = vif->tx_unmap_ops;
  1267. /* Free up any grants we have finished using */
  1268. do {
  1269. dp = vif->dealloc_prod;
  1270. /* Ensure we see all indices enqueued by all
  1271. * xenvif_zerocopy_callback().
  1272. */
  1273. smp_rmb();
  1274. while (dc != dp) {
  1275. BUG_ON(gop - vif->tx_unmap_ops > MAX_PENDING_REQS);
  1276. pending_idx =
  1277. vif->dealloc_ring[pending_index(dc++)];
  1278. pending_idx_release[gop-vif->tx_unmap_ops] =
  1279. pending_idx;
  1280. vif->pages_to_unmap[gop-vif->tx_unmap_ops] =
  1281. vif->mmap_pages[pending_idx];
  1282. gnttab_set_unmap_op(gop,
  1283. idx_to_kaddr(vif, pending_idx),
  1284. GNTMAP_host_map,
  1285. vif->grant_tx_handle[pending_idx]);
  1286. /* Btw. already unmapped? */
  1287. xenvif_grant_handle_reset(vif, pending_idx);
  1288. ++gop;
  1289. }
  1290. } while (dp != vif->dealloc_prod);
  1291. vif->dealloc_cons = dc;
  1292. if (gop - vif->tx_unmap_ops > 0) {
  1293. int ret;
  1294. ret = gnttab_unmap_refs(vif->tx_unmap_ops,
  1295. NULL,
  1296. vif->pages_to_unmap,
  1297. gop - vif->tx_unmap_ops);
  1298. if (ret) {
  1299. netdev_err(vif->dev, "Unmap fail: nr_ops %x ret %d\n",
  1300. gop - vif->tx_unmap_ops, ret);
  1301. for (i = 0; i < gop - vif->tx_unmap_ops; ++i) {
  1302. if (gop[i].status != GNTST_okay)
  1303. netdev_err(vif->dev,
  1304. " host_addr: %llx handle: %x status: %d\n",
  1305. gop[i].host_addr,
  1306. gop[i].handle,
  1307. gop[i].status);
  1308. }
  1309. BUG();
  1310. }
  1311. }
  1312. for (i = 0; i < gop - vif->tx_unmap_ops; ++i)
  1313. xenvif_idx_release(vif, pending_idx_release[i],
  1314. XEN_NETIF_RSP_OKAY);
  1315. }
  1316. /* Called after netfront has transmitted */
  1317. int xenvif_tx_action(struct xenvif *vif, int budget)
  1318. {
  1319. unsigned nr_gops;
  1320. int work_done, ret;
  1321. if (unlikely(!tx_work_todo(vif)))
  1322. return 0;
  1323. nr_gops = xenvif_tx_build_gops(vif, budget);
  1324. if (nr_gops == 0)
  1325. return 0;
  1326. ret = gnttab_map_refs(vif->tx_map_ops,
  1327. NULL,
  1328. vif->pages_to_map,
  1329. nr_gops);
  1330. BUG_ON(ret);
  1331. work_done = xenvif_tx_submit(vif);
  1332. return work_done;
  1333. }
  1334. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  1335. u8 status)
  1336. {
  1337. struct pending_tx_info *pending_tx_info;
  1338. pending_ring_idx_t index;
  1339. unsigned long flags;
  1340. pending_tx_info = &vif->pending_tx_info[pending_idx];
  1341. spin_lock_irqsave(&vif->response_lock, flags);
  1342. make_tx_response(vif, &pending_tx_info->req, status);
  1343. index = pending_index(vif->pending_prod);
  1344. vif->pending_ring[index] = pending_idx;
  1345. /* TX shouldn't use the index before we give it back here */
  1346. mb();
  1347. vif->pending_prod++;
  1348. spin_unlock_irqrestore(&vif->response_lock, flags);
  1349. }
  1350. static void make_tx_response(struct xenvif *vif,
  1351. struct xen_netif_tx_request *txp,
  1352. s8 st)
  1353. {
  1354. RING_IDX i = vif->tx.rsp_prod_pvt;
  1355. struct xen_netif_tx_response *resp;
  1356. int notify;
  1357. resp = RING_GET_RESPONSE(&vif->tx, i);
  1358. resp->id = txp->id;
  1359. resp->status = st;
  1360. if (txp->flags & XEN_NETTXF_extra_info)
  1361. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1362. vif->tx.rsp_prod_pvt = ++i;
  1363. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1364. if (notify)
  1365. notify_remote_via_irq(vif->tx_irq);
  1366. }
  1367. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1368. u16 id,
  1369. s8 st,
  1370. u16 offset,
  1371. u16 size,
  1372. u16 flags)
  1373. {
  1374. RING_IDX i = vif->rx.rsp_prod_pvt;
  1375. struct xen_netif_rx_response *resp;
  1376. resp = RING_GET_RESPONSE(&vif->rx, i);
  1377. resp->offset = offset;
  1378. resp->flags = flags;
  1379. resp->id = id;
  1380. resp->status = (s16)size;
  1381. if (st < 0)
  1382. resp->status = (s16)st;
  1383. vif->rx.rsp_prod_pvt = ++i;
  1384. return resp;
  1385. }
  1386. void xenvif_idx_unmap(struct xenvif *vif, u16 pending_idx)
  1387. {
  1388. int ret;
  1389. struct gnttab_unmap_grant_ref tx_unmap_op;
  1390. gnttab_set_unmap_op(&tx_unmap_op,
  1391. idx_to_kaddr(vif, pending_idx),
  1392. GNTMAP_host_map,
  1393. vif->grant_tx_handle[pending_idx]);
  1394. /* Btw. already unmapped? */
  1395. xenvif_grant_handle_reset(vif, pending_idx);
  1396. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1397. &vif->mmap_pages[pending_idx], 1);
  1398. BUG_ON(ret);
  1399. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
  1400. }
  1401. static inline int rx_work_todo(struct xenvif *vif)
  1402. {
  1403. return !skb_queue_empty(&vif->rx_queue) &&
  1404. xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
  1405. }
  1406. static inline int tx_work_todo(struct xenvif *vif)
  1407. {
  1408. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
  1409. xenvif_tx_pending_slots_available(vif))
  1410. return 1;
  1411. return 0;
  1412. }
  1413. static inline bool tx_dealloc_work_todo(struct xenvif *vif)
  1414. {
  1415. return vif->dealloc_cons != vif->dealloc_prod;
  1416. }
  1417. void xenvif_unmap_frontend_rings(struct xenvif *vif)
  1418. {
  1419. if (vif->tx.sring)
  1420. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1421. vif->tx.sring);
  1422. if (vif->rx.sring)
  1423. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1424. vif->rx.sring);
  1425. }
  1426. int xenvif_map_frontend_rings(struct xenvif *vif,
  1427. grant_ref_t tx_ring_ref,
  1428. grant_ref_t rx_ring_ref)
  1429. {
  1430. void *addr;
  1431. struct xen_netif_tx_sring *txs;
  1432. struct xen_netif_rx_sring *rxs;
  1433. int err = -ENOMEM;
  1434. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1435. tx_ring_ref, &addr);
  1436. if (err)
  1437. goto err;
  1438. txs = (struct xen_netif_tx_sring *)addr;
  1439. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1440. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1441. rx_ring_ref, &addr);
  1442. if (err)
  1443. goto err;
  1444. rxs = (struct xen_netif_rx_sring *)addr;
  1445. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1446. return 0;
  1447. err:
  1448. xenvif_unmap_frontend_rings(vif);
  1449. return err;
  1450. }
  1451. void xenvif_stop_queue(struct xenvif *vif)
  1452. {
  1453. if (!vif->can_queue)
  1454. return;
  1455. netif_stop_queue(vif->dev);
  1456. }
  1457. static void xenvif_start_queue(struct xenvif *vif)
  1458. {
  1459. if (xenvif_schedulable(vif))
  1460. netif_wake_queue(vif->dev);
  1461. }
  1462. int xenvif_kthread_guest_rx(void *data)
  1463. {
  1464. struct xenvif *vif = data;
  1465. struct sk_buff *skb;
  1466. while (!kthread_should_stop()) {
  1467. wait_event_interruptible(vif->wq,
  1468. rx_work_todo(vif) ||
  1469. kthread_should_stop());
  1470. if (kthread_should_stop())
  1471. break;
  1472. if (!skb_queue_empty(&vif->rx_queue))
  1473. xenvif_rx_action(vif);
  1474. if (skb_queue_empty(&vif->rx_queue) &&
  1475. netif_queue_stopped(vif->dev))
  1476. xenvif_start_queue(vif);
  1477. cond_resched();
  1478. }
  1479. /* Bin any remaining skbs */
  1480. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
  1481. dev_kfree_skb(skb);
  1482. return 0;
  1483. }
  1484. int xenvif_dealloc_kthread(void *data)
  1485. {
  1486. struct xenvif *vif = data;
  1487. while (!kthread_should_stop()) {
  1488. wait_event_interruptible(vif->dealloc_wq,
  1489. tx_dealloc_work_todo(vif) ||
  1490. kthread_should_stop());
  1491. if (kthread_should_stop())
  1492. break;
  1493. xenvif_tx_dealloc_action(vif);
  1494. cond_resched();
  1495. }
  1496. /* Unmap anything remaining*/
  1497. if (tx_dealloc_work_todo(vif))
  1498. xenvif_tx_dealloc_action(vif);
  1499. return 0;
  1500. }
  1501. static int __init netback_init(void)
  1502. {
  1503. int rc = 0;
  1504. if (!xen_domain())
  1505. return -ENODEV;
  1506. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1507. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1508. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1509. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1510. }
  1511. rc = xenvif_xenbus_init();
  1512. if (rc)
  1513. goto failed_init;
  1514. return 0;
  1515. failed_init:
  1516. return rc;
  1517. }
  1518. module_init(netback_init);
  1519. static void __exit netback_fini(void)
  1520. {
  1521. xenvif_xenbus_fini();
  1522. }
  1523. module_exit(netback_fini);
  1524. MODULE_LICENSE("Dual BSD/GPL");
  1525. MODULE_ALIAS("xen-backend:vif");