netvsc_drv.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Authors:
  17. * Haiyang Zhang <haiyangz@microsoft.com>
  18. * Hank Janssen <hjanssen@microsoft.com>
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/init.h>
  22. #include <linux/atomic.h>
  23. #include <linux/module.h>
  24. #include <linux/highmem.h>
  25. #include <linux/device.h>
  26. #include <linux/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/in.h>
  34. #include <linux/slab.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/netpoll.h>
  37. #include <net/arp.h>
  38. #include <net/route.h>
  39. #include <net/sock.h>
  40. #include <net/pkt_sched.h>
  41. #include <net/checksum.h>
  42. #include <net/ip6_checksum.h>
  43. #include <net/failover.h>
  44. #include "hyperv_net.h"
  45. #define RING_SIZE_MIN 64
  46. #define RETRY_US_LO 5000
  47. #define RETRY_US_HI 10000
  48. #define RETRY_MAX 2000 /* >10 sec */
  49. #define LINKCHANGE_INT (2 * HZ)
  50. #define VF_TAKEOVER_INT (HZ / 10)
  51. static unsigned int ring_size __ro_after_init = 128;
  52. module_param(ring_size, uint, 0444);
  53. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  54. unsigned int netvsc_ring_bytes __ro_after_init;
  55. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  56. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  57. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  58. NETIF_MSG_TX_ERR;
  59. static int debug = -1;
  60. module_param(debug, int, 0444);
  61. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  62. static void netvsc_change_rx_flags(struct net_device *net, int change)
  63. {
  64. struct net_device_context *ndev_ctx = netdev_priv(net);
  65. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  66. int inc;
  67. if (!vf_netdev)
  68. return;
  69. if (change & IFF_PROMISC) {
  70. inc = (net->flags & IFF_PROMISC) ? 1 : -1;
  71. dev_set_promiscuity(vf_netdev, inc);
  72. }
  73. if (change & IFF_ALLMULTI) {
  74. inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
  75. dev_set_allmulti(vf_netdev, inc);
  76. }
  77. }
  78. static void netvsc_set_rx_mode(struct net_device *net)
  79. {
  80. struct net_device_context *ndev_ctx = netdev_priv(net);
  81. struct net_device *vf_netdev;
  82. struct netvsc_device *nvdev;
  83. rcu_read_lock();
  84. vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
  85. if (vf_netdev) {
  86. dev_uc_sync(vf_netdev, net);
  87. dev_mc_sync(vf_netdev, net);
  88. }
  89. nvdev = rcu_dereference(ndev_ctx->nvdev);
  90. if (nvdev)
  91. rndis_filter_update(nvdev);
  92. rcu_read_unlock();
  93. }
  94. static int netvsc_open(struct net_device *net)
  95. {
  96. struct net_device_context *ndev_ctx = netdev_priv(net);
  97. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  98. struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
  99. struct rndis_device *rdev;
  100. int ret = 0;
  101. netif_carrier_off(net);
  102. /* Open up the device */
  103. ret = rndis_filter_open(nvdev);
  104. if (ret != 0) {
  105. netdev_err(net, "unable to open device (ret %d).\n", ret);
  106. return ret;
  107. }
  108. rdev = nvdev->extension;
  109. if (!rdev->link_state)
  110. netif_carrier_on(net);
  111. if (vf_netdev) {
  112. /* Setting synthetic device up transparently sets
  113. * slave as up. If open fails, then slave will be
  114. * still be offline (and not used).
  115. */
  116. ret = dev_open(vf_netdev);
  117. if (ret)
  118. netdev_warn(net,
  119. "unable to open slave: %s: %d\n",
  120. vf_netdev->name, ret);
  121. }
  122. return 0;
  123. }
  124. static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
  125. {
  126. unsigned int retry = 0;
  127. int i;
  128. /* Ensure pending bytes in ring are read */
  129. for (;;) {
  130. u32 aread = 0;
  131. for (i = 0; i < nvdev->num_chn; i++) {
  132. struct vmbus_channel *chn
  133. = nvdev->chan_table[i].channel;
  134. if (!chn)
  135. continue;
  136. /* make sure receive not running now */
  137. napi_synchronize(&nvdev->chan_table[i].napi);
  138. aread = hv_get_bytes_to_read(&chn->inbound);
  139. if (aread)
  140. break;
  141. aread = hv_get_bytes_to_read(&chn->outbound);
  142. if (aread)
  143. break;
  144. }
  145. if (aread == 0)
  146. return 0;
  147. if (++retry > RETRY_MAX)
  148. return -ETIMEDOUT;
  149. usleep_range(RETRY_US_LO, RETRY_US_HI);
  150. }
  151. }
  152. static int netvsc_close(struct net_device *net)
  153. {
  154. struct net_device_context *net_device_ctx = netdev_priv(net);
  155. struct net_device *vf_netdev
  156. = rtnl_dereference(net_device_ctx->vf_netdev);
  157. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  158. int ret;
  159. netif_tx_disable(net);
  160. /* No need to close rndis filter if it is removed already */
  161. if (!nvdev)
  162. return 0;
  163. ret = rndis_filter_close(nvdev);
  164. if (ret != 0) {
  165. netdev_err(net, "unable to close device (ret %d).\n", ret);
  166. return ret;
  167. }
  168. ret = netvsc_wait_until_empty(nvdev);
  169. if (ret)
  170. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  171. if (vf_netdev)
  172. dev_close(vf_netdev);
  173. return ret;
  174. }
  175. static inline void *init_ppi_data(struct rndis_message *msg,
  176. u32 ppi_size, u32 pkt_type)
  177. {
  178. struct rndis_packet *rndis_pkt = &msg->msg.pkt;
  179. struct rndis_per_packet_info *ppi;
  180. rndis_pkt->data_offset += ppi_size;
  181. ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
  182. + rndis_pkt->per_pkt_info_len;
  183. ppi->size = ppi_size;
  184. ppi->type = pkt_type;
  185. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  186. rndis_pkt->per_pkt_info_len += ppi_size;
  187. return ppi + 1;
  188. }
  189. /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
  190. * packets. We can use ethtool to change UDP hash level when necessary.
  191. */
  192. static inline u32 netvsc_get_hash(
  193. struct sk_buff *skb,
  194. const struct net_device_context *ndc)
  195. {
  196. struct flow_keys flow;
  197. u32 hash, pkt_proto = 0;
  198. static u32 hashrnd __read_mostly;
  199. net_get_random_once(&hashrnd, sizeof(hashrnd));
  200. if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
  201. return 0;
  202. switch (flow.basic.ip_proto) {
  203. case IPPROTO_TCP:
  204. if (flow.basic.n_proto == htons(ETH_P_IP))
  205. pkt_proto = HV_TCP4_L4HASH;
  206. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  207. pkt_proto = HV_TCP6_L4HASH;
  208. break;
  209. case IPPROTO_UDP:
  210. if (flow.basic.n_proto == htons(ETH_P_IP))
  211. pkt_proto = HV_UDP4_L4HASH;
  212. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  213. pkt_proto = HV_UDP6_L4HASH;
  214. break;
  215. }
  216. if (pkt_proto & ndc->l4_hash) {
  217. return skb_get_hash(skb);
  218. } else {
  219. if (flow.basic.n_proto == htons(ETH_P_IP))
  220. hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
  221. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  222. hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
  223. else
  224. hash = 0;
  225. skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
  226. }
  227. return hash;
  228. }
  229. static inline int netvsc_get_tx_queue(struct net_device *ndev,
  230. struct sk_buff *skb, int old_idx)
  231. {
  232. const struct net_device_context *ndc = netdev_priv(ndev);
  233. struct sock *sk = skb->sk;
  234. int q_idx;
  235. q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
  236. (VRSS_SEND_TAB_SIZE - 1)];
  237. /* If queue index changed record the new value */
  238. if (q_idx != old_idx &&
  239. sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
  240. sk_tx_queue_set(sk, q_idx);
  241. return q_idx;
  242. }
  243. /*
  244. * Select queue for transmit.
  245. *
  246. * If a valid queue has already been assigned, then use that.
  247. * Otherwise compute tx queue based on hash and the send table.
  248. *
  249. * This is basically similar to default (__netdev_pick_tx) with the added step
  250. * of using the host send_table when no other queue has been assigned.
  251. *
  252. * TODO support XPS - but get_xps_queue not exported
  253. */
  254. static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
  255. {
  256. int q_idx = sk_tx_queue_get(skb->sk);
  257. if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
  258. /* If forwarding a packet, we use the recorded queue when
  259. * available for better cache locality.
  260. */
  261. if (skb_rx_queue_recorded(skb))
  262. q_idx = skb_get_rx_queue(skb);
  263. else
  264. q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
  265. }
  266. return q_idx;
  267. }
  268. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  269. void *accel_priv,
  270. select_queue_fallback_t fallback)
  271. {
  272. struct net_device_context *ndc = netdev_priv(ndev);
  273. struct net_device *vf_netdev;
  274. u16 txq;
  275. rcu_read_lock();
  276. vf_netdev = rcu_dereference(ndc->vf_netdev);
  277. if (vf_netdev) {
  278. const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
  279. if (vf_ops->ndo_select_queue)
  280. txq = vf_ops->ndo_select_queue(vf_netdev, skb,
  281. accel_priv, fallback);
  282. else
  283. txq = fallback(vf_netdev, skb);
  284. /* Record the queue selected by VF so that it can be
  285. * used for common case where VF has more queues than
  286. * the synthetic device.
  287. */
  288. qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
  289. } else {
  290. txq = netvsc_pick_tx(ndev, skb);
  291. }
  292. rcu_read_unlock();
  293. while (unlikely(txq >= ndev->real_num_tx_queues))
  294. txq -= ndev->real_num_tx_queues;
  295. return txq;
  296. }
  297. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  298. struct hv_page_buffer *pb)
  299. {
  300. int j = 0;
  301. /* Deal with compund pages by ignoring unused part
  302. * of the page.
  303. */
  304. page += (offset >> PAGE_SHIFT);
  305. offset &= ~PAGE_MASK;
  306. while (len > 0) {
  307. unsigned long bytes;
  308. bytes = PAGE_SIZE - offset;
  309. if (bytes > len)
  310. bytes = len;
  311. pb[j].pfn = page_to_pfn(page);
  312. pb[j].offset = offset;
  313. pb[j].len = bytes;
  314. offset += bytes;
  315. len -= bytes;
  316. if (offset == PAGE_SIZE && len) {
  317. page++;
  318. offset = 0;
  319. j++;
  320. }
  321. }
  322. return j + 1;
  323. }
  324. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  325. struct hv_netvsc_packet *packet,
  326. struct hv_page_buffer *pb)
  327. {
  328. u32 slots_used = 0;
  329. char *data = skb->data;
  330. int frags = skb_shinfo(skb)->nr_frags;
  331. int i;
  332. /* The packet is laid out thus:
  333. * 1. hdr: RNDIS header and PPI
  334. * 2. skb linear data
  335. * 3. skb fragment data
  336. */
  337. slots_used += fill_pg_buf(virt_to_page(hdr),
  338. offset_in_page(hdr),
  339. len, &pb[slots_used]);
  340. packet->rmsg_size = len;
  341. packet->rmsg_pgcnt = slots_used;
  342. slots_used += fill_pg_buf(virt_to_page(data),
  343. offset_in_page(data),
  344. skb_headlen(skb), &pb[slots_used]);
  345. for (i = 0; i < frags; i++) {
  346. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  347. slots_used += fill_pg_buf(skb_frag_page(frag),
  348. frag->page_offset,
  349. skb_frag_size(frag), &pb[slots_used]);
  350. }
  351. return slots_used;
  352. }
  353. static int count_skb_frag_slots(struct sk_buff *skb)
  354. {
  355. int i, frags = skb_shinfo(skb)->nr_frags;
  356. int pages = 0;
  357. for (i = 0; i < frags; i++) {
  358. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  359. unsigned long size = skb_frag_size(frag);
  360. unsigned long offset = frag->page_offset;
  361. /* Skip unused frames from start of page */
  362. offset &= ~PAGE_MASK;
  363. pages += PFN_UP(offset + size);
  364. }
  365. return pages;
  366. }
  367. static int netvsc_get_slots(struct sk_buff *skb)
  368. {
  369. char *data = skb->data;
  370. unsigned int offset = offset_in_page(data);
  371. unsigned int len = skb_headlen(skb);
  372. int slots;
  373. int frag_slots;
  374. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  375. frag_slots = count_skb_frag_slots(skb);
  376. return slots + frag_slots;
  377. }
  378. static u32 net_checksum_info(struct sk_buff *skb)
  379. {
  380. if (skb->protocol == htons(ETH_P_IP)) {
  381. struct iphdr *ip = ip_hdr(skb);
  382. if (ip->protocol == IPPROTO_TCP)
  383. return TRANSPORT_INFO_IPV4_TCP;
  384. else if (ip->protocol == IPPROTO_UDP)
  385. return TRANSPORT_INFO_IPV4_UDP;
  386. } else {
  387. struct ipv6hdr *ip6 = ipv6_hdr(skb);
  388. if (ip6->nexthdr == IPPROTO_TCP)
  389. return TRANSPORT_INFO_IPV6_TCP;
  390. else if (ip6->nexthdr == IPPROTO_UDP)
  391. return TRANSPORT_INFO_IPV6_UDP;
  392. }
  393. return TRANSPORT_INFO_NOT_IP;
  394. }
  395. /* Send skb on the slave VF device. */
  396. static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
  397. struct sk_buff *skb)
  398. {
  399. struct net_device_context *ndev_ctx = netdev_priv(net);
  400. unsigned int len = skb->len;
  401. int rc;
  402. skb->dev = vf_netdev;
  403. skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
  404. rc = dev_queue_xmit(skb);
  405. if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
  406. struct netvsc_vf_pcpu_stats *pcpu_stats
  407. = this_cpu_ptr(ndev_ctx->vf_stats);
  408. u64_stats_update_begin(&pcpu_stats->syncp);
  409. pcpu_stats->tx_packets++;
  410. pcpu_stats->tx_bytes += len;
  411. u64_stats_update_end(&pcpu_stats->syncp);
  412. } else {
  413. this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
  414. }
  415. return rc;
  416. }
  417. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  418. {
  419. struct net_device_context *net_device_ctx = netdev_priv(net);
  420. struct hv_netvsc_packet *packet = NULL;
  421. int ret;
  422. unsigned int num_data_pgs;
  423. struct rndis_message *rndis_msg;
  424. struct net_device *vf_netdev;
  425. u32 rndis_msg_size;
  426. u32 hash;
  427. struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
  428. /* if VF is present and up then redirect packets
  429. * already called with rcu_read_lock_bh
  430. */
  431. vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
  432. if (vf_netdev && netif_running(vf_netdev) &&
  433. !netpoll_tx_running(net))
  434. return netvsc_vf_xmit(net, vf_netdev, skb);
  435. /* We will atmost need two pages to describe the rndis
  436. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  437. * of pages in a single packet. If skb is scattered around
  438. * more pages we try linearizing it.
  439. */
  440. num_data_pgs = netvsc_get_slots(skb) + 2;
  441. if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
  442. ++net_device_ctx->eth_stats.tx_scattered;
  443. if (skb_linearize(skb))
  444. goto no_memory;
  445. num_data_pgs = netvsc_get_slots(skb) + 2;
  446. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  447. ++net_device_ctx->eth_stats.tx_too_big;
  448. goto drop;
  449. }
  450. }
  451. /*
  452. * Place the rndis header in the skb head room and
  453. * the skb->cb will be used for hv_netvsc_packet
  454. * structure.
  455. */
  456. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  457. if (ret)
  458. goto no_memory;
  459. /* Use the skb control buffer for building up the packet */
  460. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  461. FIELD_SIZEOF(struct sk_buff, cb));
  462. packet = (struct hv_netvsc_packet *)skb->cb;
  463. packet->q_idx = skb_get_queue_mapping(skb);
  464. packet->total_data_buflen = skb->len;
  465. packet->total_bytes = skb->len;
  466. packet->total_packets = 1;
  467. rndis_msg = (struct rndis_message *)skb->head;
  468. /* Add the rndis header */
  469. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  470. rndis_msg->msg_len = packet->total_data_buflen;
  471. rndis_msg->msg.pkt = (struct rndis_packet) {
  472. .data_offset = sizeof(struct rndis_packet),
  473. .data_len = packet->total_data_buflen,
  474. .per_pkt_info_offset = sizeof(struct rndis_packet),
  475. };
  476. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  477. hash = skb_get_hash_raw(skb);
  478. if (hash != 0 && net->real_num_tx_queues > 1) {
  479. u32 *hash_info;
  480. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  481. hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  482. NBL_HASH_VALUE);
  483. *hash_info = hash;
  484. }
  485. if (skb_vlan_tag_present(skb)) {
  486. struct ndis_pkt_8021q_info *vlan;
  487. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  488. vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  489. IEEE_8021Q_INFO);
  490. vlan->value = 0;
  491. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  492. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  493. VLAN_PRIO_SHIFT;
  494. }
  495. if (skb_is_gso(skb)) {
  496. struct ndis_tcp_lso_info *lso_info;
  497. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  498. lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  499. TCP_LARGESEND_PKTINFO);
  500. lso_info->value = 0;
  501. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  502. if (skb->protocol == htons(ETH_P_IP)) {
  503. lso_info->lso_v2_transmit.ip_version =
  504. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  505. ip_hdr(skb)->tot_len = 0;
  506. ip_hdr(skb)->check = 0;
  507. tcp_hdr(skb)->check =
  508. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  509. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  510. } else {
  511. lso_info->lso_v2_transmit.ip_version =
  512. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  513. ipv6_hdr(skb)->payload_len = 0;
  514. tcp_hdr(skb)->check =
  515. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  516. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  517. }
  518. lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
  519. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  520. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  521. if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
  522. struct ndis_tcp_ip_checksum_info *csum_info;
  523. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  524. csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  525. TCPIP_CHKSUM_PKTINFO);
  526. csum_info->value = 0;
  527. csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
  528. if (skb->protocol == htons(ETH_P_IP)) {
  529. csum_info->transmit.is_ipv4 = 1;
  530. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  531. csum_info->transmit.tcp_checksum = 1;
  532. else
  533. csum_info->transmit.udp_checksum = 1;
  534. } else {
  535. csum_info->transmit.is_ipv6 = 1;
  536. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  537. csum_info->transmit.tcp_checksum = 1;
  538. else
  539. csum_info->transmit.udp_checksum = 1;
  540. }
  541. } else {
  542. /* Can't do offload of this type of checksum */
  543. if (skb_checksum_help(skb))
  544. goto drop;
  545. }
  546. }
  547. /* Start filling in the page buffers with the rndis hdr */
  548. rndis_msg->msg_len += rndis_msg_size;
  549. packet->total_data_buflen = rndis_msg->msg_len;
  550. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  551. skb, packet, pb);
  552. /* timestamp packet in software */
  553. skb_tx_timestamp(skb);
  554. ret = netvsc_send(net, packet, rndis_msg, pb, skb);
  555. if (likely(ret == 0))
  556. return NETDEV_TX_OK;
  557. if (ret == -EAGAIN) {
  558. ++net_device_ctx->eth_stats.tx_busy;
  559. return NETDEV_TX_BUSY;
  560. }
  561. if (ret == -ENOSPC)
  562. ++net_device_ctx->eth_stats.tx_no_space;
  563. drop:
  564. dev_kfree_skb_any(skb);
  565. net->stats.tx_dropped++;
  566. return NETDEV_TX_OK;
  567. no_memory:
  568. ++net_device_ctx->eth_stats.tx_no_memory;
  569. goto drop;
  570. }
  571. /*
  572. * netvsc_linkstatus_callback - Link up/down notification
  573. */
  574. void netvsc_linkstatus_callback(struct net_device *net,
  575. struct rndis_message *resp)
  576. {
  577. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  578. struct net_device_context *ndev_ctx = netdev_priv(net);
  579. struct netvsc_reconfig *event;
  580. unsigned long flags;
  581. /* Update the physical link speed when changing to another vSwitch */
  582. if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
  583. u32 speed;
  584. speed = *(u32 *)((void *)indicate
  585. + indicate->status_buf_offset) / 10000;
  586. ndev_ctx->speed = speed;
  587. return;
  588. }
  589. /* Handle these link change statuses below */
  590. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  591. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  592. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  593. return;
  594. if (net->reg_state != NETREG_REGISTERED)
  595. return;
  596. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  597. if (!event)
  598. return;
  599. event->event = indicate->status;
  600. spin_lock_irqsave(&ndev_ctx->lock, flags);
  601. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  602. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  603. schedule_delayed_work(&ndev_ctx->dwork, 0);
  604. }
  605. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  606. struct napi_struct *napi,
  607. const struct ndis_tcp_ip_checksum_info *csum_info,
  608. const struct ndis_pkt_8021q_info *vlan,
  609. void *data, u32 buflen)
  610. {
  611. struct sk_buff *skb;
  612. skb = napi_alloc_skb(napi, buflen);
  613. if (!skb)
  614. return skb;
  615. /*
  616. * Copy to skb. This copy is needed here since the memory pointed by
  617. * hv_netvsc_packet cannot be deallocated
  618. */
  619. skb_put_data(skb, data, buflen);
  620. skb->protocol = eth_type_trans(skb, net);
  621. /* skb is already created with CHECKSUM_NONE */
  622. skb_checksum_none_assert(skb);
  623. /*
  624. * In Linux, the IP checksum is always checked.
  625. * Do L4 checksum offload if enabled and present.
  626. */
  627. if (csum_info && (net->features & NETIF_F_RXCSUM)) {
  628. if (csum_info->receive.tcp_checksum_succeeded ||
  629. csum_info->receive.udp_checksum_succeeded)
  630. skb->ip_summed = CHECKSUM_UNNECESSARY;
  631. }
  632. if (vlan) {
  633. u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
  634. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  635. vlan_tci);
  636. }
  637. return skb;
  638. }
  639. /*
  640. * netvsc_recv_callback - Callback when we receive a packet from the
  641. * "wire" on the specified device.
  642. */
  643. int netvsc_recv_callback(struct net_device *net,
  644. struct netvsc_device *net_device,
  645. struct vmbus_channel *channel,
  646. void *data, u32 len,
  647. const struct ndis_tcp_ip_checksum_info *csum_info,
  648. const struct ndis_pkt_8021q_info *vlan)
  649. {
  650. struct net_device_context *net_device_ctx = netdev_priv(net);
  651. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  652. struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
  653. struct sk_buff *skb;
  654. struct netvsc_stats *rx_stats;
  655. if (net->reg_state != NETREG_REGISTERED)
  656. return NVSP_STAT_FAIL;
  657. /* Allocate a skb - TODO direct I/O to pages? */
  658. skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
  659. csum_info, vlan, data, len);
  660. if (unlikely(!skb)) {
  661. ++net_device_ctx->eth_stats.rx_no_memory;
  662. rcu_read_unlock();
  663. return NVSP_STAT_FAIL;
  664. }
  665. skb_record_rx_queue(skb, q_idx);
  666. /*
  667. * Even if injecting the packet, record the statistics
  668. * on the synthetic device because modifying the VF device
  669. * statistics will not work correctly.
  670. */
  671. rx_stats = &nvchan->rx_stats;
  672. u64_stats_update_begin(&rx_stats->syncp);
  673. rx_stats->packets++;
  674. rx_stats->bytes += len;
  675. if (skb->pkt_type == PACKET_BROADCAST)
  676. ++rx_stats->broadcast;
  677. else if (skb->pkt_type == PACKET_MULTICAST)
  678. ++rx_stats->multicast;
  679. u64_stats_update_end(&rx_stats->syncp);
  680. napi_gro_receive(&nvchan->napi, skb);
  681. return NVSP_STAT_SUCCESS;
  682. }
  683. static void netvsc_get_drvinfo(struct net_device *net,
  684. struct ethtool_drvinfo *info)
  685. {
  686. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  687. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  688. }
  689. static void netvsc_get_channels(struct net_device *net,
  690. struct ethtool_channels *channel)
  691. {
  692. struct net_device_context *net_device_ctx = netdev_priv(net);
  693. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  694. if (nvdev) {
  695. channel->max_combined = nvdev->max_chn;
  696. channel->combined_count = nvdev->num_chn;
  697. }
  698. }
  699. static int netvsc_detach(struct net_device *ndev,
  700. struct netvsc_device *nvdev)
  701. {
  702. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  703. struct hv_device *hdev = ndev_ctx->device_ctx;
  704. int ret;
  705. /* Don't try continuing to try and setup sub channels */
  706. if (cancel_work_sync(&nvdev->subchan_work))
  707. nvdev->num_chn = 1;
  708. /* If device was up (receiving) then shutdown */
  709. if (netif_running(ndev)) {
  710. netif_tx_disable(ndev);
  711. ret = rndis_filter_close(nvdev);
  712. if (ret) {
  713. netdev_err(ndev,
  714. "unable to close device (ret %d).\n", ret);
  715. return ret;
  716. }
  717. ret = netvsc_wait_until_empty(nvdev);
  718. if (ret) {
  719. netdev_err(ndev,
  720. "Ring buffer not empty after closing rndis\n");
  721. return ret;
  722. }
  723. }
  724. netif_device_detach(ndev);
  725. rndis_filter_device_remove(hdev, nvdev);
  726. return 0;
  727. }
  728. static int netvsc_attach(struct net_device *ndev,
  729. struct netvsc_device_info *dev_info)
  730. {
  731. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  732. struct hv_device *hdev = ndev_ctx->device_ctx;
  733. struct netvsc_device *nvdev;
  734. struct rndis_device *rdev;
  735. int ret;
  736. nvdev = rndis_filter_device_add(hdev, dev_info);
  737. if (IS_ERR(nvdev))
  738. return PTR_ERR(nvdev);
  739. /* Note: enable and attach happen when sub-channels setup */
  740. netif_carrier_off(ndev);
  741. if (netif_running(ndev)) {
  742. ret = rndis_filter_open(nvdev);
  743. if (ret)
  744. return ret;
  745. rdev = nvdev->extension;
  746. if (!rdev->link_state)
  747. netif_carrier_on(ndev);
  748. }
  749. return 0;
  750. }
  751. static int netvsc_set_channels(struct net_device *net,
  752. struct ethtool_channels *channels)
  753. {
  754. struct net_device_context *net_device_ctx = netdev_priv(net);
  755. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  756. unsigned int orig, count = channels->combined_count;
  757. struct netvsc_device_info device_info;
  758. int ret;
  759. /* We do not support separate count for rx, tx, or other */
  760. if (count == 0 ||
  761. channels->rx_count || channels->tx_count || channels->other_count)
  762. return -EINVAL;
  763. if (!nvdev || nvdev->destroy)
  764. return -ENODEV;
  765. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  766. return -EINVAL;
  767. if (count > nvdev->max_chn)
  768. return -EINVAL;
  769. orig = nvdev->num_chn;
  770. memset(&device_info, 0, sizeof(device_info));
  771. device_info.num_chn = count;
  772. device_info.send_sections = nvdev->send_section_cnt;
  773. device_info.send_section_size = nvdev->send_section_size;
  774. device_info.recv_sections = nvdev->recv_section_cnt;
  775. device_info.recv_section_size = nvdev->recv_section_size;
  776. ret = netvsc_detach(net, nvdev);
  777. if (ret)
  778. return ret;
  779. ret = netvsc_attach(net, &device_info);
  780. if (ret) {
  781. device_info.num_chn = orig;
  782. if (netvsc_attach(net, &device_info))
  783. netdev_err(net, "restoring channel setting failed\n");
  784. }
  785. return ret;
  786. }
  787. static bool
  788. netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
  789. {
  790. struct ethtool_link_ksettings diff1 = *cmd;
  791. struct ethtool_link_ksettings diff2 = {};
  792. diff1.base.speed = 0;
  793. diff1.base.duplex = 0;
  794. /* advertising and cmd are usually set */
  795. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  796. diff1.base.cmd = 0;
  797. /* We set port to PORT_OTHER */
  798. diff2.base.port = PORT_OTHER;
  799. return !memcmp(&diff1, &diff2, sizeof(diff1));
  800. }
  801. static void netvsc_init_settings(struct net_device *dev)
  802. {
  803. struct net_device_context *ndc = netdev_priv(dev);
  804. ndc->l4_hash = HV_DEFAULT_L4HASH;
  805. ndc->speed = SPEED_UNKNOWN;
  806. ndc->duplex = DUPLEX_FULL;
  807. }
  808. static int netvsc_get_link_ksettings(struct net_device *dev,
  809. struct ethtool_link_ksettings *cmd)
  810. {
  811. struct net_device_context *ndc = netdev_priv(dev);
  812. cmd->base.speed = ndc->speed;
  813. cmd->base.duplex = ndc->duplex;
  814. cmd->base.port = PORT_OTHER;
  815. return 0;
  816. }
  817. static int netvsc_set_link_ksettings(struct net_device *dev,
  818. const struct ethtool_link_ksettings *cmd)
  819. {
  820. struct net_device_context *ndc = netdev_priv(dev);
  821. u32 speed;
  822. speed = cmd->base.speed;
  823. if (!ethtool_validate_speed(speed) ||
  824. !ethtool_validate_duplex(cmd->base.duplex) ||
  825. !netvsc_validate_ethtool_ss_cmd(cmd))
  826. return -EINVAL;
  827. ndc->speed = speed;
  828. ndc->duplex = cmd->base.duplex;
  829. return 0;
  830. }
  831. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  832. {
  833. struct net_device_context *ndevctx = netdev_priv(ndev);
  834. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  835. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  836. int orig_mtu = ndev->mtu;
  837. struct netvsc_device_info device_info;
  838. int ret = 0;
  839. if (!nvdev || nvdev->destroy)
  840. return -ENODEV;
  841. /* Change MTU of underlying VF netdev first. */
  842. if (vf_netdev) {
  843. ret = dev_set_mtu(vf_netdev, mtu);
  844. if (ret)
  845. return ret;
  846. }
  847. memset(&device_info, 0, sizeof(device_info));
  848. device_info.num_chn = nvdev->num_chn;
  849. device_info.send_sections = nvdev->send_section_cnt;
  850. device_info.send_section_size = nvdev->send_section_size;
  851. device_info.recv_sections = nvdev->recv_section_cnt;
  852. device_info.recv_section_size = nvdev->recv_section_size;
  853. ret = netvsc_detach(ndev, nvdev);
  854. if (ret)
  855. goto rollback_vf;
  856. ndev->mtu = mtu;
  857. ret = netvsc_attach(ndev, &device_info);
  858. if (ret)
  859. goto rollback;
  860. return 0;
  861. rollback:
  862. /* Attempt rollback to original MTU */
  863. ndev->mtu = orig_mtu;
  864. if (netvsc_attach(ndev, &device_info))
  865. netdev_err(ndev, "restoring mtu failed\n");
  866. rollback_vf:
  867. if (vf_netdev)
  868. dev_set_mtu(vf_netdev, orig_mtu);
  869. return ret;
  870. }
  871. static void netvsc_get_vf_stats(struct net_device *net,
  872. struct netvsc_vf_pcpu_stats *tot)
  873. {
  874. struct net_device_context *ndev_ctx = netdev_priv(net);
  875. int i;
  876. memset(tot, 0, sizeof(*tot));
  877. for_each_possible_cpu(i) {
  878. const struct netvsc_vf_pcpu_stats *stats
  879. = per_cpu_ptr(ndev_ctx->vf_stats, i);
  880. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  881. unsigned int start;
  882. do {
  883. start = u64_stats_fetch_begin_irq(&stats->syncp);
  884. rx_packets = stats->rx_packets;
  885. tx_packets = stats->tx_packets;
  886. rx_bytes = stats->rx_bytes;
  887. tx_bytes = stats->tx_bytes;
  888. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  889. tot->rx_packets += rx_packets;
  890. tot->tx_packets += tx_packets;
  891. tot->rx_bytes += rx_bytes;
  892. tot->tx_bytes += tx_bytes;
  893. tot->tx_dropped += stats->tx_dropped;
  894. }
  895. }
  896. static void netvsc_get_stats64(struct net_device *net,
  897. struct rtnl_link_stats64 *t)
  898. {
  899. struct net_device_context *ndev_ctx = netdev_priv(net);
  900. struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
  901. struct netvsc_vf_pcpu_stats vf_tot;
  902. int i;
  903. if (!nvdev)
  904. return;
  905. netdev_stats_to_stats64(t, &net->stats);
  906. netvsc_get_vf_stats(net, &vf_tot);
  907. t->rx_packets += vf_tot.rx_packets;
  908. t->tx_packets += vf_tot.tx_packets;
  909. t->rx_bytes += vf_tot.rx_bytes;
  910. t->tx_bytes += vf_tot.tx_bytes;
  911. t->tx_dropped += vf_tot.tx_dropped;
  912. for (i = 0; i < nvdev->num_chn; i++) {
  913. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  914. const struct netvsc_stats *stats;
  915. u64 packets, bytes, multicast;
  916. unsigned int start;
  917. stats = &nvchan->tx_stats;
  918. do {
  919. start = u64_stats_fetch_begin_irq(&stats->syncp);
  920. packets = stats->packets;
  921. bytes = stats->bytes;
  922. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  923. t->tx_bytes += bytes;
  924. t->tx_packets += packets;
  925. stats = &nvchan->rx_stats;
  926. do {
  927. start = u64_stats_fetch_begin_irq(&stats->syncp);
  928. packets = stats->packets;
  929. bytes = stats->bytes;
  930. multicast = stats->multicast + stats->broadcast;
  931. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  932. t->rx_bytes += bytes;
  933. t->rx_packets += packets;
  934. t->multicast += multicast;
  935. }
  936. }
  937. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  938. {
  939. struct net_device_context *ndc = netdev_priv(ndev);
  940. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  941. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  942. struct sockaddr *addr = p;
  943. int err;
  944. err = eth_prepare_mac_addr_change(ndev, p);
  945. if (err)
  946. return err;
  947. if (!nvdev)
  948. return -ENODEV;
  949. if (vf_netdev) {
  950. err = dev_set_mac_address(vf_netdev, addr);
  951. if (err)
  952. return err;
  953. }
  954. err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
  955. if (!err) {
  956. eth_commit_mac_addr_change(ndev, p);
  957. } else if (vf_netdev) {
  958. /* rollback change on VF */
  959. memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
  960. dev_set_mac_address(vf_netdev, addr);
  961. }
  962. return err;
  963. }
  964. static const struct {
  965. char name[ETH_GSTRING_LEN];
  966. u16 offset;
  967. } netvsc_stats[] = {
  968. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  969. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  970. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  971. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  972. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  973. { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
  974. { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
  975. { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
  976. { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
  977. { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
  978. }, vf_stats[] = {
  979. { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
  980. { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
  981. { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
  982. { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
  983. { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
  984. };
  985. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  986. #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
  987. /* 4 statistics per queue (rx/tx packets/bytes) */
  988. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
  989. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  990. {
  991. struct net_device_context *ndc = netdev_priv(dev);
  992. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  993. if (!nvdev)
  994. return -ENODEV;
  995. switch (string_set) {
  996. case ETH_SS_STATS:
  997. return NETVSC_GLOBAL_STATS_LEN
  998. + NETVSC_VF_STATS_LEN
  999. + NETVSC_QUEUE_STATS_LEN(nvdev);
  1000. default:
  1001. return -EINVAL;
  1002. }
  1003. }
  1004. static void netvsc_get_ethtool_stats(struct net_device *dev,
  1005. struct ethtool_stats *stats, u64 *data)
  1006. {
  1007. struct net_device_context *ndc = netdev_priv(dev);
  1008. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1009. const void *nds = &ndc->eth_stats;
  1010. const struct netvsc_stats *qstats;
  1011. struct netvsc_vf_pcpu_stats sum;
  1012. unsigned int start;
  1013. u64 packets, bytes;
  1014. int i, j;
  1015. if (!nvdev)
  1016. return;
  1017. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  1018. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  1019. netvsc_get_vf_stats(dev, &sum);
  1020. for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
  1021. data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
  1022. for (j = 0; j < nvdev->num_chn; j++) {
  1023. qstats = &nvdev->chan_table[j].tx_stats;
  1024. do {
  1025. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1026. packets = qstats->packets;
  1027. bytes = qstats->bytes;
  1028. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1029. data[i++] = packets;
  1030. data[i++] = bytes;
  1031. qstats = &nvdev->chan_table[j].rx_stats;
  1032. do {
  1033. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1034. packets = qstats->packets;
  1035. bytes = qstats->bytes;
  1036. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1037. data[i++] = packets;
  1038. data[i++] = bytes;
  1039. }
  1040. }
  1041. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1042. {
  1043. struct net_device_context *ndc = netdev_priv(dev);
  1044. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1045. u8 *p = data;
  1046. int i;
  1047. if (!nvdev)
  1048. return;
  1049. switch (stringset) {
  1050. case ETH_SS_STATS:
  1051. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
  1052. memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
  1053. p += ETH_GSTRING_LEN;
  1054. }
  1055. for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
  1056. memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
  1057. p += ETH_GSTRING_LEN;
  1058. }
  1059. for (i = 0; i < nvdev->num_chn; i++) {
  1060. sprintf(p, "tx_queue_%u_packets", i);
  1061. p += ETH_GSTRING_LEN;
  1062. sprintf(p, "tx_queue_%u_bytes", i);
  1063. p += ETH_GSTRING_LEN;
  1064. sprintf(p, "rx_queue_%u_packets", i);
  1065. p += ETH_GSTRING_LEN;
  1066. sprintf(p, "rx_queue_%u_bytes", i);
  1067. p += ETH_GSTRING_LEN;
  1068. }
  1069. break;
  1070. }
  1071. }
  1072. static int
  1073. netvsc_get_rss_hash_opts(struct net_device_context *ndc,
  1074. struct ethtool_rxnfc *info)
  1075. {
  1076. const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1077. info->data = RXH_IP_SRC | RXH_IP_DST;
  1078. switch (info->flow_type) {
  1079. case TCP_V4_FLOW:
  1080. if (ndc->l4_hash & HV_TCP4_L4HASH)
  1081. info->data |= l4_flag;
  1082. break;
  1083. case TCP_V6_FLOW:
  1084. if (ndc->l4_hash & HV_TCP6_L4HASH)
  1085. info->data |= l4_flag;
  1086. break;
  1087. case UDP_V4_FLOW:
  1088. if (ndc->l4_hash & HV_UDP4_L4HASH)
  1089. info->data |= l4_flag;
  1090. break;
  1091. case UDP_V6_FLOW:
  1092. if (ndc->l4_hash & HV_UDP6_L4HASH)
  1093. info->data |= l4_flag;
  1094. break;
  1095. case IPV4_FLOW:
  1096. case IPV6_FLOW:
  1097. break;
  1098. default:
  1099. info->data = 0;
  1100. break;
  1101. }
  1102. return 0;
  1103. }
  1104. static int
  1105. netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  1106. u32 *rules)
  1107. {
  1108. struct net_device_context *ndc = netdev_priv(dev);
  1109. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1110. if (!nvdev)
  1111. return -ENODEV;
  1112. switch (info->cmd) {
  1113. case ETHTOOL_GRXRINGS:
  1114. info->data = nvdev->num_chn;
  1115. return 0;
  1116. case ETHTOOL_GRXFH:
  1117. return netvsc_get_rss_hash_opts(ndc, info);
  1118. }
  1119. return -EOPNOTSUPP;
  1120. }
  1121. static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
  1122. struct ethtool_rxnfc *info)
  1123. {
  1124. if (info->data == (RXH_IP_SRC | RXH_IP_DST |
  1125. RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  1126. switch (info->flow_type) {
  1127. case TCP_V4_FLOW:
  1128. ndc->l4_hash |= HV_TCP4_L4HASH;
  1129. break;
  1130. case TCP_V6_FLOW:
  1131. ndc->l4_hash |= HV_TCP6_L4HASH;
  1132. break;
  1133. case UDP_V4_FLOW:
  1134. ndc->l4_hash |= HV_UDP4_L4HASH;
  1135. break;
  1136. case UDP_V6_FLOW:
  1137. ndc->l4_hash |= HV_UDP6_L4HASH;
  1138. break;
  1139. default:
  1140. return -EOPNOTSUPP;
  1141. }
  1142. return 0;
  1143. }
  1144. if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
  1145. switch (info->flow_type) {
  1146. case TCP_V4_FLOW:
  1147. ndc->l4_hash &= ~HV_TCP4_L4HASH;
  1148. break;
  1149. case TCP_V6_FLOW:
  1150. ndc->l4_hash &= ~HV_TCP6_L4HASH;
  1151. break;
  1152. case UDP_V4_FLOW:
  1153. ndc->l4_hash &= ~HV_UDP4_L4HASH;
  1154. break;
  1155. case UDP_V6_FLOW:
  1156. ndc->l4_hash &= ~HV_UDP6_L4HASH;
  1157. break;
  1158. default:
  1159. return -EOPNOTSUPP;
  1160. }
  1161. return 0;
  1162. }
  1163. return -EOPNOTSUPP;
  1164. }
  1165. static int
  1166. netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
  1167. {
  1168. struct net_device_context *ndc = netdev_priv(ndev);
  1169. if (info->cmd == ETHTOOL_SRXFH)
  1170. return netvsc_set_rss_hash_opts(ndc, info);
  1171. return -EOPNOTSUPP;
  1172. }
  1173. #ifdef CONFIG_NET_POLL_CONTROLLER
  1174. static void netvsc_poll_controller(struct net_device *dev)
  1175. {
  1176. struct net_device_context *ndc = netdev_priv(dev);
  1177. struct netvsc_device *ndev;
  1178. int i;
  1179. rcu_read_lock();
  1180. ndev = rcu_dereference(ndc->nvdev);
  1181. if (ndev) {
  1182. for (i = 0; i < ndev->num_chn; i++) {
  1183. struct netvsc_channel *nvchan = &ndev->chan_table[i];
  1184. napi_schedule(&nvchan->napi);
  1185. }
  1186. }
  1187. rcu_read_unlock();
  1188. }
  1189. #endif
  1190. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  1191. {
  1192. return NETVSC_HASH_KEYLEN;
  1193. }
  1194. static u32 netvsc_rss_indir_size(struct net_device *dev)
  1195. {
  1196. return ITAB_NUM;
  1197. }
  1198. static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  1199. u8 *hfunc)
  1200. {
  1201. struct net_device_context *ndc = netdev_priv(dev);
  1202. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1203. struct rndis_device *rndis_dev;
  1204. int i;
  1205. if (!ndev)
  1206. return -ENODEV;
  1207. if (hfunc)
  1208. *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  1209. rndis_dev = ndev->extension;
  1210. if (indir) {
  1211. for (i = 0; i < ITAB_NUM; i++)
  1212. indir[i] = rndis_dev->rx_table[i];
  1213. }
  1214. if (key)
  1215. memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  1216. return 0;
  1217. }
  1218. static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
  1219. const u8 *key, const u8 hfunc)
  1220. {
  1221. struct net_device_context *ndc = netdev_priv(dev);
  1222. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1223. struct rndis_device *rndis_dev;
  1224. int i;
  1225. if (!ndev)
  1226. return -ENODEV;
  1227. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  1228. return -EOPNOTSUPP;
  1229. rndis_dev = ndev->extension;
  1230. if (indir) {
  1231. for (i = 0; i < ITAB_NUM; i++)
  1232. if (indir[i] >= ndev->num_chn)
  1233. return -EINVAL;
  1234. for (i = 0; i < ITAB_NUM; i++)
  1235. rndis_dev->rx_table[i] = indir[i];
  1236. }
  1237. if (!key) {
  1238. if (!indir)
  1239. return 0;
  1240. key = rndis_dev->rss_key;
  1241. }
  1242. return rndis_filter_set_rss_param(rndis_dev, key);
  1243. }
  1244. /* Hyper-V RNDIS protocol does not have ring in the HW sense.
  1245. * It does have pre-allocated receive area which is divided into sections.
  1246. */
  1247. static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
  1248. struct ethtool_ringparam *ring)
  1249. {
  1250. u32 max_buf_size;
  1251. ring->rx_pending = nvdev->recv_section_cnt;
  1252. ring->tx_pending = nvdev->send_section_cnt;
  1253. if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  1254. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  1255. else
  1256. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  1257. ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
  1258. ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
  1259. / nvdev->send_section_size;
  1260. }
  1261. static void netvsc_get_ringparam(struct net_device *ndev,
  1262. struct ethtool_ringparam *ring)
  1263. {
  1264. struct net_device_context *ndevctx = netdev_priv(ndev);
  1265. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1266. if (!nvdev)
  1267. return;
  1268. __netvsc_get_ringparam(nvdev, ring);
  1269. }
  1270. static int netvsc_set_ringparam(struct net_device *ndev,
  1271. struct ethtool_ringparam *ring)
  1272. {
  1273. struct net_device_context *ndevctx = netdev_priv(ndev);
  1274. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1275. struct netvsc_device_info device_info;
  1276. struct ethtool_ringparam orig;
  1277. u32 new_tx, new_rx;
  1278. int ret = 0;
  1279. if (!nvdev || nvdev->destroy)
  1280. return -ENODEV;
  1281. memset(&orig, 0, sizeof(orig));
  1282. __netvsc_get_ringparam(nvdev, &orig);
  1283. new_tx = clamp_t(u32, ring->tx_pending,
  1284. NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
  1285. new_rx = clamp_t(u32, ring->rx_pending,
  1286. NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
  1287. if (new_tx == orig.tx_pending &&
  1288. new_rx == orig.rx_pending)
  1289. return 0; /* no change */
  1290. memset(&device_info, 0, sizeof(device_info));
  1291. device_info.num_chn = nvdev->num_chn;
  1292. device_info.send_sections = new_tx;
  1293. device_info.send_section_size = nvdev->send_section_size;
  1294. device_info.recv_sections = new_rx;
  1295. device_info.recv_section_size = nvdev->recv_section_size;
  1296. ret = netvsc_detach(ndev, nvdev);
  1297. if (ret)
  1298. return ret;
  1299. ret = netvsc_attach(ndev, &device_info);
  1300. if (ret) {
  1301. device_info.send_sections = orig.tx_pending;
  1302. device_info.recv_sections = orig.rx_pending;
  1303. if (netvsc_attach(ndev, &device_info))
  1304. netdev_err(ndev, "restoring ringparam failed");
  1305. }
  1306. return ret;
  1307. }
  1308. static u32 netvsc_get_msglevel(struct net_device *ndev)
  1309. {
  1310. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1311. return ndev_ctx->msg_enable;
  1312. }
  1313. static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
  1314. {
  1315. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1316. ndev_ctx->msg_enable = val;
  1317. }
  1318. static const struct ethtool_ops ethtool_ops = {
  1319. .get_drvinfo = netvsc_get_drvinfo,
  1320. .get_msglevel = netvsc_get_msglevel,
  1321. .set_msglevel = netvsc_set_msglevel,
  1322. .get_link = ethtool_op_get_link,
  1323. .get_ethtool_stats = netvsc_get_ethtool_stats,
  1324. .get_sset_count = netvsc_get_sset_count,
  1325. .get_strings = netvsc_get_strings,
  1326. .get_channels = netvsc_get_channels,
  1327. .set_channels = netvsc_set_channels,
  1328. .get_ts_info = ethtool_op_get_ts_info,
  1329. .get_rxnfc = netvsc_get_rxnfc,
  1330. .set_rxnfc = netvsc_set_rxnfc,
  1331. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  1332. .get_rxfh_indir_size = netvsc_rss_indir_size,
  1333. .get_rxfh = netvsc_get_rxfh,
  1334. .set_rxfh = netvsc_set_rxfh,
  1335. .get_link_ksettings = netvsc_get_link_ksettings,
  1336. .set_link_ksettings = netvsc_set_link_ksettings,
  1337. .get_ringparam = netvsc_get_ringparam,
  1338. .set_ringparam = netvsc_set_ringparam,
  1339. };
  1340. static const struct net_device_ops device_ops = {
  1341. .ndo_open = netvsc_open,
  1342. .ndo_stop = netvsc_close,
  1343. .ndo_start_xmit = netvsc_start_xmit,
  1344. .ndo_change_rx_flags = netvsc_change_rx_flags,
  1345. .ndo_set_rx_mode = netvsc_set_rx_mode,
  1346. .ndo_change_mtu = netvsc_change_mtu,
  1347. .ndo_validate_addr = eth_validate_addr,
  1348. .ndo_set_mac_address = netvsc_set_mac_addr,
  1349. .ndo_select_queue = netvsc_select_queue,
  1350. .ndo_get_stats64 = netvsc_get_stats64,
  1351. #ifdef CONFIG_NET_POLL_CONTROLLER
  1352. .ndo_poll_controller = netvsc_poll_controller,
  1353. #endif
  1354. };
  1355. /*
  1356. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  1357. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  1358. * present send GARP packet to network peers with netif_notify_peers().
  1359. */
  1360. static void netvsc_link_change(struct work_struct *w)
  1361. {
  1362. struct net_device_context *ndev_ctx =
  1363. container_of(w, struct net_device_context, dwork.work);
  1364. struct hv_device *device_obj = ndev_ctx->device_ctx;
  1365. struct net_device *net = hv_get_drvdata(device_obj);
  1366. struct netvsc_device *net_device;
  1367. struct rndis_device *rdev;
  1368. struct netvsc_reconfig *event = NULL;
  1369. bool notify = false, reschedule = false;
  1370. unsigned long flags, next_reconfig, delay;
  1371. /* if changes are happening, comeback later */
  1372. if (!rtnl_trylock()) {
  1373. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1374. return;
  1375. }
  1376. net_device = rtnl_dereference(ndev_ctx->nvdev);
  1377. if (!net_device)
  1378. goto out_unlock;
  1379. rdev = net_device->extension;
  1380. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  1381. if (time_is_after_jiffies(next_reconfig)) {
  1382. /* link_watch only sends one notification with current state
  1383. * per second, avoid doing reconfig more frequently. Handle
  1384. * wrap around.
  1385. */
  1386. delay = next_reconfig - jiffies;
  1387. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1388. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1389. goto out_unlock;
  1390. }
  1391. ndev_ctx->last_reconfig = jiffies;
  1392. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1393. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1394. event = list_first_entry(&ndev_ctx->reconfig_events,
  1395. struct netvsc_reconfig, list);
  1396. list_del(&event->list);
  1397. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1398. }
  1399. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1400. if (!event)
  1401. goto out_unlock;
  1402. switch (event->event) {
  1403. /* Only the following events are possible due to the check in
  1404. * netvsc_linkstatus_callback()
  1405. */
  1406. case RNDIS_STATUS_MEDIA_CONNECT:
  1407. if (rdev->link_state) {
  1408. rdev->link_state = false;
  1409. netif_carrier_on(net);
  1410. netif_tx_wake_all_queues(net);
  1411. } else {
  1412. notify = true;
  1413. }
  1414. kfree(event);
  1415. break;
  1416. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1417. if (!rdev->link_state) {
  1418. rdev->link_state = true;
  1419. netif_carrier_off(net);
  1420. netif_tx_stop_all_queues(net);
  1421. }
  1422. kfree(event);
  1423. break;
  1424. case RNDIS_STATUS_NETWORK_CHANGE:
  1425. /* Only makes sense if carrier is present */
  1426. if (!rdev->link_state) {
  1427. rdev->link_state = true;
  1428. netif_carrier_off(net);
  1429. netif_tx_stop_all_queues(net);
  1430. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1431. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1432. list_add(&event->list, &ndev_ctx->reconfig_events);
  1433. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1434. reschedule = true;
  1435. }
  1436. break;
  1437. }
  1438. rtnl_unlock();
  1439. if (notify)
  1440. netdev_notify_peers(net);
  1441. /* link_watch only sends one notification with current state per
  1442. * second, handle next reconfig event in 2 seconds.
  1443. */
  1444. if (reschedule)
  1445. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1446. return;
  1447. out_unlock:
  1448. rtnl_unlock();
  1449. }
  1450. /* Called when VF is injecting data into network stack.
  1451. * Change the associated network device from VF to netvsc.
  1452. * note: already called with rcu_read_lock
  1453. */
  1454. static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
  1455. {
  1456. struct sk_buff *skb = *pskb;
  1457. struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
  1458. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1459. struct netvsc_vf_pcpu_stats *pcpu_stats
  1460. = this_cpu_ptr(ndev_ctx->vf_stats);
  1461. skb->dev = ndev;
  1462. u64_stats_update_begin(&pcpu_stats->syncp);
  1463. pcpu_stats->rx_packets++;
  1464. pcpu_stats->rx_bytes += skb->len;
  1465. u64_stats_update_end(&pcpu_stats->syncp);
  1466. return RX_HANDLER_ANOTHER;
  1467. }
  1468. static void __netvsc_vf_setup(struct net_device *ndev,
  1469. struct net_device *vf_netdev)
  1470. {
  1471. int ret;
  1472. /* Align MTU of VF with master */
  1473. ret = dev_set_mtu(vf_netdev, ndev->mtu);
  1474. if (ret)
  1475. netdev_warn(vf_netdev,
  1476. "unable to change mtu to %u\n", ndev->mtu);
  1477. /* set multicast etc flags on VF */
  1478. dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE);
  1479. /* sync address list from ndev to VF */
  1480. netif_addr_lock_bh(ndev);
  1481. dev_uc_sync(vf_netdev, ndev);
  1482. dev_mc_sync(vf_netdev, ndev);
  1483. netif_addr_unlock_bh(ndev);
  1484. if (netif_running(ndev)) {
  1485. ret = dev_open(vf_netdev);
  1486. if (ret)
  1487. netdev_warn(vf_netdev,
  1488. "unable to open: %d\n", ret);
  1489. }
  1490. }
  1491. /* Setup VF as slave of the synthetic device.
  1492. * Runs in workqueue to avoid recursion in netlink callbacks.
  1493. */
  1494. static void netvsc_vf_setup(struct work_struct *w)
  1495. {
  1496. struct net_device_context *ndev_ctx
  1497. = container_of(w, struct net_device_context, vf_takeover.work);
  1498. struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1499. struct net_device *vf_netdev;
  1500. if (!rtnl_trylock()) {
  1501. schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
  1502. return;
  1503. }
  1504. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1505. if (vf_netdev)
  1506. __netvsc_vf_setup(ndev, vf_netdev);
  1507. rtnl_unlock();
  1508. }
  1509. static int netvsc_pre_register_vf(struct net_device *vf_netdev,
  1510. struct net_device *ndev)
  1511. {
  1512. struct net_device_context *net_device_ctx;
  1513. struct netvsc_device *netvsc_dev;
  1514. net_device_ctx = netdev_priv(ndev);
  1515. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1516. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1517. return -ENODEV;
  1518. return 0;
  1519. }
  1520. static int netvsc_register_vf(struct net_device *vf_netdev,
  1521. struct net_device *ndev)
  1522. {
  1523. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1524. /* set slave flag before open to prevent IPv6 addrconf */
  1525. vf_netdev->flags |= IFF_SLAVE;
  1526. schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
  1527. call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
  1528. netdev_info(vf_netdev, "joined to %s\n", ndev->name);
  1529. dev_hold(vf_netdev);
  1530. rcu_assign_pointer(ndev_ctx->vf_netdev, vf_netdev);
  1531. return 0;
  1532. }
  1533. /* VF up/down change detected, schedule to change data path */
  1534. static int netvsc_vf_changed(struct net_device *vf_netdev,
  1535. struct net_device *ndev)
  1536. {
  1537. struct net_device_context *net_device_ctx;
  1538. struct netvsc_device *netvsc_dev;
  1539. bool vf_is_up = netif_running(vf_netdev);
  1540. net_device_ctx = netdev_priv(ndev);
  1541. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1542. if (!netvsc_dev)
  1543. return -ENODEV;
  1544. netvsc_switch_datapath(ndev, vf_is_up);
  1545. netdev_info(ndev, "Data path switched %s VF: %s\n",
  1546. vf_is_up ? "to" : "from", vf_netdev->name);
  1547. return 0;
  1548. }
  1549. static int netvsc_pre_unregister_vf(struct net_device *vf_netdev,
  1550. struct net_device *ndev)
  1551. {
  1552. struct net_device_context *net_device_ctx;
  1553. net_device_ctx = netdev_priv(ndev);
  1554. cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
  1555. return 0;
  1556. }
  1557. static int netvsc_unregister_vf(struct net_device *vf_netdev,
  1558. struct net_device *ndev)
  1559. {
  1560. struct net_device_context *net_device_ctx;
  1561. net_device_ctx = netdev_priv(ndev);
  1562. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1563. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1564. dev_put(vf_netdev);
  1565. return 0;
  1566. }
  1567. static struct failover_ops netvsc_failover_ops = {
  1568. .slave_pre_register = netvsc_pre_register_vf,
  1569. .slave_register = netvsc_register_vf,
  1570. .slave_pre_unregister = netvsc_pre_unregister_vf,
  1571. .slave_unregister = netvsc_unregister_vf,
  1572. .slave_link_change = netvsc_vf_changed,
  1573. .slave_handle_frame = netvsc_vf_handle_frame,
  1574. };
  1575. static int netvsc_probe(struct hv_device *dev,
  1576. const struct hv_vmbus_device_id *dev_id)
  1577. {
  1578. struct net_device *net = NULL;
  1579. struct net_device_context *net_device_ctx;
  1580. struct netvsc_device_info device_info;
  1581. struct netvsc_device *nvdev;
  1582. int ret = -ENOMEM;
  1583. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1584. VRSS_CHANNEL_MAX);
  1585. if (!net)
  1586. goto no_net;
  1587. netif_carrier_off(net);
  1588. netvsc_init_settings(net);
  1589. net_device_ctx = netdev_priv(net);
  1590. net_device_ctx->device_ctx = dev;
  1591. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1592. if (netif_msg_probe(net_device_ctx))
  1593. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1594. net_device_ctx->msg_enable);
  1595. hv_set_drvdata(dev, net);
  1596. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1597. spin_lock_init(&net_device_ctx->lock);
  1598. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1599. INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
  1600. net_device_ctx->vf_stats
  1601. = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
  1602. if (!net_device_ctx->vf_stats)
  1603. goto no_stats;
  1604. net->netdev_ops = &device_ops;
  1605. net->ethtool_ops = &ethtool_ops;
  1606. SET_NETDEV_DEV(net, &dev->device);
  1607. /* We always need headroom for rndis header */
  1608. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1609. /* Initialize the number of queues to be 1, we may change it if more
  1610. * channels are offered later.
  1611. */
  1612. netif_set_real_num_tx_queues(net, 1);
  1613. netif_set_real_num_rx_queues(net, 1);
  1614. /* Notify the netvsc driver of the new device */
  1615. memset(&device_info, 0, sizeof(device_info));
  1616. device_info.num_chn = VRSS_CHANNEL_DEFAULT;
  1617. device_info.send_sections = NETVSC_DEFAULT_TX;
  1618. device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
  1619. device_info.recv_sections = NETVSC_DEFAULT_RX;
  1620. device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
  1621. nvdev = rndis_filter_device_add(dev, &device_info);
  1622. if (IS_ERR(nvdev)) {
  1623. ret = PTR_ERR(nvdev);
  1624. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1625. goto rndis_failed;
  1626. }
  1627. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1628. /* hw_features computed in rndis_netdev_set_hwcaps() */
  1629. net->features = net->hw_features |
  1630. NETIF_F_HIGHDMA | NETIF_F_SG |
  1631. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1632. net->vlan_features = net->features;
  1633. netdev_lockdep_set_classes(net);
  1634. /* MTU range: 68 - 1500 or 65521 */
  1635. net->min_mtu = NETVSC_MTU_MIN;
  1636. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1637. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1638. else
  1639. net->max_mtu = ETH_DATA_LEN;
  1640. ret = register_netdev(net);
  1641. if (ret != 0) {
  1642. pr_err("Unable to register netdev.\n");
  1643. goto register_failed;
  1644. }
  1645. net_device_ctx->failover = failover_register(net, &netvsc_failover_ops);
  1646. if (IS_ERR(net_device_ctx->failover)) {
  1647. ret = PTR_ERR(net_device_ctx->failover);
  1648. goto err_failover;
  1649. }
  1650. return ret;
  1651. err_failover:
  1652. unregister_netdev(net);
  1653. register_failed:
  1654. rndis_filter_device_remove(dev, nvdev);
  1655. rndis_failed:
  1656. free_percpu(net_device_ctx->vf_stats);
  1657. no_stats:
  1658. hv_set_drvdata(dev, NULL);
  1659. free_netdev(net);
  1660. no_net:
  1661. return ret;
  1662. }
  1663. static int netvsc_remove(struct hv_device *dev)
  1664. {
  1665. struct net_device_context *ndev_ctx;
  1666. struct net_device *vf_netdev, *net;
  1667. struct netvsc_device *nvdev;
  1668. net = hv_get_drvdata(dev);
  1669. if (net == NULL) {
  1670. dev_err(&dev->device, "No net device to remove\n");
  1671. return 0;
  1672. }
  1673. ndev_ctx = netdev_priv(net);
  1674. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1675. rcu_read_lock();
  1676. nvdev = rcu_dereference(ndev_ctx->nvdev);
  1677. if (nvdev)
  1678. cancel_work_sync(&nvdev->subchan_work);
  1679. /*
  1680. * Call to the vsc driver to let it know that the device is being
  1681. * removed. Also blocks mtu and channel changes.
  1682. */
  1683. rtnl_lock();
  1684. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1685. if (vf_netdev)
  1686. failover_slave_unregister(vf_netdev);
  1687. if (nvdev)
  1688. rndis_filter_device_remove(dev, nvdev);
  1689. unregister_netdevice(net);
  1690. failover_unregister(ndev_ctx->failover);
  1691. rtnl_unlock();
  1692. rcu_read_unlock();
  1693. hv_set_drvdata(dev, NULL);
  1694. free_percpu(ndev_ctx->vf_stats);
  1695. free_netdev(net);
  1696. return 0;
  1697. }
  1698. static const struct hv_vmbus_device_id id_table[] = {
  1699. /* Network guid */
  1700. { HV_NIC_GUID, },
  1701. { },
  1702. };
  1703. MODULE_DEVICE_TABLE(vmbus, id_table);
  1704. /* The one and only one */
  1705. static struct hv_driver netvsc_drv = {
  1706. .name = KBUILD_MODNAME,
  1707. .id_table = id_table,
  1708. .probe = netvsc_probe,
  1709. .remove = netvsc_remove,
  1710. };
  1711. static void __exit netvsc_drv_exit(void)
  1712. {
  1713. vmbus_driver_unregister(&netvsc_drv);
  1714. }
  1715. static int __init netvsc_drv_init(void)
  1716. {
  1717. int ret;
  1718. if (ring_size < RING_SIZE_MIN) {
  1719. ring_size = RING_SIZE_MIN;
  1720. pr_info("Increased ring_size to %u (min allowed)\n",
  1721. ring_size);
  1722. }
  1723. netvsc_ring_bytes = ring_size * PAGE_SIZE;
  1724. ret = vmbus_driver_register(&netvsc_drv);
  1725. if (ret)
  1726. return ret;
  1727. return 0;
  1728. }
  1729. MODULE_LICENSE("GPL");
  1730. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1731. module_init(netvsc_drv_init);
  1732. module_exit(netvsc_drv_exit);