xen-netfront.c 49 KB

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