netback.c 46 KB

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