netback.c 48 KB

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