netback.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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 the guest submitted a frame >= 64 KiB then
  791. * first->size overflowed and following slots will
  792. * appear to be larger than the frame.
  793. *
  794. * This cannot be fatal error as there are buggy
  795. * frontends that do this.
  796. *
  797. * Consume all slots and drop the packet.
  798. */
  799. if (!drop_err && txp->size > first->size) {
  800. if (net_ratelimit())
  801. netdev_dbg(vif->dev,
  802. "Invalid tx request, slot size %u > remaining size %u\n",
  803. txp->size, first->size);
  804. drop_err = -EIO;
  805. }
  806. first->size -= txp->size;
  807. slots++;
  808. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  809. netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
  810. txp->offset, txp->size);
  811. netbk_fatal_tx_err(vif);
  812. return -EINVAL;
  813. }
  814. } while ((txp++)->flags & XEN_NETTXF_more_data);
  815. if (drop_err) {
  816. netbk_tx_err(vif, first, first_idx + slots);
  817. return drop_err;
  818. }
  819. return slots;
  820. }
  821. static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
  822. u16 pending_idx)
  823. {
  824. struct page *page;
  825. page = alloc_page(GFP_KERNEL|__GFP_COLD);
  826. if (!page)
  827. return NULL;
  828. set_page_ext(page, netbk, pending_idx);
  829. netbk->mmap_pages[pending_idx] = page;
  830. return page;
  831. }
  832. static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
  833. struct xenvif *vif,
  834. struct sk_buff *skb,
  835. struct xen_netif_tx_request *txp,
  836. struct gnttab_copy *gop)
  837. {
  838. struct skb_shared_info *shinfo = skb_shinfo(skb);
  839. skb_frag_t *frags = shinfo->frags;
  840. u16 pending_idx = *((u16 *)skb->data);
  841. u16 head_idx = 0;
  842. int slot, start;
  843. struct page *page;
  844. pending_ring_idx_t index, start_idx = 0;
  845. uint16_t dst_offset;
  846. unsigned int nr_slots;
  847. struct pending_tx_info *first = NULL;
  848. /* At this point shinfo->nr_frags is in fact the number of
  849. * slots, which can be as large as XEN_NETIF_NR_SLOTS_MIN.
  850. */
  851. nr_slots = shinfo->nr_frags;
  852. /* Skip first skb fragment if it is on same page as header fragment. */
  853. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  854. /* Coalesce tx requests, at this point the packet passed in
  855. * should be <= 64K. Any packets larger than 64K have been
  856. * handled in netbk_count_requests().
  857. */
  858. for (shinfo->nr_frags = slot = start; slot < nr_slots;
  859. shinfo->nr_frags++) {
  860. struct pending_tx_info *pending_tx_info =
  861. netbk->pending_tx_info;
  862. page = alloc_page(GFP_KERNEL|__GFP_COLD);
  863. if (!page)
  864. goto err;
  865. dst_offset = 0;
  866. first = NULL;
  867. while (dst_offset < PAGE_SIZE && slot < nr_slots) {
  868. gop->flags = GNTCOPY_source_gref;
  869. gop->source.u.ref = txp->gref;
  870. gop->source.domid = vif->domid;
  871. gop->source.offset = txp->offset;
  872. gop->dest.domid = DOMID_SELF;
  873. gop->dest.offset = dst_offset;
  874. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  875. if (dst_offset + txp->size > PAGE_SIZE) {
  876. /* This page can only merge a portion
  877. * of tx request. Do not increment any
  878. * pointer / counter here. The txp
  879. * will be dealt with in future
  880. * rounds, eventually hitting the
  881. * `else` branch.
  882. */
  883. gop->len = PAGE_SIZE - dst_offset;
  884. txp->offset += gop->len;
  885. txp->size -= gop->len;
  886. dst_offset += gop->len; /* quit loop */
  887. } else {
  888. /* This tx request can be merged in the page */
  889. gop->len = txp->size;
  890. dst_offset += gop->len;
  891. index = pending_index(netbk->pending_cons++);
  892. pending_idx = netbk->pending_ring[index];
  893. memcpy(&pending_tx_info[pending_idx].req, txp,
  894. sizeof(*txp));
  895. xenvif_get(vif);
  896. pending_tx_info[pending_idx].vif = vif;
  897. /* Poison these fields, corresponding
  898. * fields for head tx req will be set
  899. * to correct values after the loop.
  900. */
  901. netbk->mmap_pages[pending_idx] = (void *)(~0UL);
  902. pending_tx_info[pending_idx].head =
  903. INVALID_PENDING_RING_IDX;
  904. if (!first) {
  905. first = &pending_tx_info[pending_idx];
  906. start_idx = index;
  907. head_idx = pending_idx;
  908. }
  909. txp++;
  910. slot++;
  911. }
  912. gop++;
  913. }
  914. first->req.offset = 0;
  915. first->req.size = dst_offset;
  916. first->head = start_idx;
  917. set_page_ext(page, netbk, head_idx);
  918. netbk->mmap_pages[head_idx] = page;
  919. frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
  920. }
  921. BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
  922. return gop;
  923. err:
  924. /* Unwind, freeing all pages and sending error responses. */
  925. while (shinfo->nr_frags-- > start) {
  926. xen_netbk_idx_release(netbk,
  927. frag_get_pending_idx(&frags[shinfo->nr_frags]),
  928. XEN_NETIF_RSP_ERROR);
  929. }
  930. /* The head too, if necessary. */
  931. if (start)
  932. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
  933. return NULL;
  934. }
  935. static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
  936. struct sk_buff *skb,
  937. struct gnttab_copy **gopp)
  938. {
  939. struct gnttab_copy *gop = *gopp;
  940. u16 pending_idx = *((u16 *)skb->data);
  941. struct skb_shared_info *shinfo = skb_shinfo(skb);
  942. struct pending_tx_info *tx_info;
  943. int nr_frags = shinfo->nr_frags;
  944. int i, err, start;
  945. u16 peek; /* peek into next tx request */
  946. /* Check status of header. */
  947. err = gop->status;
  948. if (unlikely(err))
  949. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
  950. /* Skip first skb fragment if it is on same page as header fragment. */
  951. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  952. for (i = start; i < nr_frags; i++) {
  953. int j, newerr;
  954. pending_ring_idx_t head;
  955. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  956. tx_info = &netbk->pending_tx_info[pending_idx];
  957. head = tx_info->head;
  958. /* Check error status: if okay then remember grant handle. */
  959. do {
  960. newerr = (++gop)->status;
  961. if (newerr)
  962. break;
  963. peek = netbk->pending_ring[pending_index(++head)];
  964. } while (!pending_tx_is_head(netbk, peek));
  965. if (likely(!newerr)) {
  966. /* Had a previous error? Invalidate this fragment. */
  967. if (unlikely(err))
  968. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
  969. continue;
  970. }
  971. /* Error on this fragment: respond to client with an error. */
  972. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
  973. /* Not the first error? Preceding frags already invalidated. */
  974. if (err)
  975. continue;
  976. /* First error: invalidate header and preceding fragments. */
  977. pending_idx = *((u16 *)skb->data);
  978. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
  979. for (j = start; j < i; j++) {
  980. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  981. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
  982. }
  983. /* Remember the error: invalidate all subsequent fragments. */
  984. err = newerr;
  985. }
  986. *gopp = gop + 1;
  987. return err;
  988. }
  989. static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
  990. {
  991. struct skb_shared_info *shinfo = skb_shinfo(skb);
  992. int nr_frags = shinfo->nr_frags;
  993. int i;
  994. for (i = 0; i < nr_frags; i++) {
  995. skb_frag_t *frag = shinfo->frags + i;
  996. struct xen_netif_tx_request *txp;
  997. struct page *page;
  998. u16 pending_idx;
  999. pending_idx = frag_get_pending_idx(frag);
  1000. txp = &netbk->pending_tx_info[pending_idx].req;
  1001. page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
  1002. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  1003. skb->len += txp->size;
  1004. skb->data_len += txp->size;
  1005. skb->truesize += txp->size;
  1006. /* Take an extra reference to offset xen_netbk_idx_release */
  1007. get_page(netbk->mmap_pages[pending_idx]);
  1008. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
  1009. }
  1010. }
  1011. static int xen_netbk_get_extras(struct xenvif *vif,
  1012. struct xen_netif_extra_info *extras,
  1013. int work_to_do)
  1014. {
  1015. struct xen_netif_extra_info extra;
  1016. RING_IDX cons = vif->tx.req_cons;
  1017. do {
  1018. if (unlikely(work_to_do-- <= 0)) {
  1019. netdev_err(vif->dev, "Missing extra info\n");
  1020. netbk_fatal_tx_err(vif);
  1021. return -EBADR;
  1022. }
  1023. memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
  1024. sizeof(extra));
  1025. if (unlikely(!extra.type ||
  1026. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  1027. vif->tx.req_cons = ++cons;
  1028. netdev_err(vif->dev,
  1029. "Invalid extra type: %d\n", extra.type);
  1030. netbk_fatal_tx_err(vif);
  1031. return -EINVAL;
  1032. }
  1033. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  1034. vif->tx.req_cons = ++cons;
  1035. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  1036. return work_to_do;
  1037. }
  1038. static int netbk_set_skb_gso(struct xenvif *vif,
  1039. struct sk_buff *skb,
  1040. struct xen_netif_extra_info *gso)
  1041. {
  1042. if (!gso->u.gso.size) {
  1043. netdev_err(vif->dev, "GSO size must not be zero.\n");
  1044. netbk_fatal_tx_err(vif);
  1045. return -EINVAL;
  1046. }
  1047. /* Currently only TCPv4 S.O. is supported. */
  1048. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
  1049. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  1050. netbk_fatal_tx_err(vif);
  1051. return -EINVAL;
  1052. }
  1053. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  1054. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  1055. /* Header must be checked, and gso_segs computed. */
  1056. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1057. skb_shinfo(skb)->gso_segs = 0;
  1058. return 0;
  1059. }
  1060. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  1061. {
  1062. struct iphdr *iph;
  1063. int err = -EPROTO;
  1064. int recalculate_partial_csum = 0;
  1065. /*
  1066. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  1067. * peers can fail to set NETRXF_csum_blank when sending a GSO
  1068. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  1069. * recalculate the partial checksum.
  1070. */
  1071. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  1072. vif->rx_gso_checksum_fixup++;
  1073. skb->ip_summed = CHECKSUM_PARTIAL;
  1074. recalculate_partial_csum = 1;
  1075. }
  1076. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  1077. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1078. return 0;
  1079. if (skb->protocol != htons(ETH_P_IP))
  1080. goto out;
  1081. iph = (void *)skb->data;
  1082. switch (iph->protocol) {
  1083. case IPPROTO_TCP:
  1084. if (!skb_partial_csum_set(skb, 4 * iph->ihl,
  1085. offsetof(struct tcphdr, check)))
  1086. goto out;
  1087. if (recalculate_partial_csum) {
  1088. struct tcphdr *tcph = tcp_hdr(skb);
  1089. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1090. skb->len - iph->ihl*4,
  1091. IPPROTO_TCP, 0);
  1092. }
  1093. break;
  1094. case IPPROTO_UDP:
  1095. if (!skb_partial_csum_set(skb, 4 * iph->ihl,
  1096. offsetof(struct udphdr, check)))
  1097. goto out;
  1098. if (recalculate_partial_csum) {
  1099. struct udphdr *udph = udp_hdr(skb);
  1100. udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1101. skb->len - iph->ihl*4,
  1102. IPPROTO_UDP, 0);
  1103. }
  1104. break;
  1105. default:
  1106. if (net_ratelimit())
  1107. netdev_err(vif->dev,
  1108. "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
  1109. iph->protocol);
  1110. goto out;
  1111. }
  1112. err = 0;
  1113. out:
  1114. return err;
  1115. }
  1116. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  1117. {
  1118. unsigned long now = jiffies;
  1119. unsigned long next_credit =
  1120. vif->credit_timeout.expires +
  1121. msecs_to_jiffies(vif->credit_usec / 1000);
  1122. /* Timer could already be pending in rare cases. */
  1123. if (timer_pending(&vif->credit_timeout))
  1124. return true;
  1125. /* Passed the point where we can replenish credit? */
  1126. if (time_after_eq(now, next_credit)) {
  1127. vif->credit_timeout.expires = now;
  1128. tx_add_credit(vif);
  1129. }
  1130. /* Still too big to send right now? Set a callback. */
  1131. if (size > vif->remaining_credit) {
  1132. vif->credit_timeout.data =
  1133. (unsigned long)vif;
  1134. vif->credit_timeout.function =
  1135. tx_credit_callback;
  1136. mod_timer(&vif->credit_timeout,
  1137. next_credit);
  1138. return true;
  1139. }
  1140. return false;
  1141. }
  1142. static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
  1143. {
  1144. struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
  1145. struct sk_buff *skb;
  1146. int ret;
  1147. while ((nr_pending_reqs(netbk) + XEN_NETIF_NR_SLOTS_MIN
  1148. < MAX_PENDING_REQS) &&
  1149. !list_empty(&netbk->net_schedule_list)) {
  1150. struct xenvif *vif;
  1151. struct xen_netif_tx_request txreq;
  1152. struct xen_netif_tx_request txfrags[max_skb_slots];
  1153. struct page *page;
  1154. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  1155. u16 pending_idx;
  1156. RING_IDX idx;
  1157. int work_to_do;
  1158. unsigned int data_len;
  1159. pending_ring_idx_t index;
  1160. /* Get a netif from the list with work to do. */
  1161. vif = poll_net_schedule_list(netbk);
  1162. /* This can sometimes happen because the test of
  1163. * list_empty(net_schedule_list) at the top of the
  1164. * loop is unlocked. Just go back and have another
  1165. * look.
  1166. */
  1167. if (!vif)
  1168. continue;
  1169. if (vif->tx.sring->req_prod - vif->tx.req_cons >
  1170. XEN_NETIF_TX_RING_SIZE) {
  1171. netdev_err(vif->dev,
  1172. "Impossible number of requests. "
  1173. "req_prod %d, req_cons %d, size %ld\n",
  1174. vif->tx.sring->req_prod, vif->tx.req_cons,
  1175. XEN_NETIF_TX_RING_SIZE);
  1176. netbk_fatal_tx_err(vif);
  1177. continue;
  1178. }
  1179. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
  1180. if (!work_to_do) {
  1181. xenvif_put(vif);
  1182. continue;
  1183. }
  1184. idx = vif->tx.req_cons;
  1185. rmb(); /* Ensure that we see the request before we copy it. */
  1186. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  1187. /* Credit-based scheduling. */
  1188. if (txreq.size > vif->remaining_credit &&
  1189. tx_credit_exceeded(vif, txreq.size)) {
  1190. xenvif_put(vif);
  1191. continue;
  1192. }
  1193. vif->remaining_credit -= txreq.size;
  1194. work_to_do--;
  1195. vif->tx.req_cons = ++idx;
  1196. memset(extras, 0, sizeof(extras));
  1197. if (txreq.flags & XEN_NETTXF_extra_info) {
  1198. work_to_do = xen_netbk_get_extras(vif, extras,
  1199. work_to_do);
  1200. idx = vif->tx.req_cons;
  1201. if (unlikely(work_to_do < 0))
  1202. continue;
  1203. }
  1204. ret = netbk_count_requests(vif, &txreq, idx,
  1205. txfrags, work_to_do);
  1206. if (unlikely(ret < 0))
  1207. continue;
  1208. idx += ret;
  1209. if (unlikely(txreq.size < ETH_HLEN)) {
  1210. netdev_dbg(vif->dev,
  1211. "Bad packet size: %d\n", txreq.size);
  1212. netbk_tx_err(vif, &txreq, idx);
  1213. continue;
  1214. }
  1215. /* No crossing a page as the payload mustn't fragment. */
  1216. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1217. netdev_err(vif->dev,
  1218. "txreq.offset: %x, size: %u, end: %lu\n",
  1219. txreq.offset, txreq.size,
  1220. (txreq.offset&~PAGE_MASK) + txreq.size);
  1221. netbk_fatal_tx_err(vif);
  1222. continue;
  1223. }
  1224. index = pending_index(netbk->pending_cons);
  1225. pending_idx = netbk->pending_ring[index];
  1226. data_len = (txreq.size > PKT_PROT_LEN &&
  1227. ret < XEN_NETIF_NR_SLOTS_MIN) ?
  1228. PKT_PROT_LEN : txreq.size;
  1229. skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
  1230. GFP_ATOMIC | __GFP_NOWARN);
  1231. if (unlikely(skb == NULL)) {
  1232. netdev_dbg(vif->dev,
  1233. "Can't allocate a skb in start_xmit.\n");
  1234. netbk_tx_err(vif, &txreq, idx);
  1235. break;
  1236. }
  1237. /* Packets passed to netif_rx() must have some headroom. */
  1238. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  1239. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1240. struct xen_netif_extra_info *gso;
  1241. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1242. if (netbk_set_skb_gso(vif, skb, gso)) {
  1243. /* Failure in netbk_set_skb_gso is fatal. */
  1244. kfree_skb(skb);
  1245. continue;
  1246. }
  1247. }
  1248. /* XXX could copy straight to head */
  1249. page = xen_netbk_alloc_page(netbk, pending_idx);
  1250. if (!page) {
  1251. kfree_skb(skb);
  1252. netbk_tx_err(vif, &txreq, idx);
  1253. continue;
  1254. }
  1255. gop->source.u.ref = txreq.gref;
  1256. gop->source.domid = vif->domid;
  1257. gop->source.offset = txreq.offset;
  1258. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  1259. gop->dest.domid = DOMID_SELF;
  1260. gop->dest.offset = txreq.offset;
  1261. gop->len = txreq.size;
  1262. gop->flags = GNTCOPY_source_gref;
  1263. gop++;
  1264. memcpy(&netbk->pending_tx_info[pending_idx].req,
  1265. &txreq, sizeof(txreq));
  1266. netbk->pending_tx_info[pending_idx].vif = vif;
  1267. netbk->pending_tx_info[pending_idx].head = index;
  1268. *((u16 *)skb->data) = pending_idx;
  1269. __skb_put(skb, data_len);
  1270. skb_shinfo(skb)->nr_frags = ret;
  1271. if (data_len < txreq.size) {
  1272. skb_shinfo(skb)->nr_frags++;
  1273. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1274. pending_idx);
  1275. } else {
  1276. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1277. INVALID_PENDING_IDX);
  1278. }
  1279. netbk->pending_cons++;
  1280. request_gop = xen_netbk_get_requests(netbk, vif,
  1281. skb, txfrags, gop);
  1282. if (request_gop == NULL) {
  1283. kfree_skb(skb);
  1284. netbk_tx_err(vif, &txreq, idx);
  1285. continue;
  1286. }
  1287. gop = request_gop;
  1288. __skb_queue_tail(&netbk->tx_queue, skb);
  1289. vif->tx.req_cons = idx;
  1290. xen_netbk_check_rx_xenvif(vif);
  1291. if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
  1292. break;
  1293. }
  1294. return gop - netbk->tx_copy_ops;
  1295. }
  1296. static void xen_netbk_tx_submit(struct xen_netbk *netbk)
  1297. {
  1298. struct gnttab_copy *gop = netbk->tx_copy_ops;
  1299. struct sk_buff *skb;
  1300. while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
  1301. struct xen_netif_tx_request *txp;
  1302. struct xenvif *vif;
  1303. u16 pending_idx;
  1304. unsigned data_len;
  1305. pending_idx = *((u16 *)skb->data);
  1306. vif = netbk->pending_tx_info[pending_idx].vif;
  1307. txp = &netbk->pending_tx_info[pending_idx].req;
  1308. /* Check the remap error code. */
  1309. if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
  1310. netdev_dbg(vif->dev, "netback grant failed.\n");
  1311. skb_shinfo(skb)->nr_frags = 0;
  1312. kfree_skb(skb);
  1313. continue;
  1314. }
  1315. data_len = skb->len;
  1316. memcpy(skb->data,
  1317. (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
  1318. data_len);
  1319. if (data_len < txp->size) {
  1320. /* Append the packet payload as a fragment. */
  1321. txp->offset += data_len;
  1322. txp->size -= data_len;
  1323. } else {
  1324. /* Schedule a response immediately. */
  1325. xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
  1326. }
  1327. if (txp->flags & XEN_NETTXF_csum_blank)
  1328. skb->ip_summed = CHECKSUM_PARTIAL;
  1329. else if (txp->flags & XEN_NETTXF_data_validated)
  1330. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1331. xen_netbk_fill_frags(netbk, skb);
  1332. /*
  1333. * If the initial fragment was < PKT_PROT_LEN then
  1334. * pull through some bytes from the other fragments to
  1335. * increase the linear region to PKT_PROT_LEN bytes.
  1336. */
  1337. if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
  1338. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1339. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1340. }
  1341. skb->dev = vif->dev;
  1342. skb->protocol = eth_type_trans(skb, skb->dev);
  1343. skb_reset_network_header(skb);
  1344. if (checksum_setup(vif, skb)) {
  1345. netdev_dbg(vif->dev,
  1346. "Can't setup checksum in net_tx_action\n");
  1347. kfree_skb(skb);
  1348. continue;
  1349. }
  1350. skb_probe_transport_header(skb, 0);
  1351. vif->dev->stats.rx_bytes += skb->len;
  1352. vif->dev->stats.rx_packets++;
  1353. xenvif_receive_skb(vif, skb);
  1354. }
  1355. }
  1356. /* Called after netfront has transmitted */
  1357. static void xen_netbk_tx_action(struct xen_netbk *netbk)
  1358. {
  1359. unsigned nr_gops;
  1360. nr_gops = xen_netbk_tx_build_gops(netbk);
  1361. if (nr_gops == 0)
  1362. return;
  1363. gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
  1364. xen_netbk_tx_submit(netbk);
  1365. }
  1366. static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
  1367. u8 status)
  1368. {
  1369. struct xenvif *vif;
  1370. struct pending_tx_info *pending_tx_info;
  1371. pending_ring_idx_t head;
  1372. u16 peek; /* peek into next tx request */
  1373. BUG_ON(netbk->mmap_pages[pending_idx] == (void *)(~0UL));
  1374. /* Already complete? */
  1375. if (netbk->mmap_pages[pending_idx] == NULL)
  1376. return;
  1377. pending_tx_info = &netbk->pending_tx_info[pending_idx];
  1378. vif = pending_tx_info->vif;
  1379. head = pending_tx_info->head;
  1380. BUG_ON(!pending_tx_is_head(netbk, head));
  1381. BUG_ON(netbk->pending_ring[pending_index(head)] != pending_idx);
  1382. do {
  1383. pending_ring_idx_t index;
  1384. pending_ring_idx_t idx = pending_index(head);
  1385. u16 info_idx = netbk->pending_ring[idx];
  1386. pending_tx_info = &netbk->pending_tx_info[info_idx];
  1387. make_tx_response(vif, &pending_tx_info->req, status);
  1388. /* Setting any number other than
  1389. * INVALID_PENDING_RING_IDX indicates this slot is
  1390. * starting a new packet / ending a previous packet.
  1391. */
  1392. pending_tx_info->head = 0;
  1393. index = pending_index(netbk->pending_prod++);
  1394. netbk->pending_ring[index] = netbk->pending_ring[info_idx];
  1395. xenvif_put(vif);
  1396. peek = netbk->pending_ring[pending_index(++head)];
  1397. } while (!pending_tx_is_head(netbk, peek));
  1398. netbk->mmap_pages[pending_idx]->mapping = 0;
  1399. put_page(netbk->mmap_pages[pending_idx]);
  1400. netbk->mmap_pages[pending_idx] = NULL;
  1401. }
  1402. static void make_tx_response(struct xenvif *vif,
  1403. struct xen_netif_tx_request *txp,
  1404. s8 st)
  1405. {
  1406. RING_IDX i = vif->tx.rsp_prod_pvt;
  1407. struct xen_netif_tx_response *resp;
  1408. int notify;
  1409. resp = RING_GET_RESPONSE(&vif->tx, i);
  1410. resp->id = txp->id;
  1411. resp->status = st;
  1412. if (txp->flags & XEN_NETTXF_extra_info)
  1413. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1414. vif->tx.rsp_prod_pvt = ++i;
  1415. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1416. if (notify)
  1417. notify_remote_via_irq(vif->irq);
  1418. }
  1419. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1420. u16 id,
  1421. s8 st,
  1422. u16 offset,
  1423. u16 size,
  1424. u16 flags)
  1425. {
  1426. RING_IDX i = vif->rx.rsp_prod_pvt;
  1427. struct xen_netif_rx_response *resp;
  1428. resp = RING_GET_RESPONSE(&vif->rx, i);
  1429. resp->offset = offset;
  1430. resp->flags = flags;
  1431. resp->id = id;
  1432. resp->status = (s16)size;
  1433. if (st < 0)
  1434. resp->status = (s16)st;
  1435. vif->rx.rsp_prod_pvt = ++i;
  1436. return resp;
  1437. }
  1438. static inline int rx_work_todo(struct xen_netbk *netbk)
  1439. {
  1440. return !skb_queue_empty(&netbk->rx_queue);
  1441. }
  1442. static inline int tx_work_todo(struct xen_netbk *netbk)
  1443. {
  1444. if ((nr_pending_reqs(netbk) + XEN_NETIF_NR_SLOTS_MIN
  1445. < MAX_PENDING_REQS) &&
  1446. !list_empty(&netbk->net_schedule_list))
  1447. return 1;
  1448. return 0;
  1449. }
  1450. static int xen_netbk_kthread(void *data)
  1451. {
  1452. struct xen_netbk *netbk = data;
  1453. while (!kthread_should_stop()) {
  1454. wait_event_interruptible(netbk->wq,
  1455. rx_work_todo(netbk) ||
  1456. tx_work_todo(netbk) ||
  1457. kthread_should_stop());
  1458. cond_resched();
  1459. if (kthread_should_stop())
  1460. break;
  1461. if (rx_work_todo(netbk))
  1462. xen_netbk_rx_action(netbk);
  1463. if (tx_work_todo(netbk))
  1464. xen_netbk_tx_action(netbk);
  1465. }
  1466. return 0;
  1467. }
  1468. void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
  1469. {
  1470. if (vif->tx.sring)
  1471. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1472. vif->tx.sring);
  1473. if (vif->rx.sring)
  1474. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1475. vif->rx.sring);
  1476. }
  1477. int xen_netbk_map_frontend_rings(struct xenvif *vif,
  1478. grant_ref_t tx_ring_ref,
  1479. grant_ref_t rx_ring_ref)
  1480. {
  1481. void *addr;
  1482. struct xen_netif_tx_sring *txs;
  1483. struct xen_netif_rx_sring *rxs;
  1484. int err = -ENOMEM;
  1485. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1486. tx_ring_ref, &addr);
  1487. if (err)
  1488. goto err;
  1489. txs = (struct xen_netif_tx_sring *)addr;
  1490. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1491. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1492. rx_ring_ref, &addr);
  1493. if (err)
  1494. goto err;
  1495. rxs = (struct xen_netif_rx_sring *)addr;
  1496. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1497. vif->rx_req_cons_peek = 0;
  1498. return 0;
  1499. err:
  1500. xen_netbk_unmap_frontend_rings(vif);
  1501. return err;
  1502. }
  1503. static int __init netback_init(void)
  1504. {
  1505. int i;
  1506. int rc = 0;
  1507. int group;
  1508. if (!xen_domain())
  1509. return -ENODEV;
  1510. if (max_skb_slots < XEN_NETIF_NR_SLOTS_MIN) {
  1511. printk(KERN_INFO
  1512. "xen-netback: max_skb_slots too small (%d), bump it to XEN_NETIF_NR_SLOTS_MIN (%d)\n",
  1513. max_skb_slots, XEN_NETIF_NR_SLOTS_MIN);
  1514. max_skb_slots = XEN_NETIF_NR_SLOTS_MIN;
  1515. }
  1516. xen_netbk_group_nr = num_online_cpus();
  1517. xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
  1518. if (!xen_netbk)
  1519. return -ENOMEM;
  1520. for (group = 0; group < xen_netbk_group_nr; group++) {
  1521. struct xen_netbk *netbk = &xen_netbk[group];
  1522. skb_queue_head_init(&netbk->rx_queue);
  1523. skb_queue_head_init(&netbk->tx_queue);
  1524. init_timer(&netbk->net_timer);
  1525. netbk->net_timer.data = (unsigned long)netbk;
  1526. netbk->net_timer.function = xen_netbk_alarm;
  1527. netbk->pending_cons = 0;
  1528. netbk->pending_prod = MAX_PENDING_REQS;
  1529. for (i = 0; i < MAX_PENDING_REQS; i++)
  1530. netbk->pending_ring[i] = i;
  1531. init_waitqueue_head(&netbk->wq);
  1532. netbk->task = kthread_create(xen_netbk_kthread,
  1533. (void *)netbk,
  1534. "netback/%u", group);
  1535. if (IS_ERR(netbk->task)) {
  1536. printk(KERN_ALERT "kthread_create() fails at netback\n");
  1537. del_timer(&netbk->net_timer);
  1538. rc = PTR_ERR(netbk->task);
  1539. goto failed_init;
  1540. }
  1541. kthread_bind(netbk->task, group);
  1542. INIT_LIST_HEAD(&netbk->net_schedule_list);
  1543. spin_lock_init(&netbk->net_schedule_list_lock);
  1544. atomic_set(&netbk->netfront_count, 0);
  1545. wake_up_process(netbk->task);
  1546. }
  1547. rc = xenvif_xenbus_init();
  1548. if (rc)
  1549. goto failed_init;
  1550. return 0;
  1551. failed_init:
  1552. while (--group >= 0) {
  1553. struct xen_netbk *netbk = &xen_netbk[group];
  1554. for (i = 0; i < MAX_PENDING_REQS; i++) {
  1555. if (netbk->mmap_pages[i])
  1556. __free_page(netbk->mmap_pages[i]);
  1557. }
  1558. del_timer(&netbk->net_timer);
  1559. kthread_stop(netbk->task);
  1560. }
  1561. vfree(xen_netbk);
  1562. return rc;
  1563. }
  1564. module_init(netback_init);
  1565. MODULE_LICENSE("Dual BSD/GPL");
  1566. MODULE_ALIAS("xen-backend:vif");