xen-netfront.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /*
  2. * Virtual network driver for conversing with remote driver backends.
  3. *
  4. * Copyright (c) 2002-2005, K A Fraser
  5. * Copyright (c) 2005, XenSource Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation; or, when distributed
  10. * separately from the Linux kernel or incorporated into other
  11. * software packages, subject to the following license:
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this source file (the "Software"), to deal in the Software without
  15. * restriction, including without limitation the rights to use, copy, modify,
  16. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  17. * and to permit persons to whom the Software is furnished to do so, subject to
  18. * the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  29. * IN THE SOFTWARE.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/if_ether.h>
  38. #include <net/tcp.h>
  39. #include <linux/udp.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/mm.h>
  42. #include <linux/slab.h>
  43. #include <net/ip.h>
  44. #include <asm/xen/page.h>
  45. #include <xen/xen.h>
  46. #include <xen/xenbus.h>
  47. #include <xen/events.h>
  48. #include <xen/page.h>
  49. #include <xen/platform_pci.h>
  50. #include <xen/grant_table.h>
  51. #include <xen/interface/io/netif.h>
  52. #include <xen/interface/memory.h>
  53. #include <xen/interface/grant_table.h>
  54. static const struct ethtool_ops xennet_ethtool_ops;
  55. struct netfront_cb {
  56. int pull_to;
  57. };
  58. #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
  59. #define RX_COPY_THRESHOLD 256
  60. #define GRANT_INVALID_REF 0
  61. #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
  62. #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
  63. #define TX_MAX_TARGET min_t(int, NET_TX_RING_SIZE, 256)
  64. struct netfront_stats {
  65. u64 rx_packets;
  66. u64 tx_packets;
  67. u64 rx_bytes;
  68. u64 tx_bytes;
  69. struct u64_stats_sync syncp;
  70. };
  71. struct netfront_info {
  72. struct list_head list;
  73. struct net_device *netdev;
  74. struct napi_struct napi;
  75. /* Split event channels support, tx_* == rx_* when using
  76. * single event channel.
  77. */
  78. unsigned int tx_evtchn, rx_evtchn;
  79. unsigned int tx_irq, rx_irq;
  80. /* Only used when split event channels support is enabled */
  81. char tx_irq_name[IFNAMSIZ+4]; /* DEVNAME-tx */
  82. char rx_irq_name[IFNAMSIZ+4]; /* DEVNAME-rx */
  83. struct xenbus_device *xbdev;
  84. spinlock_t tx_lock;
  85. struct xen_netif_tx_front_ring tx;
  86. int tx_ring_ref;
  87. /*
  88. * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
  89. * are linked from tx_skb_freelist through skb_entry.link.
  90. *
  91. * NB. Freelist index entries are always going to be less than
  92. * PAGE_OFFSET, whereas pointers to skbs will always be equal or
  93. * greater than PAGE_OFFSET: we use this property to distinguish
  94. * them.
  95. */
  96. union skb_entry {
  97. struct sk_buff *skb;
  98. unsigned long link;
  99. } tx_skbs[NET_TX_RING_SIZE];
  100. grant_ref_t gref_tx_head;
  101. grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
  102. unsigned tx_skb_freelist;
  103. spinlock_t rx_lock ____cacheline_aligned_in_smp;
  104. struct xen_netif_rx_front_ring rx;
  105. int rx_ring_ref;
  106. /* Receive-ring batched refills. */
  107. #define RX_MIN_TARGET 8
  108. #define RX_DFL_MIN_TARGET 64
  109. #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
  110. unsigned rx_min_target, rx_max_target, rx_target;
  111. struct sk_buff_head rx_batch;
  112. struct timer_list rx_refill_timer;
  113. struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
  114. grant_ref_t gref_rx_head;
  115. grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
  116. unsigned long rx_pfn_array[NET_RX_RING_SIZE];
  117. struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
  118. struct mmu_update rx_mmu[NET_RX_RING_SIZE];
  119. /* Statistics */
  120. struct netfront_stats __percpu *stats;
  121. unsigned long rx_gso_checksum_fixup;
  122. };
  123. struct netfront_rx_info {
  124. struct xen_netif_rx_response rx;
  125. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
  126. };
  127. static void skb_entry_set_link(union skb_entry *list, unsigned short id)
  128. {
  129. list->link = id;
  130. }
  131. static int skb_entry_is_link(const union skb_entry *list)
  132. {
  133. BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
  134. return (unsigned long)list->skb < PAGE_OFFSET;
  135. }
  136. /*
  137. * Access macros for acquiring freeing slots in tx_skbs[].
  138. */
  139. static void add_id_to_freelist(unsigned *head, union skb_entry *list,
  140. unsigned short id)
  141. {
  142. skb_entry_set_link(&list[id], *head);
  143. *head = id;
  144. }
  145. static unsigned short get_id_from_freelist(unsigned *head,
  146. union skb_entry *list)
  147. {
  148. unsigned int id = *head;
  149. *head = list[id].link;
  150. return id;
  151. }
  152. static int xennet_rxidx(RING_IDX idx)
  153. {
  154. return idx & (NET_RX_RING_SIZE - 1);
  155. }
  156. static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
  157. RING_IDX ri)
  158. {
  159. int i = xennet_rxidx(ri);
  160. struct sk_buff *skb = np->rx_skbs[i];
  161. np->rx_skbs[i] = NULL;
  162. return skb;
  163. }
  164. static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
  165. RING_IDX ri)
  166. {
  167. int i = xennet_rxidx(ri);
  168. grant_ref_t ref = np->grant_rx_ref[i];
  169. np->grant_rx_ref[i] = GRANT_INVALID_REF;
  170. return ref;
  171. }
  172. #ifdef CONFIG_SYSFS
  173. static int xennet_sysfs_addif(struct net_device *netdev);
  174. static void xennet_sysfs_delif(struct net_device *netdev);
  175. #else /* !CONFIG_SYSFS */
  176. #define xennet_sysfs_addif(dev) (0)
  177. #define xennet_sysfs_delif(dev) do { } while (0)
  178. #endif
  179. static bool xennet_can_sg(struct net_device *dev)
  180. {
  181. return dev->features & NETIF_F_SG;
  182. }
  183. static void rx_refill_timeout(unsigned long data)
  184. {
  185. struct net_device *dev = (struct net_device *)data;
  186. struct netfront_info *np = netdev_priv(dev);
  187. napi_schedule(&np->napi);
  188. }
  189. static int netfront_tx_slot_available(struct netfront_info *np)
  190. {
  191. return (np->tx.req_prod_pvt - np->tx.rsp_cons) <
  192. (TX_MAX_TARGET - MAX_SKB_FRAGS - 2);
  193. }
  194. static void xennet_maybe_wake_tx(struct net_device *dev)
  195. {
  196. struct netfront_info *np = netdev_priv(dev);
  197. if (unlikely(netif_queue_stopped(dev)) &&
  198. netfront_tx_slot_available(np) &&
  199. likely(netif_running(dev)))
  200. netif_wake_queue(dev);
  201. }
  202. static void xennet_alloc_rx_buffers(struct net_device *dev)
  203. {
  204. unsigned short id;
  205. struct netfront_info *np = netdev_priv(dev);
  206. struct sk_buff *skb;
  207. struct page *page;
  208. int i, batch_target, notify;
  209. RING_IDX req_prod = np->rx.req_prod_pvt;
  210. grant_ref_t ref;
  211. unsigned long pfn;
  212. void *vaddr;
  213. struct xen_netif_rx_request *req;
  214. if (unlikely(!netif_carrier_ok(dev)))
  215. return;
  216. /*
  217. * Allocate skbuffs greedily, even though we batch updates to the
  218. * receive ring. This creates a less bursty demand on the memory
  219. * allocator, so should reduce the chance of failed allocation requests
  220. * both for ourself and for other kernel subsystems.
  221. */
  222. batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
  223. for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
  224. skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD + NET_IP_ALIGN,
  225. GFP_ATOMIC | __GFP_NOWARN);
  226. if (unlikely(!skb))
  227. goto no_skb;
  228. /* Align ip header to a 16 bytes boundary */
  229. skb_reserve(skb, NET_IP_ALIGN);
  230. page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
  231. if (!page) {
  232. kfree_skb(skb);
  233. no_skb:
  234. /* Any skbuffs queued for refill? Force them out. */
  235. if (i != 0)
  236. goto refill;
  237. /* Could not allocate any skbuffs. Try again later. */
  238. mod_timer(&np->rx_refill_timer,
  239. jiffies + (HZ/10));
  240. break;
  241. }
  242. __skb_fill_page_desc(skb, 0, page, 0, 0);
  243. skb_shinfo(skb)->nr_frags = 1;
  244. __skb_queue_tail(&np->rx_batch, skb);
  245. }
  246. /* Is the batch large enough to be worthwhile? */
  247. if (i < (np->rx_target/2)) {
  248. if (req_prod > np->rx.sring->req_prod)
  249. goto push;
  250. return;
  251. }
  252. /* Adjust our fill target if we risked running out of buffers. */
  253. if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
  254. ((np->rx_target *= 2) > np->rx_max_target))
  255. np->rx_target = np->rx_max_target;
  256. refill:
  257. for (i = 0; ; i++) {
  258. skb = __skb_dequeue(&np->rx_batch);
  259. if (skb == NULL)
  260. break;
  261. skb->dev = dev;
  262. id = xennet_rxidx(req_prod + i);
  263. BUG_ON(np->rx_skbs[id]);
  264. np->rx_skbs[id] = skb;
  265. ref = gnttab_claim_grant_reference(&np->gref_rx_head);
  266. BUG_ON((signed short)ref < 0);
  267. np->grant_rx_ref[id] = ref;
  268. pfn = page_to_pfn(skb_frag_page(&skb_shinfo(skb)->frags[0]));
  269. vaddr = page_address(skb_frag_page(&skb_shinfo(skb)->frags[0]));
  270. req = RING_GET_REQUEST(&np->rx, req_prod + i);
  271. gnttab_grant_foreign_access_ref(ref,
  272. np->xbdev->otherend_id,
  273. pfn_to_mfn(pfn),
  274. 0);
  275. req->id = id;
  276. req->gref = ref;
  277. }
  278. wmb(); /* barrier so backend seens requests */
  279. /* Above is a suitable barrier to ensure backend will see requests. */
  280. np->rx.req_prod_pvt = req_prod + i;
  281. push:
  282. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
  283. if (notify)
  284. notify_remote_via_irq(np->rx_irq);
  285. }
  286. static int xennet_open(struct net_device *dev)
  287. {
  288. struct netfront_info *np = netdev_priv(dev);
  289. napi_enable(&np->napi);
  290. spin_lock_bh(&np->rx_lock);
  291. if (netif_carrier_ok(dev)) {
  292. xennet_alloc_rx_buffers(dev);
  293. np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
  294. if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
  295. napi_schedule(&np->napi);
  296. }
  297. spin_unlock_bh(&np->rx_lock);
  298. netif_start_queue(dev);
  299. return 0;
  300. }
  301. static void xennet_tx_buf_gc(struct net_device *dev)
  302. {
  303. RING_IDX cons, prod;
  304. unsigned short id;
  305. struct netfront_info *np = netdev_priv(dev);
  306. struct sk_buff *skb;
  307. BUG_ON(!netif_carrier_ok(dev));
  308. do {
  309. prod = np->tx.sring->rsp_prod;
  310. rmb(); /* Ensure we see responses up to 'rp'. */
  311. for (cons = np->tx.rsp_cons; cons != prod; cons++) {
  312. struct xen_netif_tx_response *txrsp;
  313. txrsp = RING_GET_RESPONSE(&np->tx, cons);
  314. if (txrsp->status == XEN_NETIF_RSP_NULL)
  315. continue;
  316. id = txrsp->id;
  317. skb = np->tx_skbs[id].skb;
  318. if (unlikely(gnttab_query_foreign_access(
  319. np->grant_tx_ref[id]) != 0)) {
  320. printk(KERN_ALERT "xennet_tx_buf_gc: warning "
  321. "-- grant still in use by backend "
  322. "domain.\n");
  323. BUG();
  324. }
  325. gnttab_end_foreign_access_ref(
  326. np->grant_tx_ref[id], GNTMAP_readonly);
  327. gnttab_release_grant_reference(
  328. &np->gref_tx_head, np->grant_tx_ref[id]);
  329. np->grant_tx_ref[id] = GRANT_INVALID_REF;
  330. add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
  331. dev_kfree_skb_irq(skb);
  332. }
  333. np->tx.rsp_cons = prod;
  334. /*
  335. * Set a new event, then check for race with update of tx_cons.
  336. * Note that it is essential to schedule a callback, no matter
  337. * how few buffers are pending. Even if there is space in the
  338. * transmit ring, higher layers may be blocked because too much
  339. * data is outstanding: in such cases notification from Xen is
  340. * likely to be the only kick that we'll get.
  341. */
  342. np->tx.sring->rsp_event =
  343. prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
  344. mb(); /* update shared area */
  345. } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
  346. xennet_maybe_wake_tx(dev);
  347. }
  348. static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
  349. struct xen_netif_tx_request *tx)
  350. {
  351. struct netfront_info *np = netdev_priv(dev);
  352. char *data = skb->data;
  353. unsigned long mfn;
  354. RING_IDX prod = np->tx.req_prod_pvt;
  355. int frags = skb_shinfo(skb)->nr_frags;
  356. unsigned int offset = offset_in_page(data);
  357. unsigned int len = skb_headlen(skb);
  358. unsigned int id;
  359. grant_ref_t ref;
  360. int i;
  361. /* While the header overlaps a page boundary (including being
  362. larger than a page), split it it into page-sized chunks. */
  363. while (len > PAGE_SIZE - offset) {
  364. tx->size = PAGE_SIZE - offset;
  365. tx->flags |= XEN_NETTXF_more_data;
  366. len -= tx->size;
  367. data += tx->size;
  368. offset = 0;
  369. id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
  370. np->tx_skbs[id].skb = skb_get(skb);
  371. tx = RING_GET_REQUEST(&np->tx, prod++);
  372. tx->id = id;
  373. ref = gnttab_claim_grant_reference(&np->gref_tx_head);
  374. BUG_ON((signed short)ref < 0);
  375. mfn = virt_to_mfn(data);
  376. gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
  377. mfn, GNTMAP_readonly);
  378. tx->gref = np->grant_tx_ref[id] = ref;
  379. tx->offset = offset;
  380. tx->size = len;
  381. tx->flags = 0;
  382. }
  383. /* Grant backend access to each skb fragment page. */
  384. for (i = 0; i < frags; i++) {
  385. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  386. struct page *page = skb_frag_page(frag);
  387. len = skb_frag_size(frag);
  388. offset = frag->page_offset;
  389. /* Data must not cross a page boundary. */
  390. BUG_ON(len + offset > PAGE_SIZE<<compound_order(page));
  391. /* Skip unused frames from start of page */
  392. page += offset >> PAGE_SHIFT;
  393. offset &= ~PAGE_MASK;
  394. while (len > 0) {
  395. unsigned long bytes;
  396. BUG_ON(offset >= PAGE_SIZE);
  397. bytes = PAGE_SIZE - offset;
  398. if (bytes > len)
  399. bytes = len;
  400. tx->flags |= XEN_NETTXF_more_data;
  401. id = get_id_from_freelist(&np->tx_skb_freelist,
  402. np->tx_skbs);
  403. np->tx_skbs[id].skb = skb_get(skb);
  404. tx = RING_GET_REQUEST(&np->tx, prod++);
  405. tx->id = id;
  406. ref = gnttab_claim_grant_reference(&np->gref_tx_head);
  407. BUG_ON((signed short)ref < 0);
  408. mfn = pfn_to_mfn(page_to_pfn(page));
  409. gnttab_grant_foreign_access_ref(ref,
  410. np->xbdev->otherend_id,
  411. mfn, GNTMAP_readonly);
  412. tx->gref = np->grant_tx_ref[id] = ref;
  413. tx->offset = offset;
  414. tx->size = bytes;
  415. tx->flags = 0;
  416. offset += bytes;
  417. len -= bytes;
  418. /* Next frame */
  419. if (offset == PAGE_SIZE && len) {
  420. BUG_ON(!PageCompound(page));
  421. page++;
  422. offset = 0;
  423. }
  424. }
  425. }
  426. np->tx.req_prod_pvt = prod;
  427. }
  428. /*
  429. * Count how many ring slots are required to send the frags of this
  430. * skb. Each frag might be a compound page.
  431. */
  432. static int xennet_count_skb_frag_slots(struct sk_buff *skb)
  433. {
  434. int i, frags = skb_shinfo(skb)->nr_frags;
  435. int pages = 0;
  436. for (i = 0; i < frags; i++) {
  437. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  438. unsigned long size = skb_frag_size(frag);
  439. unsigned long offset = frag->page_offset;
  440. /* Skip unused frames from start of page */
  441. offset &= ~PAGE_MASK;
  442. pages += PFN_UP(offset + size);
  443. }
  444. return pages;
  445. }
  446. static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  447. {
  448. unsigned short id;
  449. struct netfront_info *np = netdev_priv(dev);
  450. struct netfront_stats *stats = this_cpu_ptr(np->stats);
  451. struct xen_netif_tx_request *tx;
  452. char *data = skb->data;
  453. RING_IDX i;
  454. grant_ref_t ref;
  455. unsigned long mfn;
  456. int notify;
  457. int slots;
  458. unsigned int offset = offset_in_page(data);
  459. unsigned int len = skb_headlen(skb);
  460. unsigned long flags;
  461. /* If skb->len is too big for wire format, drop skb and alert
  462. * user about misconfiguration.
  463. */
  464. if (unlikely(skb->len > XEN_NETIF_MAX_TX_SIZE)) {
  465. net_alert_ratelimited(
  466. "xennet: skb->len = %u, too big for wire format\n",
  467. skb->len);
  468. goto drop;
  469. }
  470. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE) +
  471. xennet_count_skb_frag_slots(skb);
  472. if (unlikely(slots > MAX_SKB_FRAGS + 1)) {
  473. net_alert_ratelimited(
  474. "xennet: skb rides the rocket: %d slots\n", slots);
  475. goto drop;
  476. }
  477. spin_lock_irqsave(&np->tx_lock, flags);
  478. if (unlikely(!netif_carrier_ok(dev) ||
  479. (slots > 1 && !xennet_can_sg(dev)) ||
  480. netif_needs_gso(skb, netif_skb_features(skb)))) {
  481. spin_unlock_irqrestore(&np->tx_lock, flags);
  482. goto drop;
  483. }
  484. i = np->tx.req_prod_pvt;
  485. id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
  486. np->tx_skbs[id].skb = skb;
  487. tx = RING_GET_REQUEST(&np->tx, i);
  488. tx->id = id;
  489. ref = gnttab_claim_grant_reference(&np->gref_tx_head);
  490. BUG_ON((signed short)ref < 0);
  491. mfn = virt_to_mfn(data);
  492. gnttab_grant_foreign_access_ref(
  493. ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
  494. tx->gref = np->grant_tx_ref[id] = ref;
  495. tx->offset = offset;
  496. tx->size = len;
  497. tx->flags = 0;
  498. if (skb->ip_summed == CHECKSUM_PARTIAL)
  499. /* local packet? */
  500. tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
  501. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  502. /* remote but checksummed. */
  503. tx->flags |= XEN_NETTXF_data_validated;
  504. if (skb_shinfo(skb)->gso_size) {
  505. struct xen_netif_extra_info *gso;
  506. gso = (struct xen_netif_extra_info *)
  507. RING_GET_REQUEST(&np->tx, ++i);
  508. tx->flags |= XEN_NETTXF_extra_info;
  509. gso->u.gso.size = skb_shinfo(skb)->gso_size;
  510. gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
  511. gso->u.gso.pad = 0;
  512. gso->u.gso.features = 0;
  513. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  514. gso->flags = 0;
  515. }
  516. np->tx.req_prod_pvt = i + 1;
  517. xennet_make_frags(skb, dev, tx);
  518. tx->size = skb->len;
  519. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
  520. if (notify)
  521. notify_remote_via_irq(np->tx_irq);
  522. u64_stats_update_begin(&stats->syncp);
  523. stats->tx_bytes += skb->len;
  524. stats->tx_packets++;
  525. u64_stats_update_end(&stats->syncp);
  526. /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
  527. xennet_tx_buf_gc(dev);
  528. if (!netfront_tx_slot_available(np))
  529. netif_stop_queue(dev);
  530. spin_unlock_irqrestore(&np->tx_lock, flags);
  531. return NETDEV_TX_OK;
  532. drop:
  533. dev->stats.tx_dropped++;
  534. dev_kfree_skb(skb);
  535. return NETDEV_TX_OK;
  536. }
  537. static int xennet_close(struct net_device *dev)
  538. {
  539. struct netfront_info *np = netdev_priv(dev);
  540. netif_stop_queue(np->netdev);
  541. napi_disable(&np->napi);
  542. return 0;
  543. }
  544. static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
  545. grant_ref_t ref)
  546. {
  547. int new = xennet_rxidx(np->rx.req_prod_pvt);
  548. BUG_ON(np->rx_skbs[new]);
  549. np->rx_skbs[new] = skb;
  550. np->grant_rx_ref[new] = ref;
  551. RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
  552. RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
  553. np->rx.req_prod_pvt++;
  554. }
  555. static int xennet_get_extras(struct netfront_info *np,
  556. struct xen_netif_extra_info *extras,
  557. RING_IDX rp)
  558. {
  559. struct xen_netif_extra_info *extra;
  560. struct device *dev = &np->netdev->dev;
  561. RING_IDX cons = np->rx.rsp_cons;
  562. int err = 0;
  563. do {
  564. struct sk_buff *skb;
  565. grant_ref_t ref;
  566. if (unlikely(cons + 1 == rp)) {
  567. if (net_ratelimit())
  568. dev_warn(dev, "Missing extra info\n");
  569. err = -EBADR;
  570. break;
  571. }
  572. extra = (struct xen_netif_extra_info *)
  573. RING_GET_RESPONSE(&np->rx, ++cons);
  574. if (unlikely(!extra->type ||
  575. extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  576. if (net_ratelimit())
  577. dev_warn(dev, "Invalid extra type: %d\n",
  578. extra->type);
  579. err = -EINVAL;
  580. } else {
  581. memcpy(&extras[extra->type - 1], extra,
  582. sizeof(*extra));
  583. }
  584. skb = xennet_get_rx_skb(np, cons);
  585. ref = xennet_get_rx_ref(np, cons);
  586. xennet_move_rx_slot(np, skb, ref);
  587. } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
  588. np->rx.rsp_cons = cons;
  589. return err;
  590. }
  591. static int xennet_get_responses(struct netfront_info *np,
  592. struct netfront_rx_info *rinfo, RING_IDX rp,
  593. struct sk_buff_head *list)
  594. {
  595. struct xen_netif_rx_response *rx = &rinfo->rx;
  596. struct xen_netif_extra_info *extras = rinfo->extras;
  597. struct device *dev = &np->netdev->dev;
  598. RING_IDX cons = np->rx.rsp_cons;
  599. struct sk_buff *skb = xennet_get_rx_skb(np, cons);
  600. grant_ref_t ref = xennet_get_rx_ref(np, cons);
  601. int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
  602. int slots = 1;
  603. int err = 0;
  604. unsigned long ret;
  605. if (rx->flags & XEN_NETRXF_extra_info) {
  606. err = xennet_get_extras(np, extras, rp);
  607. cons = np->rx.rsp_cons;
  608. }
  609. for (;;) {
  610. if (unlikely(rx->status < 0 ||
  611. rx->offset + rx->status > PAGE_SIZE)) {
  612. if (net_ratelimit())
  613. dev_warn(dev, "rx->offset: %x, size: %u\n",
  614. rx->offset, rx->status);
  615. xennet_move_rx_slot(np, skb, ref);
  616. err = -EINVAL;
  617. goto next;
  618. }
  619. /*
  620. * This definitely indicates a bug, either in this driver or in
  621. * the backend driver. In future this should flag the bad
  622. * situation to the system controller to reboot the backend.
  623. */
  624. if (ref == GRANT_INVALID_REF) {
  625. if (net_ratelimit())
  626. dev_warn(dev, "Bad rx response id %d.\n",
  627. rx->id);
  628. err = -EINVAL;
  629. goto next;
  630. }
  631. ret = gnttab_end_foreign_access_ref(ref, 0);
  632. BUG_ON(!ret);
  633. gnttab_release_grant_reference(&np->gref_rx_head, ref);
  634. __skb_queue_tail(list, skb);
  635. next:
  636. if (!(rx->flags & XEN_NETRXF_more_data))
  637. break;
  638. if (cons + slots == rp) {
  639. if (net_ratelimit())
  640. dev_warn(dev, "Need more slots\n");
  641. err = -ENOENT;
  642. break;
  643. }
  644. rx = RING_GET_RESPONSE(&np->rx, cons + slots);
  645. skb = xennet_get_rx_skb(np, cons + slots);
  646. ref = xennet_get_rx_ref(np, cons + slots);
  647. slots++;
  648. }
  649. if (unlikely(slots > max)) {
  650. if (net_ratelimit())
  651. dev_warn(dev, "Too many slots\n");
  652. err = -E2BIG;
  653. }
  654. if (unlikely(err))
  655. np->rx.rsp_cons = cons + slots;
  656. return err;
  657. }
  658. static int xennet_set_skb_gso(struct sk_buff *skb,
  659. struct xen_netif_extra_info *gso)
  660. {
  661. if (!gso->u.gso.size) {
  662. if (net_ratelimit())
  663. printk(KERN_WARNING "GSO size must not be zero.\n");
  664. return -EINVAL;
  665. }
  666. /* Currently only TCPv4 S.O. is supported. */
  667. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
  668. if (net_ratelimit())
  669. printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
  670. return -EINVAL;
  671. }
  672. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  673. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  674. /* Header must be checked, and gso_segs computed. */
  675. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  676. skb_shinfo(skb)->gso_segs = 0;
  677. return 0;
  678. }
  679. static RING_IDX xennet_fill_frags(struct netfront_info *np,
  680. struct sk_buff *skb,
  681. struct sk_buff_head *list)
  682. {
  683. struct skb_shared_info *shinfo = skb_shinfo(skb);
  684. int nr_frags = shinfo->nr_frags;
  685. RING_IDX cons = np->rx.rsp_cons;
  686. struct sk_buff *nskb;
  687. while ((nskb = __skb_dequeue(list))) {
  688. struct xen_netif_rx_response *rx =
  689. RING_GET_RESPONSE(&np->rx, ++cons);
  690. skb_frag_t *nfrag = &skb_shinfo(nskb)->frags[0];
  691. __skb_fill_page_desc(skb, nr_frags,
  692. skb_frag_page(nfrag),
  693. rx->offset, rx->status);
  694. skb->data_len += rx->status;
  695. skb_shinfo(nskb)->nr_frags = 0;
  696. kfree_skb(nskb);
  697. nr_frags++;
  698. }
  699. shinfo->nr_frags = nr_frags;
  700. return cons;
  701. }
  702. static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
  703. {
  704. struct iphdr *iph;
  705. int err = -EPROTO;
  706. int recalculate_partial_csum = 0;
  707. /*
  708. * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  709. * peers can fail to set NETRXF_csum_blank when sending a GSO
  710. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  711. * recalculate the partial checksum.
  712. */
  713. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  714. struct netfront_info *np = netdev_priv(dev);
  715. np->rx_gso_checksum_fixup++;
  716. skb->ip_summed = CHECKSUM_PARTIAL;
  717. recalculate_partial_csum = 1;
  718. }
  719. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  720. if (skb->ip_summed != CHECKSUM_PARTIAL)
  721. return 0;
  722. if (skb->protocol != htons(ETH_P_IP))
  723. goto out;
  724. iph = (void *)skb->data;
  725. switch (iph->protocol) {
  726. case IPPROTO_TCP:
  727. if (!skb_partial_csum_set(skb, 4 * iph->ihl,
  728. offsetof(struct tcphdr, check)))
  729. goto out;
  730. if (recalculate_partial_csum) {
  731. struct tcphdr *tcph = tcp_hdr(skb);
  732. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  733. skb->len - iph->ihl*4,
  734. IPPROTO_TCP, 0);
  735. }
  736. break;
  737. case IPPROTO_UDP:
  738. if (!skb_partial_csum_set(skb, 4 * iph->ihl,
  739. offsetof(struct udphdr, check)))
  740. goto out;
  741. if (recalculate_partial_csum) {
  742. struct udphdr *udph = udp_hdr(skb);
  743. udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  744. skb->len - iph->ihl*4,
  745. IPPROTO_UDP, 0);
  746. }
  747. break;
  748. default:
  749. if (net_ratelimit())
  750. printk(KERN_ERR "Attempting to checksum a non-"
  751. "TCP/UDP packet, dropping a protocol"
  752. " %d packet", iph->protocol);
  753. goto out;
  754. }
  755. err = 0;
  756. out:
  757. return err;
  758. }
  759. static int handle_incoming_queue(struct net_device *dev,
  760. struct sk_buff_head *rxq)
  761. {
  762. struct netfront_info *np = netdev_priv(dev);
  763. struct netfront_stats *stats = this_cpu_ptr(np->stats);
  764. int packets_dropped = 0;
  765. struct sk_buff *skb;
  766. while ((skb = __skb_dequeue(rxq)) != NULL) {
  767. int pull_to = NETFRONT_SKB_CB(skb)->pull_to;
  768. __pskb_pull_tail(skb, pull_to - skb_headlen(skb));
  769. /* Ethernet work: Delayed to here as it peeks the header. */
  770. skb->protocol = eth_type_trans(skb, dev);
  771. if (checksum_setup(dev, skb)) {
  772. kfree_skb(skb);
  773. packets_dropped++;
  774. dev->stats.rx_errors++;
  775. continue;
  776. }
  777. u64_stats_update_begin(&stats->syncp);
  778. stats->rx_packets++;
  779. stats->rx_bytes += skb->len;
  780. u64_stats_update_end(&stats->syncp);
  781. /* Pass it up. */
  782. netif_receive_skb(skb);
  783. }
  784. return packets_dropped;
  785. }
  786. static int xennet_poll(struct napi_struct *napi, int budget)
  787. {
  788. struct netfront_info *np = container_of(napi, struct netfront_info, napi);
  789. struct net_device *dev = np->netdev;
  790. struct sk_buff *skb;
  791. struct netfront_rx_info rinfo;
  792. struct xen_netif_rx_response *rx = &rinfo.rx;
  793. struct xen_netif_extra_info *extras = rinfo.extras;
  794. RING_IDX i, rp;
  795. int work_done;
  796. struct sk_buff_head rxq;
  797. struct sk_buff_head errq;
  798. struct sk_buff_head tmpq;
  799. unsigned long flags;
  800. int err;
  801. spin_lock(&np->rx_lock);
  802. skb_queue_head_init(&rxq);
  803. skb_queue_head_init(&errq);
  804. skb_queue_head_init(&tmpq);
  805. rp = np->rx.sring->rsp_prod;
  806. rmb(); /* Ensure we see queued responses up to 'rp'. */
  807. i = np->rx.rsp_cons;
  808. work_done = 0;
  809. while ((i != rp) && (work_done < budget)) {
  810. memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
  811. memset(extras, 0, sizeof(rinfo.extras));
  812. err = xennet_get_responses(np, &rinfo, rp, &tmpq);
  813. if (unlikely(err)) {
  814. err:
  815. while ((skb = __skb_dequeue(&tmpq)))
  816. __skb_queue_tail(&errq, skb);
  817. dev->stats.rx_errors++;
  818. i = np->rx.rsp_cons;
  819. continue;
  820. }
  821. skb = __skb_dequeue(&tmpq);
  822. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  823. struct xen_netif_extra_info *gso;
  824. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  825. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  826. __skb_queue_head(&tmpq, skb);
  827. np->rx.rsp_cons += skb_queue_len(&tmpq);
  828. goto err;
  829. }
  830. }
  831. NETFRONT_SKB_CB(skb)->pull_to = rx->status;
  832. if (NETFRONT_SKB_CB(skb)->pull_to > RX_COPY_THRESHOLD)
  833. NETFRONT_SKB_CB(skb)->pull_to = RX_COPY_THRESHOLD;
  834. skb_shinfo(skb)->frags[0].page_offset = rx->offset;
  835. skb_frag_size_set(&skb_shinfo(skb)->frags[0], rx->status);
  836. skb->data_len = rx->status;
  837. i = xennet_fill_frags(np, skb, &tmpq);
  838. /*
  839. * Truesize is the actual allocation size, even if the
  840. * allocation is only partially used.
  841. */
  842. skb->truesize += PAGE_SIZE * skb_shinfo(skb)->nr_frags;
  843. skb->len += skb->data_len;
  844. if (rx->flags & XEN_NETRXF_csum_blank)
  845. skb->ip_summed = CHECKSUM_PARTIAL;
  846. else if (rx->flags & XEN_NETRXF_data_validated)
  847. skb->ip_summed = CHECKSUM_UNNECESSARY;
  848. __skb_queue_tail(&rxq, skb);
  849. np->rx.rsp_cons = ++i;
  850. work_done++;
  851. }
  852. __skb_queue_purge(&errq);
  853. work_done -= handle_incoming_queue(dev, &rxq);
  854. /* If we get a callback with very few responses, reduce fill target. */
  855. /* NB. Note exponential increase, linear decrease. */
  856. if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
  857. ((3*np->rx_target) / 4)) &&
  858. (--np->rx_target < np->rx_min_target))
  859. np->rx_target = np->rx_min_target;
  860. xennet_alloc_rx_buffers(dev);
  861. if (work_done < budget) {
  862. int more_to_do = 0;
  863. local_irq_save(flags);
  864. RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
  865. if (!more_to_do)
  866. __napi_complete(napi);
  867. local_irq_restore(flags);
  868. }
  869. spin_unlock(&np->rx_lock);
  870. return work_done;
  871. }
  872. static int xennet_change_mtu(struct net_device *dev, int mtu)
  873. {
  874. int max = xennet_can_sg(dev) ?
  875. XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER : ETH_DATA_LEN;
  876. if (mtu > max)
  877. return -EINVAL;
  878. dev->mtu = mtu;
  879. return 0;
  880. }
  881. static struct rtnl_link_stats64 *xennet_get_stats64(struct net_device *dev,
  882. struct rtnl_link_stats64 *tot)
  883. {
  884. struct netfront_info *np = netdev_priv(dev);
  885. int cpu;
  886. for_each_possible_cpu(cpu) {
  887. struct netfront_stats *stats = per_cpu_ptr(np->stats, cpu);
  888. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  889. unsigned int start;
  890. do {
  891. start = u64_stats_fetch_begin_bh(&stats->syncp);
  892. rx_packets = stats->rx_packets;
  893. tx_packets = stats->tx_packets;
  894. rx_bytes = stats->rx_bytes;
  895. tx_bytes = stats->tx_bytes;
  896. } while (u64_stats_fetch_retry_bh(&stats->syncp, start));
  897. tot->rx_packets += rx_packets;
  898. tot->tx_packets += tx_packets;
  899. tot->rx_bytes += rx_bytes;
  900. tot->tx_bytes += tx_bytes;
  901. }
  902. tot->rx_errors = dev->stats.rx_errors;
  903. tot->tx_dropped = dev->stats.tx_dropped;
  904. return tot;
  905. }
  906. static void xennet_release_tx_bufs(struct netfront_info *np)
  907. {
  908. struct sk_buff *skb;
  909. int i;
  910. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  911. /* Skip over entries which are actually freelist references */
  912. if (skb_entry_is_link(&np->tx_skbs[i]))
  913. continue;
  914. skb = np->tx_skbs[i].skb;
  915. gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
  916. GNTMAP_readonly);
  917. gnttab_release_grant_reference(&np->gref_tx_head,
  918. np->grant_tx_ref[i]);
  919. np->grant_tx_ref[i] = GRANT_INVALID_REF;
  920. add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
  921. dev_kfree_skb_irq(skb);
  922. }
  923. }
  924. static void xennet_release_rx_bufs(struct netfront_info *np)
  925. {
  926. struct mmu_update *mmu = np->rx_mmu;
  927. struct multicall_entry *mcl = np->rx_mcl;
  928. struct sk_buff_head free_list;
  929. struct sk_buff *skb;
  930. unsigned long mfn;
  931. int xfer = 0, noxfer = 0, unused = 0;
  932. int id, ref;
  933. dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
  934. __func__);
  935. return;
  936. skb_queue_head_init(&free_list);
  937. spin_lock_bh(&np->rx_lock);
  938. for (id = 0; id < NET_RX_RING_SIZE; id++) {
  939. ref = np->grant_rx_ref[id];
  940. if (ref == GRANT_INVALID_REF) {
  941. unused++;
  942. continue;
  943. }
  944. skb = np->rx_skbs[id];
  945. mfn = gnttab_end_foreign_transfer_ref(ref);
  946. gnttab_release_grant_reference(&np->gref_rx_head, ref);
  947. np->grant_rx_ref[id] = GRANT_INVALID_REF;
  948. if (0 == mfn) {
  949. skb_shinfo(skb)->nr_frags = 0;
  950. dev_kfree_skb(skb);
  951. noxfer++;
  952. continue;
  953. }
  954. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  955. /* Remap the page. */
  956. const struct page *page =
  957. skb_frag_page(&skb_shinfo(skb)->frags[0]);
  958. unsigned long pfn = page_to_pfn(page);
  959. void *vaddr = page_address(page);
  960. MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
  961. mfn_pte(mfn, PAGE_KERNEL),
  962. 0);
  963. mcl++;
  964. mmu->ptr = ((u64)mfn << PAGE_SHIFT)
  965. | MMU_MACHPHYS_UPDATE;
  966. mmu->val = pfn;
  967. mmu++;
  968. set_phys_to_machine(pfn, mfn);
  969. }
  970. __skb_queue_tail(&free_list, skb);
  971. xfer++;
  972. }
  973. dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
  974. __func__, xfer, noxfer, unused);
  975. if (xfer) {
  976. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  977. /* Do all the remapping work and M2P updates. */
  978. MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
  979. NULL, DOMID_SELF);
  980. mcl++;
  981. HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
  982. }
  983. }
  984. __skb_queue_purge(&free_list);
  985. spin_unlock_bh(&np->rx_lock);
  986. }
  987. static void xennet_uninit(struct net_device *dev)
  988. {
  989. struct netfront_info *np = netdev_priv(dev);
  990. xennet_release_tx_bufs(np);
  991. xennet_release_rx_bufs(np);
  992. gnttab_free_grant_references(np->gref_tx_head);
  993. gnttab_free_grant_references(np->gref_rx_head);
  994. }
  995. static netdev_features_t xennet_fix_features(struct net_device *dev,
  996. netdev_features_t features)
  997. {
  998. struct netfront_info *np = netdev_priv(dev);
  999. int val;
  1000. if (features & NETIF_F_SG) {
  1001. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
  1002. "%d", &val) < 0)
  1003. val = 0;
  1004. if (!val)
  1005. features &= ~NETIF_F_SG;
  1006. }
  1007. if (features & NETIF_F_TSO) {
  1008. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1009. "feature-gso-tcpv4", "%d", &val) < 0)
  1010. val = 0;
  1011. if (!val)
  1012. features &= ~NETIF_F_TSO;
  1013. }
  1014. return features;
  1015. }
  1016. static int xennet_set_features(struct net_device *dev,
  1017. netdev_features_t features)
  1018. {
  1019. if (!(features & NETIF_F_SG) && dev->mtu > ETH_DATA_LEN) {
  1020. netdev_info(dev, "Reducing MTU because no SG offload");
  1021. dev->mtu = ETH_DATA_LEN;
  1022. }
  1023. return 0;
  1024. }
  1025. static irqreturn_t xennet_tx_interrupt(int irq, void *dev_id)
  1026. {
  1027. struct netfront_info *np = dev_id;
  1028. struct net_device *dev = np->netdev;
  1029. unsigned long flags;
  1030. spin_lock_irqsave(&np->tx_lock, flags);
  1031. xennet_tx_buf_gc(dev);
  1032. spin_unlock_irqrestore(&np->tx_lock, flags);
  1033. return IRQ_HANDLED;
  1034. }
  1035. static irqreturn_t xennet_rx_interrupt(int irq, void *dev_id)
  1036. {
  1037. struct netfront_info *np = dev_id;
  1038. struct net_device *dev = np->netdev;
  1039. if (likely(netif_carrier_ok(dev) &&
  1040. RING_HAS_UNCONSUMED_RESPONSES(&np->rx)))
  1041. napi_schedule(&np->napi);
  1042. return IRQ_HANDLED;
  1043. }
  1044. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1045. {
  1046. xennet_tx_interrupt(irq, dev_id);
  1047. xennet_rx_interrupt(irq, dev_id);
  1048. return IRQ_HANDLED;
  1049. }
  1050. #ifdef CONFIG_NET_POLL_CONTROLLER
  1051. static void xennet_poll_controller(struct net_device *dev)
  1052. {
  1053. xennet_interrupt(0, dev);
  1054. }
  1055. #endif
  1056. static const struct net_device_ops xennet_netdev_ops = {
  1057. .ndo_open = xennet_open,
  1058. .ndo_uninit = xennet_uninit,
  1059. .ndo_stop = xennet_close,
  1060. .ndo_start_xmit = xennet_start_xmit,
  1061. .ndo_change_mtu = xennet_change_mtu,
  1062. .ndo_get_stats64 = xennet_get_stats64,
  1063. .ndo_set_mac_address = eth_mac_addr,
  1064. .ndo_validate_addr = eth_validate_addr,
  1065. .ndo_fix_features = xennet_fix_features,
  1066. .ndo_set_features = xennet_set_features,
  1067. #ifdef CONFIG_NET_POLL_CONTROLLER
  1068. .ndo_poll_controller = xennet_poll_controller,
  1069. #endif
  1070. };
  1071. static struct net_device *xennet_create_dev(struct xenbus_device *dev)
  1072. {
  1073. int i, err;
  1074. struct net_device *netdev;
  1075. struct netfront_info *np;
  1076. netdev = alloc_etherdev(sizeof(struct netfront_info));
  1077. if (!netdev)
  1078. return ERR_PTR(-ENOMEM);
  1079. np = netdev_priv(netdev);
  1080. np->xbdev = dev;
  1081. spin_lock_init(&np->tx_lock);
  1082. spin_lock_init(&np->rx_lock);
  1083. skb_queue_head_init(&np->rx_batch);
  1084. np->rx_target = RX_DFL_MIN_TARGET;
  1085. np->rx_min_target = RX_DFL_MIN_TARGET;
  1086. np->rx_max_target = RX_MAX_TARGET;
  1087. init_timer(&np->rx_refill_timer);
  1088. np->rx_refill_timer.data = (unsigned long)netdev;
  1089. np->rx_refill_timer.function = rx_refill_timeout;
  1090. err = -ENOMEM;
  1091. np->stats = alloc_percpu(struct netfront_stats);
  1092. if (np->stats == NULL)
  1093. goto exit;
  1094. /* Initialise tx_skbs as a free chain containing every entry. */
  1095. np->tx_skb_freelist = 0;
  1096. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  1097. skb_entry_set_link(&np->tx_skbs[i], i+1);
  1098. np->grant_tx_ref[i] = GRANT_INVALID_REF;
  1099. }
  1100. /* Clear out rx_skbs */
  1101. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  1102. np->rx_skbs[i] = NULL;
  1103. np->grant_rx_ref[i] = GRANT_INVALID_REF;
  1104. }
  1105. /* A grant for every tx ring slot */
  1106. if (gnttab_alloc_grant_references(TX_MAX_TARGET,
  1107. &np->gref_tx_head) < 0) {
  1108. printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
  1109. err = -ENOMEM;
  1110. goto exit_free_stats;
  1111. }
  1112. /* A grant for every rx ring slot */
  1113. if (gnttab_alloc_grant_references(RX_MAX_TARGET,
  1114. &np->gref_rx_head) < 0) {
  1115. printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
  1116. err = -ENOMEM;
  1117. goto exit_free_tx;
  1118. }
  1119. netdev->netdev_ops = &xennet_netdev_ops;
  1120. netif_napi_add(netdev, &np->napi, xennet_poll, 64);
  1121. netdev->features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
  1122. NETIF_F_GSO_ROBUST;
  1123. netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO;
  1124. /*
  1125. * Assume that all hw features are available for now. This set
  1126. * will be adjusted by the call to netdev_update_features() in
  1127. * xennet_connect() which is the earliest point where we can
  1128. * negotiate with the backend regarding supported features.
  1129. */
  1130. netdev->features |= netdev->hw_features;
  1131. SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
  1132. SET_NETDEV_DEV(netdev, &dev->dev);
  1133. netif_set_gso_max_size(netdev, XEN_NETIF_MAX_TX_SIZE - MAX_TCP_HEADER);
  1134. np->netdev = netdev;
  1135. netif_carrier_off(netdev);
  1136. return netdev;
  1137. exit_free_tx:
  1138. gnttab_free_grant_references(np->gref_tx_head);
  1139. exit_free_stats:
  1140. free_percpu(np->stats);
  1141. exit:
  1142. free_netdev(netdev);
  1143. return ERR_PTR(err);
  1144. }
  1145. /**
  1146. * Entry point to this code when a new device is created. Allocate the basic
  1147. * structures and the ring buffers for communication with the backend, and
  1148. * inform the backend of the appropriate details for those.
  1149. */
  1150. static int netfront_probe(struct xenbus_device *dev,
  1151. const struct xenbus_device_id *id)
  1152. {
  1153. int err;
  1154. struct net_device *netdev;
  1155. struct netfront_info *info;
  1156. netdev = xennet_create_dev(dev);
  1157. if (IS_ERR(netdev)) {
  1158. err = PTR_ERR(netdev);
  1159. xenbus_dev_fatal(dev, err, "creating netdev");
  1160. return err;
  1161. }
  1162. info = netdev_priv(netdev);
  1163. dev_set_drvdata(&dev->dev, info);
  1164. err = register_netdev(info->netdev);
  1165. if (err) {
  1166. printk(KERN_WARNING "%s: register_netdev err=%d\n",
  1167. __func__, err);
  1168. goto fail;
  1169. }
  1170. err = xennet_sysfs_addif(info->netdev);
  1171. if (err) {
  1172. unregister_netdev(info->netdev);
  1173. printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
  1174. __func__, err);
  1175. goto fail;
  1176. }
  1177. return 0;
  1178. fail:
  1179. free_netdev(netdev);
  1180. dev_set_drvdata(&dev->dev, NULL);
  1181. return err;
  1182. }
  1183. static void xennet_end_access(int ref, void *page)
  1184. {
  1185. /* This frees the page as a side-effect */
  1186. if (ref != GRANT_INVALID_REF)
  1187. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1188. }
  1189. static void xennet_disconnect_backend(struct netfront_info *info)
  1190. {
  1191. /* Stop old i/f to prevent errors whilst we rebuild the state. */
  1192. spin_lock_bh(&info->rx_lock);
  1193. spin_lock_irq(&info->tx_lock);
  1194. netif_carrier_off(info->netdev);
  1195. spin_unlock_irq(&info->tx_lock);
  1196. spin_unlock_bh(&info->rx_lock);
  1197. if (info->tx_irq && (info->tx_irq == info->rx_irq))
  1198. unbind_from_irqhandler(info->tx_irq, info);
  1199. if (info->tx_irq && (info->tx_irq != info->rx_irq)) {
  1200. unbind_from_irqhandler(info->tx_irq, info);
  1201. unbind_from_irqhandler(info->rx_irq, info);
  1202. }
  1203. info->tx_evtchn = info->rx_evtchn = 0;
  1204. info->tx_irq = info->rx_irq = 0;
  1205. /* End access and free the pages */
  1206. xennet_end_access(info->tx_ring_ref, info->tx.sring);
  1207. xennet_end_access(info->rx_ring_ref, info->rx.sring);
  1208. info->tx_ring_ref = GRANT_INVALID_REF;
  1209. info->rx_ring_ref = GRANT_INVALID_REF;
  1210. info->tx.sring = NULL;
  1211. info->rx.sring = NULL;
  1212. }
  1213. /**
  1214. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1215. * driver restart. We tear down our netif structure and recreate it, but
  1216. * leave the device-layer structures intact so that this is transparent to the
  1217. * rest of the kernel.
  1218. */
  1219. static int netfront_resume(struct xenbus_device *dev)
  1220. {
  1221. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1222. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1223. xennet_disconnect_backend(info);
  1224. return 0;
  1225. }
  1226. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1227. {
  1228. char *s, *e, *macstr;
  1229. int i;
  1230. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1231. if (IS_ERR(macstr))
  1232. return PTR_ERR(macstr);
  1233. for (i = 0; i < ETH_ALEN; i++) {
  1234. mac[i] = simple_strtoul(s, &e, 16);
  1235. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1236. kfree(macstr);
  1237. return -ENOENT;
  1238. }
  1239. s = e+1;
  1240. }
  1241. kfree(macstr);
  1242. return 0;
  1243. }
  1244. static int setup_netfront_single(struct netfront_info *info)
  1245. {
  1246. int err;
  1247. err = xenbus_alloc_evtchn(info->xbdev, &info->tx_evtchn);
  1248. if (err < 0)
  1249. goto fail;
  1250. err = bind_evtchn_to_irqhandler(info->tx_evtchn,
  1251. xennet_interrupt,
  1252. 0, info->netdev->name, info);
  1253. if (err < 0)
  1254. goto bind_fail;
  1255. info->rx_evtchn = info->tx_evtchn;
  1256. info->rx_irq = info->tx_irq = err;
  1257. return 0;
  1258. bind_fail:
  1259. xenbus_free_evtchn(info->xbdev, info->tx_evtchn);
  1260. info->tx_evtchn = 0;
  1261. fail:
  1262. return err;
  1263. }
  1264. static int setup_netfront_split(struct netfront_info *info)
  1265. {
  1266. int err;
  1267. err = xenbus_alloc_evtchn(info->xbdev, &info->tx_evtchn);
  1268. if (err < 0)
  1269. goto fail;
  1270. err = xenbus_alloc_evtchn(info->xbdev, &info->rx_evtchn);
  1271. if (err < 0)
  1272. goto alloc_rx_evtchn_fail;
  1273. snprintf(info->tx_irq_name, sizeof(info->tx_irq_name),
  1274. "%s-tx", info->netdev->name);
  1275. err = bind_evtchn_to_irqhandler(info->tx_evtchn,
  1276. xennet_tx_interrupt,
  1277. 0, info->tx_irq_name, info);
  1278. if (err < 0)
  1279. goto bind_tx_fail;
  1280. info->tx_irq = err;
  1281. snprintf(info->rx_irq_name, sizeof(info->rx_irq_name),
  1282. "%s-rx", info->netdev->name);
  1283. err = bind_evtchn_to_irqhandler(info->rx_evtchn,
  1284. xennet_rx_interrupt,
  1285. 0, info->rx_irq_name, info);
  1286. if (err < 0)
  1287. goto bind_rx_fail;
  1288. info->rx_irq = err;
  1289. return 0;
  1290. bind_rx_fail:
  1291. unbind_from_irqhandler(info->tx_irq, info);
  1292. info->tx_irq = 0;
  1293. bind_tx_fail:
  1294. xenbus_free_evtchn(info->xbdev, info->rx_evtchn);
  1295. info->rx_evtchn = 0;
  1296. alloc_rx_evtchn_fail:
  1297. xenbus_free_evtchn(info->xbdev, info->tx_evtchn);
  1298. info->tx_evtchn = 0;
  1299. fail:
  1300. return err;
  1301. }
  1302. static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
  1303. {
  1304. struct xen_netif_tx_sring *txs;
  1305. struct xen_netif_rx_sring *rxs;
  1306. int err;
  1307. struct net_device *netdev = info->netdev;
  1308. unsigned int feature_split_evtchn;
  1309. info->tx_ring_ref = GRANT_INVALID_REF;
  1310. info->rx_ring_ref = GRANT_INVALID_REF;
  1311. info->rx.sring = NULL;
  1312. info->tx.sring = NULL;
  1313. netdev->irq = 0;
  1314. err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  1315. "feature-split-event-channels", "%u",
  1316. &feature_split_evtchn);
  1317. if (err < 0)
  1318. feature_split_evtchn = 0;
  1319. err = xen_net_read_mac(dev, netdev->dev_addr);
  1320. if (err) {
  1321. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1322. goto fail;
  1323. }
  1324. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1325. if (!txs) {
  1326. err = -ENOMEM;
  1327. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1328. goto fail;
  1329. }
  1330. SHARED_RING_INIT(txs);
  1331. FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
  1332. err = xenbus_grant_ring(dev, virt_to_mfn(txs));
  1333. if (err < 0)
  1334. goto grant_tx_ring_fail;
  1335. info->tx_ring_ref = err;
  1336. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1337. if (!rxs) {
  1338. err = -ENOMEM;
  1339. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1340. goto alloc_rx_ring_fail;
  1341. }
  1342. SHARED_RING_INIT(rxs);
  1343. FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
  1344. err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
  1345. if (err < 0)
  1346. goto grant_rx_ring_fail;
  1347. info->rx_ring_ref = err;
  1348. if (feature_split_evtchn)
  1349. err = setup_netfront_split(info);
  1350. /* setup single event channel if
  1351. * a) feature-split-event-channels == 0
  1352. * b) feature-split-event-channels == 1 but failed to setup
  1353. */
  1354. if (!feature_split_evtchn || (feature_split_evtchn && err))
  1355. err = setup_netfront_single(info);
  1356. if (err)
  1357. goto alloc_evtchn_fail;
  1358. return 0;
  1359. /* If we fail to setup netfront, it is safe to just revoke access to
  1360. * granted pages because backend is not accessing it at this point.
  1361. */
  1362. alloc_evtchn_fail:
  1363. gnttab_end_foreign_access_ref(info->rx_ring_ref, 0);
  1364. grant_rx_ring_fail:
  1365. free_page((unsigned long)rxs);
  1366. alloc_rx_ring_fail:
  1367. gnttab_end_foreign_access_ref(info->tx_ring_ref, 0);
  1368. grant_tx_ring_fail:
  1369. free_page((unsigned long)txs);
  1370. fail:
  1371. return err;
  1372. }
  1373. /* Common code used when first setting up, and when resuming. */
  1374. static int talk_to_netback(struct xenbus_device *dev,
  1375. struct netfront_info *info)
  1376. {
  1377. const char *message;
  1378. struct xenbus_transaction xbt;
  1379. int err;
  1380. /* Create shared ring, alloc event channel. */
  1381. err = setup_netfront(dev, info);
  1382. if (err)
  1383. goto out;
  1384. again:
  1385. err = xenbus_transaction_start(&xbt);
  1386. if (err) {
  1387. xenbus_dev_fatal(dev, err, "starting transaction");
  1388. goto destroy_ring;
  1389. }
  1390. err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
  1391. info->tx_ring_ref);
  1392. if (err) {
  1393. message = "writing tx ring-ref";
  1394. goto abort_transaction;
  1395. }
  1396. err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
  1397. info->rx_ring_ref);
  1398. if (err) {
  1399. message = "writing rx ring-ref";
  1400. goto abort_transaction;
  1401. }
  1402. if (info->tx_evtchn == info->rx_evtchn) {
  1403. err = xenbus_printf(xbt, dev->nodename,
  1404. "event-channel", "%u", info->tx_evtchn);
  1405. if (err) {
  1406. message = "writing event-channel";
  1407. goto abort_transaction;
  1408. }
  1409. } else {
  1410. err = xenbus_printf(xbt, dev->nodename,
  1411. "event-channel-tx", "%u", info->tx_evtchn);
  1412. if (err) {
  1413. message = "writing event-channel-tx";
  1414. goto abort_transaction;
  1415. }
  1416. err = xenbus_printf(xbt, dev->nodename,
  1417. "event-channel-rx", "%u", info->rx_evtchn);
  1418. if (err) {
  1419. message = "writing event-channel-rx";
  1420. goto abort_transaction;
  1421. }
  1422. }
  1423. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1424. 1);
  1425. if (err) {
  1426. message = "writing request-rx-copy";
  1427. goto abort_transaction;
  1428. }
  1429. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1430. if (err) {
  1431. message = "writing feature-rx-notify";
  1432. goto abort_transaction;
  1433. }
  1434. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1435. if (err) {
  1436. message = "writing feature-sg";
  1437. goto abort_transaction;
  1438. }
  1439. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1440. if (err) {
  1441. message = "writing feature-gso-tcpv4";
  1442. goto abort_transaction;
  1443. }
  1444. err = xenbus_transaction_end(xbt, 0);
  1445. if (err) {
  1446. if (err == -EAGAIN)
  1447. goto again;
  1448. xenbus_dev_fatal(dev, err, "completing transaction");
  1449. goto destroy_ring;
  1450. }
  1451. return 0;
  1452. abort_transaction:
  1453. xenbus_transaction_end(xbt, 1);
  1454. xenbus_dev_fatal(dev, err, "%s", message);
  1455. destroy_ring:
  1456. xennet_disconnect_backend(info);
  1457. out:
  1458. return err;
  1459. }
  1460. static int xennet_connect(struct net_device *dev)
  1461. {
  1462. struct netfront_info *np = netdev_priv(dev);
  1463. int i, requeue_idx, err;
  1464. struct sk_buff *skb;
  1465. grant_ref_t ref;
  1466. struct xen_netif_rx_request *req;
  1467. unsigned int feature_rx_copy;
  1468. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1469. "feature-rx-copy", "%u", &feature_rx_copy);
  1470. if (err != 1)
  1471. feature_rx_copy = 0;
  1472. if (!feature_rx_copy) {
  1473. dev_info(&dev->dev,
  1474. "backend does not support copying receive path\n");
  1475. return -ENODEV;
  1476. }
  1477. err = talk_to_netback(np->xbdev, np);
  1478. if (err)
  1479. return err;
  1480. rtnl_lock();
  1481. netdev_update_features(dev);
  1482. rtnl_unlock();
  1483. spin_lock_bh(&np->rx_lock);
  1484. spin_lock_irq(&np->tx_lock);
  1485. /* Step 1: Discard all pending TX packet fragments. */
  1486. xennet_release_tx_bufs(np);
  1487. /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
  1488. for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
  1489. skb_frag_t *frag;
  1490. const struct page *page;
  1491. if (!np->rx_skbs[i])
  1492. continue;
  1493. skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
  1494. ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
  1495. req = RING_GET_REQUEST(&np->rx, requeue_idx);
  1496. frag = &skb_shinfo(skb)->frags[0];
  1497. page = skb_frag_page(frag);
  1498. gnttab_grant_foreign_access_ref(
  1499. ref, np->xbdev->otherend_id,
  1500. pfn_to_mfn(page_to_pfn(page)),
  1501. 0);
  1502. req->gref = ref;
  1503. req->id = requeue_idx;
  1504. requeue_idx++;
  1505. }
  1506. np->rx.req_prod_pvt = requeue_idx;
  1507. /*
  1508. * Step 3: All public and private state should now be sane. Get
  1509. * ready to start sending and receiving packets and give the driver
  1510. * domain a kick because we've probably just requeued some
  1511. * packets.
  1512. */
  1513. netif_carrier_on(np->netdev);
  1514. notify_remote_via_irq(np->tx_irq);
  1515. if (np->tx_irq != np->rx_irq)
  1516. notify_remote_via_irq(np->rx_irq);
  1517. xennet_tx_buf_gc(dev);
  1518. xennet_alloc_rx_buffers(dev);
  1519. spin_unlock_irq(&np->tx_lock);
  1520. spin_unlock_bh(&np->rx_lock);
  1521. return 0;
  1522. }
  1523. /**
  1524. * Callback received when the backend's state changes.
  1525. */
  1526. static void netback_changed(struct xenbus_device *dev,
  1527. enum xenbus_state backend_state)
  1528. {
  1529. struct netfront_info *np = dev_get_drvdata(&dev->dev);
  1530. struct net_device *netdev = np->netdev;
  1531. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1532. switch (backend_state) {
  1533. case XenbusStateInitialising:
  1534. case XenbusStateInitialised:
  1535. case XenbusStateReconfiguring:
  1536. case XenbusStateReconfigured:
  1537. case XenbusStateUnknown:
  1538. case XenbusStateClosed:
  1539. break;
  1540. case XenbusStateInitWait:
  1541. if (dev->state != XenbusStateInitialising)
  1542. break;
  1543. if (xennet_connect(netdev) != 0)
  1544. break;
  1545. xenbus_switch_state(dev, XenbusStateConnected);
  1546. break;
  1547. case XenbusStateConnected:
  1548. netdev_notify_peers(netdev);
  1549. break;
  1550. case XenbusStateClosing:
  1551. xenbus_frontend_closed(dev);
  1552. break;
  1553. }
  1554. }
  1555. static const struct xennet_stat {
  1556. char name[ETH_GSTRING_LEN];
  1557. u16 offset;
  1558. } xennet_stats[] = {
  1559. {
  1560. "rx_gso_checksum_fixup",
  1561. offsetof(struct netfront_info, rx_gso_checksum_fixup)
  1562. },
  1563. };
  1564. static int xennet_get_sset_count(struct net_device *dev, int string_set)
  1565. {
  1566. switch (string_set) {
  1567. case ETH_SS_STATS:
  1568. return ARRAY_SIZE(xennet_stats);
  1569. default:
  1570. return -EINVAL;
  1571. }
  1572. }
  1573. static void xennet_get_ethtool_stats(struct net_device *dev,
  1574. struct ethtool_stats *stats, u64 * data)
  1575. {
  1576. void *np = netdev_priv(dev);
  1577. int i;
  1578. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1579. data[i] = *(unsigned long *)(np + xennet_stats[i].offset);
  1580. }
  1581. static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
  1582. {
  1583. int i;
  1584. switch (stringset) {
  1585. case ETH_SS_STATS:
  1586. for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
  1587. memcpy(data + i * ETH_GSTRING_LEN,
  1588. xennet_stats[i].name, ETH_GSTRING_LEN);
  1589. break;
  1590. }
  1591. }
  1592. static const struct ethtool_ops xennet_ethtool_ops =
  1593. {
  1594. .get_link = ethtool_op_get_link,
  1595. .get_sset_count = xennet_get_sset_count,
  1596. .get_ethtool_stats = xennet_get_ethtool_stats,
  1597. .get_strings = xennet_get_strings,
  1598. };
  1599. #ifdef CONFIG_SYSFS
  1600. static ssize_t show_rxbuf_min(struct device *dev,
  1601. struct device_attribute *attr, char *buf)
  1602. {
  1603. struct net_device *netdev = to_net_dev(dev);
  1604. struct netfront_info *info = netdev_priv(netdev);
  1605. return sprintf(buf, "%u\n", info->rx_min_target);
  1606. }
  1607. static ssize_t store_rxbuf_min(struct device *dev,
  1608. struct device_attribute *attr,
  1609. const char *buf, size_t len)
  1610. {
  1611. struct net_device *netdev = to_net_dev(dev);
  1612. struct netfront_info *np = netdev_priv(netdev);
  1613. char *endp;
  1614. unsigned long target;
  1615. if (!capable(CAP_NET_ADMIN))
  1616. return -EPERM;
  1617. target = simple_strtoul(buf, &endp, 0);
  1618. if (endp == buf)
  1619. return -EBADMSG;
  1620. if (target < RX_MIN_TARGET)
  1621. target = RX_MIN_TARGET;
  1622. if (target > RX_MAX_TARGET)
  1623. target = RX_MAX_TARGET;
  1624. spin_lock_bh(&np->rx_lock);
  1625. if (target > np->rx_max_target)
  1626. np->rx_max_target = target;
  1627. np->rx_min_target = target;
  1628. if (target > np->rx_target)
  1629. np->rx_target = target;
  1630. xennet_alloc_rx_buffers(netdev);
  1631. spin_unlock_bh(&np->rx_lock);
  1632. return len;
  1633. }
  1634. static ssize_t show_rxbuf_max(struct device *dev,
  1635. struct device_attribute *attr, char *buf)
  1636. {
  1637. struct net_device *netdev = to_net_dev(dev);
  1638. struct netfront_info *info = netdev_priv(netdev);
  1639. return sprintf(buf, "%u\n", info->rx_max_target);
  1640. }
  1641. static ssize_t store_rxbuf_max(struct device *dev,
  1642. struct device_attribute *attr,
  1643. const char *buf, size_t len)
  1644. {
  1645. struct net_device *netdev = to_net_dev(dev);
  1646. struct netfront_info *np = netdev_priv(netdev);
  1647. char *endp;
  1648. unsigned long target;
  1649. if (!capable(CAP_NET_ADMIN))
  1650. return -EPERM;
  1651. target = simple_strtoul(buf, &endp, 0);
  1652. if (endp == buf)
  1653. return -EBADMSG;
  1654. if (target < RX_MIN_TARGET)
  1655. target = RX_MIN_TARGET;
  1656. if (target > RX_MAX_TARGET)
  1657. target = RX_MAX_TARGET;
  1658. spin_lock_bh(&np->rx_lock);
  1659. if (target < np->rx_min_target)
  1660. np->rx_min_target = target;
  1661. np->rx_max_target = target;
  1662. if (target < np->rx_target)
  1663. np->rx_target = target;
  1664. xennet_alloc_rx_buffers(netdev);
  1665. spin_unlock_bh(&np->rx_lock);
  1666. return len;
  1667. }
  1668. static ssize_t show_rxbuf_cur(struct device *dev,
  1669. struct device_attribute *attr, char *buf)
  1670. {
  1671. struct net_device *netdev = to_net_dev(dev);
  1672. struct netfront_info *info = netdev_priv(netdev);
  1673. return sprintf(buf, "%u\n", info->rx_target);
  1674. }
  1675. static struct device_attribute xennet_attrs[] = {
  1676. __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
  1677. __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
  1678. __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
  1679. };
  1680. static int xennet_sysfs_addif(struct net_device *netdev)
  1681. {
  1682. int i;
  1683. int err;
  1684. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
  1685. err = device_create_file(&netdev->dev,
  1686. &xennet_attrs[i]);
  1687. if (err)
  1688. goto fail;
  1689. }
  1690. return 0;
  1691. fail:
  1692. while (--i >= 0)
  1693. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1694. return err;
  1695. }
  1696. static void xennet_sysfs_delif(struct net_device *netdev)
  1697. {
  1698. int i;
  1699. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
  1700. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1701. }
  1702. #endif /* CONFIG_SYSFS */
  1703. static const struct xenbus_device_id netfront_ids[] = {
  1704. { "vif" },
  1705. { "" }
  1706. };
  1707. static int xennet_remove(struct xenbus_device *dev)
  1708. {
  1709. struct netfront_info *info = dev_get_drvdata(&dev->dev);
  1710. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1711. xennet_disconnect_backend(info);
  1712. xennet_sysfs_delif(info->netdev);
  1713. unregister_netdev(info->netdev);
  1714. del_timer_sync(&info->rx_refill_timer);
  1715. free_percpu(info->stats);
  1716. free_netdev(info->netdev);
  1717. return 0;
  1718. }
  1719. static DEFINE_XENBUS_DRIVER(netfront, ,
  1720. .probe = netfront_probe,
  1721. .remove = xennet_remove,
  1722. .resume = netfront_resume,
  1723. .otherend_changed = netback_changed,
  1724. );
  1725. static int __init netif_init(void)
  1726. {
  1727. if (!xen_domain())
  1728. return -ENODEV;
  1729. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  1730. return -ENODEV;
  1731. printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
  1732. return xenbus_register_frontend(&netfront_driver);
  1733. }
  1734. module_init(netif_init);
  1735. static void __exit netif_exit(void)
  1736. {
  1737. xenbus_unregister_driver(&netfront_driver);
  1738. }
  1739. module_exit(netif_exit);
  1740. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1741. MODULE_LICENSE("GPL");
  1742. MODULE_ALIAS("xen:vif");
  1743. MODULE_ALIAS("xennet");