netback.c 48 KB

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