netback.c 50 KB

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