netback.c 49 KB

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