xen-netfront.c 49 KB

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