netback.c 49 KB

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