xen-netfront.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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. netif_rx_schedule(dev, &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. netif_rx_schedule(dev, &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. dev->last_rx = jiffies;
  693. }
  694. return packets_dropped;
  695. }
  696. static int xennet_poll(struct napi_struct *napi, int budget)
  697. {
  698. struct netfront_info *np = container_of(napi, struct netfront_info, napi);
  699. struct net_device *dev = np->netdev;
  700. struct sk_buff *skb;
  701. struct netfront_rx_info rinfo;
  702. struct xen_netif_rx_response *rx = &rinfo.rx;
  703. struct xen_netif_extra_info *extras = rinfo.extras;
  704. RING_IDX i, rp;
  705. int work_done;
  706. struct sk_buff_head rxq;
  707. struct sk_buff_head errq;
  708. struct sk_buff_head tmpq;
  709. unsigned long flags;
  710. unsigned int len;
  711. int err;
  712. spin_lock(&np->rx_lock);
  713. skb_queue_head_init(&rxq);
  714. skb_queue_head_init(&errq);
  715. skb_queue_head_init(&tmpq);
  716. rp = np->rx.sring->rsp_prod;
  717. rmb(); /* Ensure we see queued responses up to 'rp'. */
  718. i = np->rx.rsp_cons;
  719. work_done = 0;
  720. while ((i != rp) && (work_done < budget)) {
  721. memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
  722. memset(extras, 0, sizeof(rinfo.extras));
  723. err = xennet_get_responses(np, &rinfo, rp, &tmpq);
  724. if (unlikely(err)) {
  725. err:
  726. while ((skb = __skb_dequeue(&tmpq)))
  727. __skb_queue_tail(&errq, skb);
  728. dev->stats.rx_errors++;
  729. i = np->rx.rsp_cons;
  730. continue;
  731. }
  732. skb = __skb_dequeue(&tmpq);
  733. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  734. struct xen_netif_extra_info *gso;
  735. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  736. if (unlikely(xennet_set_skb_gso(skb, gso))) {
  737. __skb_queue_head(&tmpq, skb);
  738. np->rx.rsp_cons += skb_queue_len(&tmpq);
  739. goto err;
  740. }
  741. }
  742. NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
  743. NETFRONT_SKB_CB(skb)->offset = rx->offset;
  744. len = rx->status;
  745. if (len > RX_COPY_THRESHOLD)
  746. len = RX_COPY_THRESHOLD;
  747. skb_put(skb, len);
  748. if (rx->status > len) {
  749. skb_shinfo(skb)->frags[0].page_offset =
  750. rx->offset + len;
  751. skb_shinfo(skb)->frags[0].size = rx->status - len;
  752. skb->data_len = rx->status - len;
  753. } else {
  754. skb_shinfo(skb)->frags[0].page = NULL;
  755. skb_shinfo(skb)->nr_frags = 0;
  756. }
  757. i = xennet_fill_frags(np, skb, &tmpq);
  758. /*
  759. * Truesize approximates the size of true data plus
  760. * any supervisor overheads. Adding hypervisor
  761. * overheads has been shown to significantly reduce
  762. * achievable bandwidth with the default receive
  763. * buffer size. It is therefore not wise to account
  764. * for it here.
  765. *
  766. * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
  767. * to RX_COPY_THRESHOLD + the supervisor
  768. * overheads. Here, we add the size of the data pulled
  769. * in xennet_fill_frags().
  770. *
  771. * We also adjust for any unused space in the main
  772. * data area by subtracting (RX_COPY_THRESHOLD -
  773. * len). This is especially important with drivers
  774. * which split incoming packets into header and data,
  775. * using only 66 bytes of the main data area (see the
  776. * e1000 driver for example.) On such systems,
  777. * without this last adjustement, our achievable
  778. * receive throughout using the standard receive
  779. * buffer size was cut by 25%(!!!).
  780. */
  781. skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
  782. skb->len += skb->data_len;
  783. if (rx->flags & NETRXF_csum_blank)
  784. skb->ip_summed = CHECKSUM_PARTIAL;
  785. else if (rx->flags & NETRXF_data_validated)
  786. skb->ip_summed = CHECKSUM_UNNECESSARY;
  787. __skb_queue_tail(&rxq, skb);
  788. np->rx.rsp_cons = ++i;
  789. work_done++;
  790. }
  791. __skb_queue_purge(&errq);
  792. work_done -= handle_incoming_queue(dev, &rxq);
  793. /* If we get a callback with very few responses, reduce fill target. */
  794. /* NB. Note exponential increase, linear decrease. */
  795. if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
  796. ((3*np->rx_target) / 4)) &&
  797. (--np->rx_target < np->rx_min_target))
  798. np->rx_target = np->rx_min_target;
  799. xennet_alloc_rx_buffers(dev);
  800. if (work_done < budget) {
  801. int more_to_do = 0;
  802. local_irq_save(flags);
  803. RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
  804. if (!more_to_do)
  805. __netif_rx_complete(dev, napi);
  806. local_irq_restore(flags);
  807. }
  808. spin_unlock(&np->rx_lock);
  809. return work_done;
  810. }
  811. static int xennet_change_mtu(struct net_device *dev, int mtu)
  812. {
  813. int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
  814. if (mtu > max)
  815. return -EINVAL;
  816. dev->mtu = mtu;
  817. return 0;
  818. }
  819. static void xennet_release_tx_bufs(struct netfront_info *np)
  820. {
  821. struct sk_buff *skb;
  822. int i;
  823. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  824. /* Skip over entries which are actually freelist references */
  825. if (skb_entry_is_link(&np->tx_skbs[i]))
  826. continue;
  827. skb = np->tx_skbs[i].skb;
  828. gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
  829. GNTMAP_readonly);
  830. gnttab_release_grant_reference(&np->gref_tx_head,
  831. np->grant_tx_ref[i]);
  832. np->grant_tx_ref[i] = GRANT_INVALID_REF;
  833. add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
  834. dev_kfree_skb_irq(skb);
  835. }
  836. }
  837. static void xennet_release_rx_bufs(struct netfront_info *np)
  838. {
  839. struct mmu_update *mmu = np->rx_mmu;
  840. struct multicall_entry *mcl = np->rx_mcl;
  841. struct sk_buff_head free_list;
  842. struct sk_buff *skb;
  843. unsigned long mfn;
  844. int xfer = 0, noxfer = 0, unused = 0;
  845. int id, ref;
  846. dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
  847. __func__);
  848. return;
  849. skb_queue_head_init(&free_list);
  850. spin_lock_bh(&np->rx_lock);
  851. for (id = 0; id < NET_RX_RING_SIZE; id++) {
  852. ref = np->grant_rx_ref[id];
  853. if (ref == GRANT_INVALID_REF) {
  854. unused++;
  855. continue;
  856. }
  857. skb = np->rx_skbs[id];
  858. mfn = gnttab_end_foreign_transfer_ref(ref);
  859. gnttab_release_grant_reference(&np->gref_rx_head, ref);
  860. np->grant_rx_ref[id] = GRANT_INVALID_REF;
  861. if (0 == mfn) {
  862. skb_shinfo(skb)->nr_frags = 0;
  863. dev_kfree_skb(skb);
  864. noxfer++;
  865. continue;
  866. }
  867. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  868. /* Remap the page. */
  869. struct page *page = skb_shinfo(skb)->frags[0].page;
  870. unsigned long pfn = page_to_pfn(page);
  871. void *vaddr = page_address(page);
  872. MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
  873. mfn_pte(mfn, PAGE_KERNEL),
  874. 0);
  875. mcl++;
  876. mmu->ptr = ((u64)mfn << PAGE_SHIFT)
  877. | MMU_MACHPHYS_UPDATE;
  878. mmu->val = pfn;
  879. mmu++;
  880. set_phys_to_machine(pfn, mfn);
  881. }
  882. __skb_queue_tail(&free_list, skb);
  883. xfer++;
  884. }
  885. dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
  886. __func__, xfer, noxfer, unused);
  887. if (xfer) {
  888. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  889. /* Do all the remapping work and M2P updates. */
  890. MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
  891. NULL, DOMID_SELF);
  892. mcl++;
  893. HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
  894. }
  895. }
  896. __skb_queue_purge(&free_list);
  897. spin_unlock_bh(&np->rx_lock);
  898. }
  899. static void xennet_uninit(struct net_device *dev)
  900. {
  901. struct netfront_info *np = netdev_priv(dev);
  902. xennet_release_tx_bufs(np);
  903. xennet_release_rx_bufs(np);
  904. gnttab_free_grant_references(np->gref_tx_head);
  905. gnttab_free_grant_references(np->gref_rx_head);
  906. }
  907. static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
  908. {
  909. int i, err;
  910. struct net_device *netdev;
  911. struct netfront_info *np;
  912. netdev = alloc_etherdev(sizeof(struct netfront_info));
  913. if (!netdev) {
  914. printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
  915. __func__);
  916. return ERR_PTR(-ENOMEM);
  917. }
  918. np = netdev_priv(netdev);
  919. np->xbdev = dev;
  920. spin_lock_init(&np->tx_lock);
  921. spin_lock_init(&np->rx_lock);
  922. skb_queue_head_init(&np->rx_batch);
  923. np->rx_target = RX_DFL_MIN_TARGET;
  924. np->rx_min_target = RX_DFL_MIN_TARGET;
  925. np->rx_max_target = RX_MAX_TARGET;
  926. init_timer(&np->rx_refill_timer);
  927. np->rx_refill_timer.data = (unsigned long)netdev;
  928. np->rx_refill_timer.function = rx_refill_timeout;
  929. /* Initialise tx_skbs as a free chain containing every entry. */
  930. np->tx_skb_freelist = 0;
  931. for (i = 0; i < NET_TX_RING_SIZE; i++) {
  932. skb_entry_set_link(&np->tx_skbs[i], i+1);
  933. np->grant_tx_ref[i] = GRANT_INVALID_REF;
  934. }
  935. /* Clear out rx_skbs */
  936. for (i = 0; i < NET_RX_RING_SIZE; i++) {
  937. np->rx_skbs[i] = NULL;
  938. np->grant_rx_ref[i] = GRANT_INVALID_REF;
  939. }
  940. /* A grant for every tx ring slot */
  941. if (gnttab_alloc_grant_references(TX_MAX_TARGET,
  942. &np->gref_tx_head) < 0) {
  943. printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
  944. err = -ENOMEM;
  945. goto exit;
  946. }
  947. /* A grant for every rx ring slot */
  948. if (gnttab_alloc_grant_references(RX_MAX_TARGET,
  949. &np->gref_rx_head) < 0) {
  950. printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
  951. err = -ENOMEM;
  952. goto exit_free_tx;
  953. }
  954. netdev->open = xennet_open;
  955. netdev->hard_start_xmit = xennet_start_xmit;
  956. netdev->stop = xennet_close;
  957. netif_napi_add(netdev, &np->napi, xennet_poll, 64);
  958. netdev->uninit = xennet_uninit;
  959. netdev->change_mtu = xennet_change_mtu;
  960. netdev->features = NETIF_F_IP_CSUM;
  961. SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
  962. SET_NETDEV_DEV(netdev, &dev->dev);
  963. np->netdev = netdev;
  964. netif_carrier_off(netdev);
  965. return netdev;
  966. exit_free_tx:
  967. gnttab_free_grant_references(np->gref_tx_head);
  968. exit:
  969. free_netdev(netdev);
  970. return ERR_PTR(err);
  971. }
  972. /**
  973. * Entry point to this code when a new device is created. Allocate the basic
  974. * structures and the ring buffers for communication with the backend, and
  975. * inform the backend of the appropriate details for those.
  976. */
  977. static int __devinit netfront_probe(struct xenbus_device *dev,
  978. const struct xenbus_device_id *id)
  979. {
  980. int err;
  981. struct net_device *netdev;
  982. struct netfront_info *info;
  983. netdev = xennet_create_dev(dev);
  984. if (IS_ERR(netdev)) {
  985. err = PTR_ERR(netdev);
  986. xenbus_dev_fatal(dev, err, "creating netdev");
  987. return err;
  988. }
  989. info = netdev_priv(netdev);
  990. dev->dev.driver_data = info;
  991. err = register_netdev(info->netdev);
  992. if (err) {
  993. printk(KERN_WARNING "%s: register_netdev err=%d\n",
  994. __func__, err);
  995. goto fail;
  996. }
  997. err = xennet_sysfs_addif(info->netdev);
  998. if (err) {
  999. unregister_netdev(info->netdev);
  1000. printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
  1001. __func__, err);
  1002. goto fail;
  1003. }
  1004. return 0;
  1005. fail:
  1006. free_netdev(netdev);
  1007. dev->dev.driver_data = NULL;
  1008. return err;
  1009. }
  1010. static void xennet_end_access(int ref, void *page)
  1011. {
  1012. /* This frees the page as a side-effect */
  1013. if (ref != GRANT_INVALID_REF)
  1014. gnttab_end_foreign_access(ref, 0, (unsigned long)page);
  1015. }
  1016. static void xennet_disconnect_backend(struct netfront_info *info)
  1017. {
  1018. /* Stop old i/f to prevent errors whilst we rebuild the state. */
  1019. spin_lock_bh(&info->rx_lock);
  1020. spin_lock_irq(&info->tx_lock);
  1021. netif_carrier_off(info->netdev);
  1022. spin_unlock_irq(&info->tx_lock);
  1023. spin_unlock_bh(&info->rx_lock);
  1024. if (info->netdev->irq)
  1025. unbind_from_irqhandler(info->netdev->irq, info->netdev);
  1026. info->evtchn = info->netdev->irq = 0;
  1027. /* End access and free the pages */
  1028. xennet_end_access(info->tx_ring_ref, info->tx.sring);
  1029. xennet_end_access(info->rx_ring_ref, info->rx.sring);
  1030. info->tx_ring_ref = GRANT_INVALID_REF;
  1031. info->rx_ring_ref = GRANT_INVALID_REF;
  1032. info->tx.sring = NULL;
  1033. info->rx.sring = NULL;
  1034. }
  1035. /**
  1036. * We are reconnecting to the backend, due to a suspend/resume, or a backend
  1037. * driver restart. We tear down our netif structure and recreate it, but
  1038. * leave the device-layer structures intact so that this is transparent to the
  1039. * rest of the kernel.
  1040. */
  1041. static int netfront_resume(struct xenbus_device *dev)
  1042. {
  1043. struct netfront_info *info = dev->dev.driver_data;
  1044. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1045. xennet_disconnect_backend(info);
  1046. return 0;
  1047. }
  1048. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  1049. {
  1050. char *s, *e, *macstr;
  1051. int i;
  1052. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  1053. if (IS_ERR(macstr))
  1054. return PTR_ERR(macstr);
  1055. for (i = 0; i < ETH_ALEN; i++) {
  1056. mac[i] = simple_strtoul(s, &e, 16);
  1057. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  1058. kfree(macstr);
  1059. return -ENOENT;
  1060. }
  1061. s = e+1;
  1062. }
  1063. kfree(macstr);
  1064. return 0;
  1065. }
  1066. static irqreturn_t xennet_interrupt(int irq, void *dev_id)
  1067. {
  1068. struct net_device *dev = dev_id;
  1069. struct netfront_info *np = netdev_priv(dev);
  1070. unsigned long flags;
  1071. spin_lock_irqsave(&np->tx_lock, flags);
  1072. if (likely(netif_carrier_ok(dev))) {
  1073. xennet_tx_buf_gc(dev);
  1074. /* Under tx_lock: protects access to rx shared-ring indexes. */
  1075. if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
  1076. netif_rx_schedule(dev, &np->napi);
  1077. }
  1078. spin_unlock_irqrestore(&np->tx_lock, flags);
  1079. return IRQ_HANDLED;
  1080. }
  1081. static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
  1082. {
  1083. struct xen_netif_tx_sring *txs;
  1084. struct xen_netif_rx_sring *rxs;
  1085. int err;
  1086. struct net_device *netdev = info->netdev;
  1087. info->tx_ring_ref = GRANT_INVALID_REF;
  1088. info->rx_ring_ref = GRANT_INVALID_REF;
  1089. info->rx.sring = NULL;
  1090. info->tx.sring = NULL;
  1091. netdev->irq = 0;
  1092. err = xen_net_read_mac(dev, netdev->dev_addr);
  1093. if (err) {
  1094. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  1095. goto fail;
  1096. }
  1097. txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1098. if (!txs) {
  1099. err = -ENOMEM;
  1100. xenbus_dev_fatal(dev, err, "allocating tx ring page");
  1101. goto fail;
  1102. }
  1103. SHARED_RING_INIT(txs);
  1104. FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
  1105. err = xenbus_grant_ring(dev, virt_to_mfn(txs));
  1106. if (err < 0) {
  1107. free_page((unsigned long)txs);
  1108. goto fail;
  1109. }
  1110. info->tx_ring_ref = err;
  1111. rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
  1112. if (!rxs) {
  1113. err = -ENOMEM;
  1114. xenbus_dev_fatal(dev, err, "allocating rx ring page");
  1115. goto fail;
  1116. }
  1117. SHARED_RING_INIT(rxs);
  1118. FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
  1119. err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
  1120. if (err < 0) {
  1121. free_page((unsigned long)rxs);
  1122. goto fail;
  1123. }
  1124. info->rx_ring_ref = err;
  1125. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  1126. if (err)
  1127. goto fail;
  1128. err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
  1129. IRQF_SAMPLE_RANDOM, netdev->name,
  1130. netdev);
  1131. if (err < 0)
  1132. goto fail;
  1133. netdev->irq = err;
  1134. return 0;
  1135. fail:
  1136. return err;
  1137. }
  1138. /* Common code used when first setting up, and when resuming. */
  1139. static int talk_to_backend(struct xenbus_device *dev,
  1140. struct netfront_info *info)
  1141. {
  1142. const char *message;
  1143. struct xenbus_transaction xbt;
  1144. int err;
  1145. /* Create shared ring, alloc event channel. */
  1146. err = setup_netfront(dev, info);
  1147. if (err)
  1148. goto out;
  1149. again:
  1150. err = xenbus_transaction_start(&xbt);
  1151. if (err) {
  1152. xenbus_dev_fatal(dev, err, "starting transaction");
  1153. goto destroy_ring;
  1154. }
  1155. err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
  1156. info->tx_ring_ref);
  1157. if (err) {
  1158. message = "writing tx ring-ref";
  1159. goto abort_transaction;
  1160. }
  1161. err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
  1162. info->rx_ring_ref);
  1163. if (err) {
  1164. message = "writing rx ring-ref";
  1165. goto abort_transaction;
  1166. }
  1167. err = xenbus_printf(xbt, dev->nodename,
  1168. "event-channel", "%u", info->evtchn);
  1169. if (err) {
  1170. message = "writing event-channel";
  1171. goto abort_transaction;
  1172. }
  1173. err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
  1174. 1);
  1175. if (err) {
  1176. message = "writing request-rx-copy";
  1177. goto abort_transaction;
  1178. }
  1179. err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
  1180. if (err) {
  1181. message = "writing feature-rx-notify";
  1182. goto abort_transaction;
  1183. }
  1184. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
  1185. if (err) {
  1186. message = "writing feature-sg";
  1187. goto abort_transaction;
  1188. }
  1189. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
  1190. if (err) {
  1191. message = "writing feature-gso-tcpv4";
  1192. goto abort_transaction;
  1193. }
  1194. err = xenbus_transaction_end(xbt, 0);
  1195. if (err) {
  1196. if (err == -EAGAIN)
  1197. goto again;
  1198. xenbus_dev_fatal(dev, err, "completing transaction");
  1199. goto destroy_ring;
  1200. }
  1201. return 0;
  1202. abort_transaction:
  1203. xenbus_transaction_end(xbt, 1);
  1204. xenbus_dev_fatal(dev, err, "%s", message);
  1205. destroy_ring:
  1206. xennet_disconnect_backend(info);
  1207. out:
  1208. return err;
  1209. }
  1210. static int xennet_set_sg(struct net_device *dev, u32 data)
  1211. {
  1212. if (data) {
  1213. struct netfront_info *np = netdev_priv(dev);
  1214. int val;
  1215. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
  1216. "%d", &val) < 0)
  1217. val = 0;
  1218. if (!val)
  1219. return -ENOSYS;
  1220. } else if (dev->mtu > ETH_DATA_LEN)
  1221. dev->mtu = ETH_DATA_LEN;
  1222. return ethtool_op_set_sg(dev, data);
  1223. }
  1224. static int xennet_set_tso(struct net_device *dev, u32 data)
  1225. {
  1226. if (data) {
  1227. struct netfront_info *np = netdev_priv(dev);
  1228. int val;
  1229. if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1230. "feature-gso-tcpv4", "%d", &val) < 0)
  1231. val = 0;
  1232. if (!val)
  1233. return -ENOSYS;
  1234. }
  1235. return ethtool_op_set_tso(dev, data);
  1236. }
  1237. static void xennet_set_features(struct net_device *dev)
  1238. {
  1239. /* Turn off all GSO bits except ROBUST. */
  1240. dev->features &= (1 << NETIF_F_GSO_SHIFT) - 1;
  1241. dev->features |= NETIF_F_GSO_ROBUST;
  1242. xennet_set_sg(dev, 0);
  1243. /* We need checksum offload to enable scatter/gather and TSO. */
  1244. if (!(dev->features & NETIF_F_IP_CSUM))
  1245. return;
  1246. if (!xennet_set_sg(dev, 1))
  1247. xennet_set_tso(dev, 1);
  1248. }
  1249. static int xennet_connect(struct net_device *dev)
  1250. {
  1251. struct netfront_info *np = netdev_priv(dev);
  1252. int i, requeue_idx, err;
  1253. struct sk_buff *skb;
  1254. grant_ref_t ref;
  1255. struct xen_netif_rx_request *req;
  1256. unsigned int feature_rx_copy;
  1257. err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
  1258. "feature-rx-copy", "%u", &feature_rx_copy);
  1259. if (err != 1)
  1260. feature_rx_copy = 0;
  1261. if (!feature_rx_copy) {
  1262. dev_info(&dev->dev,
  1263. "backend does not support copying receive path\n");
  1264. return -ENODEV;
  1265. }
  1266. err = talk_to_backend(np->xbdev, np);
  1267. if (err)
  1268. return err;
  1269. xennet_set_features(dev);
  1270. spin_lock_bh(&np->rx_lock);
  1271. spin_lock_irq(&np->tx_lock);
  1272. /* Step 1: Discard all pending TX packet fragments. */
  1273. xennet_release_tx_bufs(np);
  1274. /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
  1275. for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
  1276. if (!np->rx_skbs[i])
  1277. continue;
  1278. skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
  1279. ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
  1280. req = RING_GET_REQUEST(&np->rx, requeue_idx);
  1281. gnttab_grant_foreign_access_ref(
  1282. ref, np->xbdev->otherend_id,
  1283. pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
  1284. frags->page)),
  1285. 0);
  1286. req->gref = ref;
  1287. req->id = requeue_idx;
  1288. requeue_idx++;
  1289. }
  1290. np->rx.req_prod_pvt = requeue_idx;
  1291. /*
  1292. * Step 3: All public and private state should now be sane. Get
  1293. * ready to start sending and receiving packets and give the driver
  1294. * domain a kick because we've probably just requeued some
  1295. * packets.
  1296. */
  1297. netif_carrier_on(np->netdev);
  1298. notify_remote_via_irq(np->netdev->irq);
  1299. xennet_tx_buf_gc(dev);
  1300. xennet_alloc_rx_buffers(dev);
  1301. spin_unlock_irq(&np->tx_lock);
  1302. spin_unlock_bh(&np->rx_lock);
  1303. return 0;
  1304. }
  1305. /**
  1306. * Callback received when the backend's state changes.
  1307. */
  1308. static void backend_changed(struct xenbus_device *dev,
  1309. enum xenbus_state backend_state)
  1310. {
  1311. struct netfront_info *np = dev->dev.driver_data;
  1312. struct net_device *netdev = np->netdev;
  1313. dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
  1314. switch (backend_state) {
  1315. case XenbusStateInitialising:
  1316. case XenbusStateInitialised:
  1317. case XenbusStateConnected:
  1318. case XenbusStateUnknown:
  1319. case XenbusStateClosed:
  1320. break;
  1321. case XenbusStateInitWait:
  1322. if (dev->state != XenbusStateInitialising)
  1323. break;
  1324. if (xennet_connect(netdev) != 0)
  1325. break;
  1326. xenbus_switch_state(dev, XenbusStateConnected);
  1327. break;
  1328. case XenbusStateClosing:
  1329. xenbus_frontend_closed(dev);
  1330. break;
  1331. }
  1332. }
  1333. static struct ethtool_ops xennet_ethtool_ops =
  1334. {
  1335. .set_tx_csum = ethtool_op_set_tx_csum,
  1336. .set_sg = xennet_set_sg,
  1337. .set_tso = xennet_set_tso,
  1338. .get_link = ethtool_op_get_link,
  1339. };
  1340. #ifdef CONFIG_SYSFS
  1341. static ssize_t show_rxbuf_min(struct device *dev,
  1342. struct device_attribute *attr, char *buf)
  1343. {
  1344. struct net_device *netdev = to_net_dev(dev);
  1345. struct netfront_info *info = netdev_priv(netdev);
  1346. return sprintf(buf, "%u\n", info->rx_min_target);
  1347. }
  1348. static ssize_t store_rxbuf_min(struct device *dev,
  1349. struct device_attribute *attr,
  1350. const char *buf, size_t len)
  1351. {
  1352. struct net_device *netdev = to_net_dev(dev);
  1353. struct netfront_info *np = netdev_priv(netdev);
  1354. char *endp;
  1355. unsigned long target;
  1356. if (!capable(CAP_NET_ADMIN))
  1357. return -EPERM;
  1358. target = simple_strtoul(buf, &endp, 0);
  1359. if (endp == buf)
  1360. return -EBADMSG;
  1361. if (target < RX_MIN_TARGET)
  1362. target = RX_MIN_TARGET;
  1363. if (target > RX_MAX_TARGET)
  1364. target = RX_MAX_TARGET;
  1365. spin_lock_bh(&np->rx_lock);
  1366. if (target > np->rx_max_target)
  1367. np->rx_max_target = target;
  1368. np->rx_min_target = target;
  1369. if (target > np->rx_target)
  1370. np->rx_target = target;
  1371. xennet_alloc_rx_buffers(netdev);
  1372. spin_unlock_bh(&np->rx_lock);
  1373. return len;
  1374. }
  1375. static ssize_t show_rxbuf_max(struct device *dev,
  1376. struct device_attribute *attr, char *buf)
  1377. {
  1378. struct net_device *netdev = to_net_dev(dev);
  1379. struct netfront_info *info = netdev_priv(netdev);
  1380. return sprintf(buf, "%u\n", info->rx_max_target);
  1381. }
  1382. static ssize_t store_rxbuf_max(struct device *dev,
  1383. struct device_attribute *attr,
  1384. const char *buf, size_t len)
  1385. {
  1386. struct net_device *netdev = to_net_dev(dev);
  1387. struct netfront_info *np = netdev_priv(netdev);
  1388. char *endp;
  1389. unsigned long target;
  1390. if (!capable(CAP_NET_ADMIN))
  1391. return -EPERM;
  1392. target = simple_strtoul(buf, &endp, 0);
  1393. if (endp == buf)
  1394. return -EBADMSG;
  1395. if (target < RX_MIN_TARGET)
  1396. target = RX_MIN_TARGET;
  1397. if (target > RX_MAX_TARGET)
  1398. target = RX_MAX_TARGET;
  1399. spin_lock_bh(&np->rx_lock);
  1400. if (target < np->rx_min_target)
  1401. np->rx_min_target = target;
  1402. np->rx_max_target = target;
  1403. if (target < np->rx_target)
  1404. np->rx_target = target;
  1405. xennet_alloc_rx_buffers(netdev);
  1406. spin_unlock_bh(&np->rx_lock);
  1407. return len;
  1408. }
  1409. static ssize_t show_rxbuf_cur(struct device *dev,
  1410. struct device_attribute *attr, char *buf)
  1411. {
  1412. struct net_device *netdev = to_net_dev(dev);
  1413. struct netfront_info *info = netdev_priv(netdev);
  1414. return sprintf(buf, "%u\n", info->rx_target);
  1415. }
  1416. static struct device_attribute xennet_attrs[] = {
  1417. __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
  1418. __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
  1419. __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
  1420. };
  1421. static int xennet_sysfs_addif(struct net_device *netdev)
  1422. {
  1423. int i;
  1424. int err;
  1425. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
  1426. err = device_create_file(&netdev->dev,
  1427. &xennet_attrs[i]);
  1428. if (err)
  1429. goto fail;
  1430. }
  1431. return 0;
  1432. fail:
  1433. while (--i >= 0)
  1434. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1435. return err;
  1436. }
  1437. static void xennet_sysfs_delif(struct net_device *netdev)
  1438. {
  1439. int i;
  1440. for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
  1441. device_remove_file(&netdev->dev, &xennet_attrs[i]);
  1442. }
  1443. #endif /* CONFIG_SYSFS */
  1444. static struct xenbus_device_id netfront_ids[] = {
  1445. { "vif" },
  1446. { "" }
  1447. };
  1448. static int __devexit xennet_remove(struct xenbus_device *dev)
  1449. {
  1450. struct netfront_info *info = dev->dev.driver_data;
  1451. dev_dbg(&dev->dev, "%s\n", dev->nodename);
  1452. unregister_netdev(info->netdev);
  1453. xennet_disconnect_backend(info);
  1454. del_timer_sync(&info->rx_refill_timer);
  1455. xennet_sysfs_delif(info->netdev);
  1456. free_netdev(info->netdev);
  1457. return 0;
  1458. }
  1459. static struct xenbus_driver netfront = {
  1460. .name = "vif",
  1461. .owner = THIS_MODULE,
  1462. .ids = netfront_ids,
  1463. .probe = netfront_probe,
  1464. .remove = __devexit_p(xennet_remove),
  1465. .resume = netfront_resume,
  1466. .otherend_changed = backend_changed,
  1467. };
  1468. static int __init netif_init(void)
  1469. {
  1470. if (!xen_domain())
  1471. return -ENODEV;
  1472. if (xen_initial_domain())
  1473. return 0;
  1474. printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
  1475. return xenbus_register_frontend(&netfront);
  1476. }
  1477. module_init(netif_init);
  1478. static void __exit netif_exit(void)
  1479. {
  1480. if (xen_initial_domain())
  1481. return;
  1482. xenbus_unregister_driver(&netfront);
  1483. }
  1484. module_exit(netif_exit);
  1485. MODULE_DESCRIPTION("Xen virtual network device frontend");
  1486. MODULE_LICENSE("GPL");
  1487. MODULE_ALIAS("xen:vif");
  1488. MODULE_ALIAS("xennet");