xen-netfront.c 46 KB

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