netback.c 45 KB

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