netvsc_drv.c 54 KB

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