netback.c 50 KB

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