netback.c 49 KB

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