xen-netfront.c 49 KB

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