xen-netfront.c 45 KB

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