xen-netfront.c 44 KB

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