xen-netfront.c 44 KB

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