xen-netfront.c 47 KB

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