netvsc_drv.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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 <net/arp.h>
  36. #include <net/route.h>
  37. #include <net/sock.h>
  38. #include <net/pkt_sched.h>
  39. #include "hyperv_net.h"
  40. #define RING_SIZE_MIN 64
  41. #define LINKCHANGE_INT (2 * HZ)
  42. #define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
  43. NETIF_F_SG | \
  44. NETIF_F_TSO | \
  45. NETIF_F_TSO6 | \
  46. NETIF_F_HW_CSUM)
  47. static int ring_size = 128;
  48. module_param(ring_size, int, S_IRUGO);
  49. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  50. static int max_num_vrss_chns = 8;
  51. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  52. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  53. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  54. NETIF_MSG_TX_ERR;
  55. static int debug = -1;
  56. module_param(debug, int, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  58. static void do_set_multicast(struct work_struct *w)
  59. {
  60. struct net_device_context *ndevctx =
  61. container_of(w, struct net_device_context, work);
  62. struct hv_device *device_obj = ndevctx->device_ctx;
  63. struct net_device *ndev = hv_get_drvdata(device_obj);
  64. struct netvsc_device *nvdev = ndevctx->nvdev;
  65. struct rndis_device *rdev;
  66. if (!nvdev)
  67. return;
  68. rdev = nvdev->extension;
  69. if (rdev == NULL)
  70. return;
  71. if (ndev->flags & IFF_PROMISC)
  72. rndis_filter_set_packet_filter(rdev,
  73. NDIS_PACKET_TYPE_PROMISCUOUS);
  74. else
  75. rndis_filter_set_packet_filter(rdev,
  76. NDIS_PACKET_TYPE_BROADCAST |
  77. NDIS_PACKET_TYPE_ALL_MULTICAST |
  78. NDIS_PACKET_TYPE_DIRECTED);
  79. }
  80. static void netvsc_set_multicast_list(struct net_device *net)
  81. {
  82. struct net_device_context *net_device_ctx = netdev_priv(net);
  83. schedule_work(&net_device_ctx->work);
  84. }
  85. static int netvsc_open(struct net_device *net)
  86. {
  87. struct netvsc_device *nvdev = net_device_to_netvsc_device(net);
  88. struct rndis_device *rdev;
  89. int ret = 0;
  90. netif_carrier_off(net);
  91. /* Open up the device */
  92. ret = rndis_filter_open(nvdev);
  93. if (ret != 0) {
  94. netdev_err(net, "unable to open device (ret %d).\n", ret);
  95. return ret;
  96. }
  97. netif_tx_wake_all_queues(net);
  98. rdev = nvdev->extension;
  99. if (!rdev->link_state)
  100. netif_carrier_on(net);
  101. return ret;
  102. }
  103. static int netvsc_close(struct net_device *net)
  104. {
  105. struct net_device_context *net_device_ctx = netdev_priv(net);
  106. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  107. int ret;
  108. u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
  109. struct vmbus_channel *chn;
  110. netif_tx_disable(net);
  111. /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
  112. cancel_work_sync(&net_device_ctx->work);
  113. ret = rndis_filter_close(nvdev);
  114. if (ret != 0) {
  115. netdev_err(net, "unable to close device (ret %d).\n", ret);
  116. return ret;
  117. }
  118. /* Ensure pending bytes in ring are read */
  119. while (true) {
  120. aread = 0;
  121. for (i = 0; i < nvdev->num_chn; i++) {
  122. chn = nvdev->chn_table[i];
  123. if (!chn)
  124. continue;
  125. hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
  126. &awrite);
  127. if (aread)
  128. break;
  129. hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
  130. &awrite);
  131. if (aread)
  132. break;
  133. }
  134. retry++;
  135. if (retry > retry_max || aread == 0)
  136. break;
  137. msleep(msec);
  138. if (msec < 1000)
  139. msec *= 2;
  140. }
  141. if (aread) {
  142. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  143. ret = -ETIMEDOUT;
  144. }
  145. return ret;
  146. }
  147. static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
  148. int pkt_type)
  149. {
  150. struct rndis_packet *rndis_pkt;
  151. struct rndis_per_packet_info *ppi;
  152. rndis_pkt = &msg->msg.pkt;
  153. rndis_pkt->data_offset += ppi_size;
  154. ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
  155. rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
  156. ppi->size = ppi_size;
  157. ppi->type = pkt_type;
  158. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  159. rndis_pkt->per_pkt_info_len += ppi_size;
  160. return ppi;
  161. }
  162. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  163. void *accel_priv, select_queue_fallback_t fallback)
  164. {
  165. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  166. struct netvsc_device *nvsc_dev = net_device_ctx->nvdev;
  167. u32 hash;
  168. u16 q_idx = 0;
  169. if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
  170. return 0;
  171. hash = skb_get_hash(skb);
  172. q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
  173. ndev->real_num_tx_queues;
  174. if (!nvsc_dev->chn_table[q_idx])
  175. q_idx = 0;
  176. return q_idx;
  177. }
  178. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  179. struct hv_page_buffer *pb)
  180. {
  181. int j = 0;
  182. /* Deal with compund pages by ignoring unused part
  183. * of the page.
  184. */
  185. page += (offset >> PAGE_SHIFT);
  186. offset &= ~PAGE_MASK;
  187. while (len > 0) {
  188. unsigned long bytes;
  189. bytes = PAGE_SIZE - offset;
  190. if (bytes > len)
  191. bytes = len;
  192. pb[j].pfn = page_to_pfn(page);
  193. pb[j].offset = offset;
  194. pb[j].len = bytes;
  195. offset += bytes;
  196. len -= bytes;
  197. if (offset == PAGE_SIZE && len) {
  198. page++;
  199. offset = 0;
  200. j++;
  201. }
  202. }
  203. return j + 1;
  204. }
  205. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  206. struct hv_netvsc_packet *packet,
  207. struct hv_page_buffer **page_buf)
  208. {
  209. struct hv_page_buffer *pb = *page_buf;
  210. u32 slots_used = 0;
  211. char *data = skb->data;
  212. int frags = skb_shinfo(skb)->nr_frags;
  213. int i;
  214. /* The packet is laid out thus:
  215. * 1. hdr: RNDIS header and PPI
  216. * 2. skb linear data
  217. * 3. skb fragment data
  218. */
  219. if (hdr != NULL)
  220. slots_used += fill_pg_buf(virt_to_page(hdr),
  221. offset_in_page(hdr),
  222. len, &pb[slots_used]);
  223. packet->rmsg_size = len;
  224. packet->rmsg_pgcnt = slots_used;
  225. slots_used += fill_pg_buf(virt_to_page(data),
  226. offset_in_page(data),
  227. skb_headlen(skb), &pb[slots_used]);
  228. for (i = 0; i < frags; i++) {
  229. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  230. slots_used += fill_pg_buf(skb_frag_page(frag),
  231. frag->page_offset,
  232. skb_frag_size(frag), &pb[slots_used]);
  233. }
  234. return slots_used;
  235. }
  236. static int count_skb_frag_slots(struct sk_buff *skb)
  237. {
  238. int i, frags = skb_shinfo(skb)->nr_frags;
  239. int pages = 0;
  240. for (i = 0; i < frags; i++) {
  241. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  242. unsigned long size = skb_frag_size(frag);
  243. unsigned long offset = frag->page_offset;
  244. /* Skip unused frames from start of page */
  245. offset &= ~PAGE_MASK;
  246. pages += PFN_UP(offset + size);
  247. }
  248. return pages;
  249. }
  250. static int netvsc_get_slots(struct sk_buff *skb)
  251. {
  252. char *data = skb->data;
  253. unsigned int offset = offset_in_page(data);
  254. unsigned int len = skb_headlen(skb);
  255. int slots;
  256. int frag_slots;
  257. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  258. frag_slots = count_skb_frag_slots(skb);
  259. return slots + frag_slots;
  260. }
  261. static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
  262. {
  263. u32 ret_val = TRANSPORT_INFO_NOT_IP;
  264. if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
  265. (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
  266. goto not_ip;
  267. }
  268. *trans_off = skb_transport_offset(skb);
  269. if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
  270. struct iphdr *iphdr = ip_hdr(skb);
  271. if (iphdr->protocol == IPPROTO_TCP)
  272. ret_val = TRANSPORT_INFO_IPV4_TCP;
  273. else if (iphdr->protocol == IPPROTO_UDP)
  274. ret_val = TRANSPORT_INFO_IPV4_UDP;
  275. } else {
  276. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  277. ret_val = TRANSPORT_INFO_IPV6_TCP;
  278. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
  279. ret_val = TRANSPORT_INFO_IPV6_UDP;
  280. }
  281. not_ip:
  282. return ret_val;
  283. }
  284. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  285. {
  286. struct net_device_context *net_device_ctx = netdev_priv(net);
  287. struct hv_netvsc_packet *packet = NULL;
  288. int ret;
  289. unsigned int num_data_pgs;
  290. struct rndis_message *rndis_msg;
  291. struct rndis_packet *rndis_pkt;
  292. u32 rndis_msg_size;
  293. struct rndis_per_packet_info *ppi;
  294. struct ndis_tcp_ip_checksum_info *csum_info;
  295. int hdr_offset;
  296. u32 net_trans_info;
  297. u32 hash;
  298. u32 skb_length;
  299. struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
  300. struct hv_page_buffer *pb = page_buf;
  301. /* We will atmost need two pages to describe the rndis
  302. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  303. * of pages in a single packet. If skb is scattered around
  304. * more pages we try linearizing it.
  305. */
  306. skb_length = skb->len;
  307. num_data_pgs = netvsc_get_slots(skb) + 2;
  308. if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
  309. ++net_device_ctx->eth_stats.tx_scattered;
  310. if (skb_linearize(skb))
  311. goto no_memory;
  312. num_data_pgs = netvsc_get_slots(skb) + 2;
  313. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  314. ++net_device_ctx->eth_stats.tx_too_big;
  315. goto drop;
  316. }
  317. }
  318. /*
  319. * Place the rndis header in the skb head room and
  320. * the skb->cb will be used for hv_netvsc_packet
  321. * structure.
  322. */
  323. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  324. if (ret)
  325. goto no_memory;
  326. /* Use the skb control buffer for building up the packet */
  327. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  328. FIELD_SIZEOF(struct sk_buff, cb));
  329. packet = (struct hv_netvsc_packet *)skb->cb;
  330. packet->q_idx = skb_get_queue_mapping(skb);
  331. packet->total_data_buflen = skb->len;
  332. rndis_msg = (struct rndis_message *)skb->head;
  333. memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
  334. /* Add the rndis header */
  335. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  336. rndis_msg->msg_len = packet->total_data_buflen;
  337. rndis_pkt = &rndis_msg->msg.pkt;
  338. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  339. rndis_pkt->data_len = packet->total_data_buflen;
  340. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  341. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  342. hash = skb_get_hash_raw(skb);
  343. if (hash != 0 && net->real_num_tx_queues > 1) {
  344. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  345. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  346. NBL_HASH_VALUE);
  347. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  348. }
  349. if (skb_vlan_tag_present(skb)) {
  350. struct ndis_pkt_8021q_info *vlan;
  351. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  352. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  353. IEEE_8021Q_INFO);
  354. vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
  355. ppi->ppi_offset);
  356. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  357. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  358. VLAN_PRIO_SHIFT;
  359. }
  360. net_trans_info = get_net_transport_info(skb, &hdr_offset);
  361. if (net_trans_info == TRANSPORT_INFO_NOT_IP)
  362. goto do_send;
  363. /*
  364. * Setup the sendside checksum offload only if this is not a
  365. * GSO packet.
  366. */
  367. if (skb_is_gso(skb)) {
  368. struct ndis_tcp_lso_info *lso_info;
  369. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  370. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  371. TCP_LARGESEND_PKTINFO);
  372. lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
  373. ppi->ppi_offset);
  374. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  375. if (net_trans_info & (INFO_IPV4 << 16)) {
  376. lso_info->lso_v2_transmit.ip_version =
  377. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  378. ip_hdr(skb)->tot_len = 0;
  379. ip_hdr(skb)->check = 0;
  380. tcp_hdr(skb)->check =
  381. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  382. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  383. } else {
  384. lso_info->lso_v2_transmit.ip_version =
  385. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  386. ipv6_hdr(skb)->payload_len = 0;
  387. tcp_hdr(skb)->check =
  388. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  389. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  390. }
  391. lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
  392. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  393. goto do_send;
  394. }
  395. if ((skb->ip_summed == CHECKSUM_NONE) ||
  396. (skb->ip_summed == CHECKSUM_UNNECESSARY))
  397. goto do_send;
  398. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  399. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  400. TCPIP_CHKSUM_PKTINFO);
  401. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  402. ppi->ppi_offset);
  403. if (net_trans_info & (INFO_IPV4 << 16))
  404. csum_info->transmit.is_ipv4 = 1;
  405. else
  406. csum_info->transmit.is_ipv6 = 1;
  407. if (net_trans_info & INFO_TCP) {
  408. csum_info->transmit.tcp_checksum = 1;
  409. csum_info->transmit.tcp_header_offset = hdr_offset;
  410. } else if (net_trans_info & INFO_UDP) {
  411. /* UDP checksum offload is not supported on ws2008r2.
  412. * Furthermore, on ws2012 and ws2012r2, there are some
  413. * issues with udp checksum offload from Linux guests.
  414. * (these are host issues).
  415. * For now compute the checksum here.
  416. */
  417. struct udphdr *uh;
  418. u16 udp_len;
  419. ret = skb_cow_head(skb, 0);
  420. if (ret)
  421. goto no_memory;
  422. uh = udp_hdr(skb);
  423. udp_len = ntohs(uh->len);
  424. uh->check = 0;
  425. uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
  426. ip_hdr(skb)->daddr,
  427. udp_len, IPPROTO_UDP,
  428. csum_partial(uh, udp_len, 0));
  429. if (uh->check == 0)
  430. uh->check = CSUM_MANGLED_0;
  431. csum_info->transmit.udp_checksum = 0;
  432. }
  433. do_send:
  434. /* Start filling in the page buffers with the rndis hdr */
  435. rndis_msg->msg_len += rndis_msg_size;
  436. packet->total_data_buflen = rndis_msg->msg_len;
  437. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  438. skb, packet, &pb);
  439. /* timestamp packet in software */
  440. skb_tx_timestamp(skb);
  441. ret = netvsc_send(net_device_ctx->device_ctx, packet,
  442. rndis_msg, &pb, skb);
  443. if (likely(ret == 0)) {
  444. struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
  445. u64_stats_update_begin(&tx_stats->syncp);
  446. tx_stats->packets++;
  447. tx_stats->bytes += skb_length;
  448. u64_stats_update_end(&tx_stats->syncp);
  449. return NETDEV_TX_OK;
  450. }
  451. if (ret == -EAGAIN) {
  452. ++net_device_ctx->eth_stats.tx_busy;
  453. return NETDEV_TX_BUSY;
  454. }
  455. if (ret == -ENOSPC)
  456. ++net_device_ctx->eth_stats.tx_no_space;
  457. drop:
  458. dev_kfree_skb_any(skb);
  459. net->stats.tx_dropped++;
  460. return NETDEV_TX_OK;
  461. no_memory:
  462. ++net_device_ctx->eth_stats.tx_no_memory;
  463. goto drop;
  464. }
  465. /*
  466. * netvsc_linkstatus_callback - Link up/down notification
  467. */
  468. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  469. struct rndis_message *resp)
  470. {
  471. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  472. struct net_device *net;
  473. struct net_device_context *ndev_ctx;
  474. struct netvsc_reconfig *event;
  475. unsigned long flags;
  476. net = hv_get_drvdata(device_obj);
  477. if (!net)
  478. return;
  479. ndev_ctx = netdev_priv(net);
  480. /* Update the physical link speed when changing to another vSwitch */
  481. if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
  482. u32 speed;
  483. speed = *(u32 *)((void *)indicate + indicate->
  484. status_buf_offset) / 10000;
  485. ndev_ctx->speed = speed;
  486. return;
  487. }
  488. /* Handle these link change statuses below */
  489. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  490. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  491. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  492. return;
  493. if (net->reg_state != NETREG_REGISTERED)
  494. return;
  495. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  496. if (!event)
  497. return;
  498. event->event = indicate->status;
  499. spin_lock_irqsave(&ndev_ctx->lock, flags);
  500. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  501. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  502. schedule_delayed_work(&ndev_ctx->dwork, 0);
  503. }
  504. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  505. struct hv_netvsc_packet *packet,
  506. struct ndis_tcp_ip_checksum_info *csum_info,
  507. void *data, u16 vlan_tci)
  508. {
  509. struct sk_buff *skb;
  510. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  511. if (!skb)
  512. return skb;
  513. /*
  514. * Copy to skb. This copy is needed here since the memory pointed by
  515. * hv_netvsc_packet cannot be deallocated
  516. */
  517. memcpy(skb_put(skb, packet->total_data_buflen), data,
  518. packet->total_data_buflen);
  519. skb->protocol = eth_type_trans(skb, net);
  520. if (csum_info) {
  521. /* We only look at the IP checksum here.
  522. * Should we be dropping the packet if checksum
  523. * failed? How do we deal with other checksums - TCP/UDP?
  524. */
  525. if (csum_info->receive.ip_checksum_succeeded)
  526. skb->ip_summed = CHECKSUM_UNNECESSARY;
  527. else
  528. skb->ip_summed = CHECKSUM_NONE;
  529. }
  530. if (vlan_tci & VLAN_TAG_PRESENT)
  531. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  532. vlan_tci);
  533. return skb;
  534. }
  535. /*
  536. * netvsc_recv_callback - Callback when we receive a packet from the
  537. * "wire" on the specified device.
  538. */
  539. int netvsc_recv_callback(struct hv_device *device_obj,
  540. struct hv_netvsc_packet *packet,
  541. void **data,
  542. struct ndis_tcp_ip_checksum_info *csum_info,
  543. struct vmbus_channel *channel,
  544. u16 vlan_tci)
  545. {
  546. struct net_device *net = hv_get_drvdata(device_obj);
  547. struct net_device_context *net_device_ctx = netdev_priv(net);
  548. struct sk_buff *skb;
  549. struct sk_buff *vf_skb;
  550. struct netvsc_stats *rx_stats;
  551. u32 bytes_recvd = packet->total_data_buflen;
  552. int ret = 0;
  553. if (!net || net->reg_state != NETREG_REGISTERED)
  554. return NVSP_STAT_FAIL;
  555. if (READ_ONCE(net_device_ctx->vf_inject)) {
  556. atomic_inc(&net_device_ctx->vf_use_cnt);
  557. if (!READ_ONCE(net_device_ctx->vf_inject)) {
  558. /*
  559. * We raced; just move on.
  560. */
  561. atomic_dec(&net_device_ctx->vf_use_cnt);
  562. goto vf_injection_done;
  563. }
  564. /*
  565. * Inject this packet into the VF inerface.
  566. * On Hyper-V, multicast and brodcast packets
  567. * are only delivered on the synthetic interface
  568. * (after subjecting these to policy filters on
  569. * the host). Deliver these via the VF interface
  570. * in the guest.
  571. */
  572. vf_skb = netvsc_alloc_recv_skb(net_device_ctx->vf_netdev,
  573. packet, csum_info, *data,
  574. vlan_tci);
  575. if (vf_skb != NULL) {
  576. ++net_device_ctx->vf_netdev->stats.rx_packets;
  577. net_device_ctx->vf_netdev->stats.rx_bytes +=
  578. bytes_recvd;
  579. netif_receive_skb(vf_skb);
  580. } else {
  581. ++net->stats.rx_dropped;
  582. ret = NVSP_STAT_FAIL;
  583. }
  584. atomic_dec(&net_device_ctx->vf_use_cnt);
  585. return ret;
  586. }
  587. vf_injection_done:
  588. rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
  589. /* Allocate a skb - TODO direct I/O to pages? */
  590. skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
  591. if (unlikely(!skb)) {
  592. ++net->stats.rx_dropped;
  593. return NVSP_STAT_FAIL;
  594. }
  595. skb_record_rx_queue(skb, channel->
  596. offermsg.offer.sub_channel_index);
  597. u64_stats_update_begin(&rx_stats->syncp);
  598. rx_stats->packets++;
  599. rx_stats->bytes += packet->total_data_buflen;
  600. u64_stats_update_end(&rx_stats->syncp);
  601. /*
  602. * Pass the skb back up. Network stack will deallocate the skb when it
  603. * is done.
  604. * TODO - use NAPI?
  605. */
  606. netif_rx(skb);
  607. return 0;
  608. }
  609. static void netvsc_get_drvinfo(struct net_device *net,
  610. struct ethtool_drvinfo *info)
  611. {
  612. struct net_device_context *net_device_ctx = netdev_priv(net);
  613. struct hv_device *dev = net_device_ctx->device_ctx;
  614. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  615. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  616. strlcpy(info->bus_info, vmbus_dev_name(dev), sizeof(info->bus_info));
  617. }
  618. static void netvsc_get_channels(struct net_device *net,
  619. struct ethtool_channels *channel)
  620. {
  621. struct net_device_context *net_device_ctx = netdev_priv(net);
  622. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  623. if (nvdev) {
  624. channel->max_combined = nvdev->max_chn;
  625. channel->combined_count = nvdev->num_chn;
  626. }
  627. }
  628. static int netvsc_set_channels(struct net_device *net,
  629. struct ethtool_channels *channels)
  630. {
  631. struct net_device_context *net_device_ctx = netdev_priv(net);
  632. struct hv_device *dev = net_device_ctx->device_ctx;
  633. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  634. struct netvsc_device_info device_info;
  635. u32 num_chn;
  636. u32 max_chn;
  637. int ret = 0;
  638. bool recovering = false;
  639. if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
  640. return -ENODEV;
  641. num_chn = nvdev->num_chn;
  642. max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
  643. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
  644. pr_info("vRSS unsupported before NVSP Version 5\n");
  645. return -EINVAL;
  646. }
  647. /* We do not support rx, tx, or other */
  648. if (!channels ||
  649. channels->rx_count ||
  650. channels->tx_count ||
  651. channels->other_count ||
  652. (channels->combined_count < 1))
  653. return -EINVAL;
  654. if (channels->combined_count > max_chn) {
  655. pr_info("combined channels too high, using %d\n", max_chn);
  656. channels->combined_count = max_chn;
  657. }
  658. ret = netvsc_close(net);
  659. if (ret)
  660. goto out;
  661. do_set:
  662. net_device_ctx->start_remove = true;
  663. rndis_filter_device_remove(dev);
  664. nvdev->num_chn = channels->combined_count;
  665. memset(&device_info, 0, sizeof(device_info));
  666. device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
  667. device_info.ring_size = ring_size;
  668. device_info.max_num_vrss_chns = max_num_vrss_chns;
  669. ret = rndis_filter_device_add(dev, &device_info);
  670. if (ret) {
  671. if (recovering) {
  672. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  673. return ret;
  674. }
  675. goto recover;
  676. }
  677. nvdev = net_device_ctx->nvdev;
  678. ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
  679. if (ret) {
  680. if (recovering) {
  681. netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
  682. return ret;
  683. }
  684. goto recover;
  685. }
  686. ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
  687. if (ret) {
  688. if (recovering) {
  689. netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
  690. return ret;
  691. }
  692. goto recover;
  693. }
  694. out:
  695. netvsc_open(net);
  696. net_device_ctx->start_remove = false;
  697. /* We may have missed link change notifications */
  698. schedule_delayed_work(&net_device_ctx->dwork, 0);
  699. return ret;
  700. recover:
  701. /* If the above failed, we attempt to recover through the same
  702. * process but with the original number of channels.
  703. */
  704. netdev_err(net, "could not set channels, recovering\n");
  705. recovering = true;
  706. channels->combined_count = num_chn;
  707. goto do_set;
  708. }
  709. static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
  710. {
  711. struct ethtool_cmd diff1 = *cmd;
  712. struct ethtool_cmd diff2 = {};
  713. ethtool_cmd_speed_set(&diff1, 0);
  714. diff1.duplex = 0;
  715. /* advertising and cmd are usually set */
  716. diff1.advertising = 0;
  717. diff1.cmd = 0;
  718. /* We set port to PORT_OTHER */
  719. diff2.port = PORT_OTHER;
  720. return !memcmp(&diff1, &diff2, sizeof(diff1));
  721. }
  722. static void netvsc_init_settings(struct net_device *dev)
  723. {
  724. struct net_device_context *ndc = netdev_priv(dev);
  725. ndc->speed = SPEED_UNKNOWN;
  726. ndc->duplex = DUPLEX_UNKNOWN;
  727. }
  728. static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  729. {
  730. struct net_device_context *ndc = netdev_priv(dev);
  731. ethtool_cmd_speed_set(cmd, ndc->speed);
  732. cmd->duplex = ndc->duplex;
  733. cmd->port = PORT_OTHER;
  734. return 0;
  735. }
  736. static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  737. {
  738. struct net_device_context *ndc = netdev_priv(dev);
  739. u32 speed;
  740. speed = ethtool_cmd_speed(cmd);
  741. if (!ethtool_validate_speed(speed) ||
  742. !ethtool_validate_duplex(cmd->duplex) ||
  743. !netvsc_validate_ethtool_ss_cmd(cmd))
  744. return -EINVAL;
  745. ndc->speed = speed;
  746. ndc->duplex = cmd->duplex;
  747. return 0;
  748. }
  749. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  750. {
  751. struct net_device_context *ndevctx = netdev_priv(ndev);
  752. struct netvsc_device *nvdev = ndevctx->nvdev;
  753. struct hv_device *hdev = ndevctx->device_ctx;
  754. struct netvsc_device_info device_info;
  755. int limit = ETH_DATA_LEN;
  756. u32 num_chn;
  757. int ret = 0;
  758. if (ndevctx->start_remove || !nvdev || nvdev->destroy)
  759. return -ENODEV;
  760. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  761. limit = NETVSC_MTU - ETH_HLEN;
  762. if (mtu < NETVSC_MTU_MIN || mtu > limit)
  763. return -EINVAL;
  764. ret = netvsc_close(ndev);
  765. if (ret)
  766. goto out;
  767. num_chn = nvdev->num_chn;
  768. ndevctx->start_remove = true;
  769. rndis_filter_device_remove(hdev);
  770. ndev->mtu = mtu;
  771. memset(&device_info, 0, sizeof(device_info));
  772. device_info.ring_size = ring_size;
  773. device_info.num_chn = num_chn;
  774. device_info.max_num_vrss_chns = max_num_vrss_chns;
  775. rndis_filter_device_add(hdev, &device_info);
  776. out:
  777. netvsc_open(ndev);
  778. ndevctx->start_remove = false;
  779. /* We may have missed link change notifications */
  780. schedule_delayed_work(&ndevctx->dwork, 0);
  781. return ret;
  782. }
  783. static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
  784. struct rtnl_link_stats64 *t)
  785. {
  786. struct net_device_context *ndev_ctx = netdev_priv(net);
  787. int cpu;
  788. for_each_possible_cpu(cpu) {
  789. struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
  790. cpu);
  791. struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
  792. cpu);
  793. u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
  794. unsigned int start;
  795. do {
  796. start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
  797. tx_packets = tx_stats->packets;
  798. tx_bytes = tx_stats->bytes;
  799. } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
  800. do {
  801. start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
  802. rx_packets = rx_stats->packets;
  803. rx_bytes = rx_stats->bytes;
  804. } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
  805. t->tx_bytes += tx_bytes;
  806. t->tx_packets += tx_packets;
  807. t->rx_bytes += rx_bytes;
  808. t->rx_packets += rx_packets;
  809. }
  810. t->tx_dropped = net->stats.tx_dropped;
  811. t->tx_errors = net->stats.tx_dropped;
  812. t->rx_dropped = net->stats.rx_dropped;
  813. t->rx_errors = net->stats.rx_errors;
  814. return t;
  815. }
  816. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  817. {
  818. struct sockaddr *addr = p;
  819. char save_adr[ETH_ALEN];
  820. unsigned char save_aatype;
  821. int err;
  822. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  823. save_aatype = ndev->addr_assign_type;
  824. err = eth_mac_addr(ndev, p);
  825. if (err != 0)
  826. return err;
  827. err = rndis_filter_set_device_mac(ndev, addr->sa_data);
  828. if (err != 0) {
  829. /* roll back to saved MAC */
  830. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  831. ndev->addr_assign_type = save_aatype;
  832. }
  833. return err;
  834. }
  835. static const struct {
  836. char name[ETH_GSTRING_LEN];
  837. u16 offset;
  838. } netvsc_stats[] = {
  839. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  840. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  841. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  842. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  843. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  844. };
  845. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  846. {
  847. switch (string_set) {
  848. case ETH_SS_STATS:
  849. return ARRAY_SIZE(netvsc_stats);
  850. default:
  851. return -EINVAL;
  852. }
  853. }
  854. static void netvsc_get_ethtool_stats(struct net_device *dev,
  855. struct ethtool_stats *stats, u64 *data)
  856. {
  857. struct net_device_context *ndc = netdev_priv(dev);
  858. const void *nds = &ndc->eth_stats;
  859. int i;
  860. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
  861. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  862. }
  863. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  864. {
  865. int i;
  866. switch (stringset) {
  867. case ETH_SS_STATS:
  868. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
  869. memcpy(data + i * ETH_GSTRING_LEN,
  870. netvsc_stats[i].name, ETH_GSTRING_LEN);
  871. break;
  872. }
  873. }
  874. #ifdef CONFIG_NET_POLL_CONTROLLER
  875. static void netvsc_poll_controller(struct net_device *net)
  876. {
  877. /* As netvsc_start_xmit() works synchronous we don't have to
  878. * trigger anything here.
  879. */
  880. }
  881. #endif
  882. static const struct ethtool_ops ethtool_ops = {
  883. .get_drvinfo = netvsc_get_drvinfo,
  884. .get_link = ethtool_op_get_link,
  885. .get_ethtool_stats = netvsc_get_ethtool_stats,
  886. .get_sset_count = netvsc_get_sset_count,
  887. .get_strings = netvsc_get_strings,
  888. .get_channels = netvsc_get_channels,
  889. .set_channels = netvsc_set_channels,
  890. .get_ts_info = ethtool_op_get_ts_info,
  891. .get_settings = netvsc_get_settings,
  892. .set_settings = netvsc_set_settings,
  893. };
  894. static const struct net_device_ops device_ops = {
  895. .ndo_open = netvsc_open,
  896. .ndo_stop = netvsc_close,
  897. .ndo_start_xmit = netvsc_start_xmit,
  898. .ndo_set_rx_mode = netvsc_set_multicast_list,
  899. .ndo_change_mtu = netvsc_change_mtu,
  900. .ndo_validate_addr = eth_validate_addr,
  901. .ndo_set_mac_address = netvsc_set_mac_addr,
  902. .ndo_select_queue = netvsc_select_queue,
  903. .ndo_get_stats64 = netvsc_get_stats64,
  904. #ifdef CONFIG_NET_POLL_CONTROLLER
  905. .ndo_poll_controller = netvsc_poll_controller,
  906. #endif
  907. };
  908. /*
  909. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  910. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  911. * present send GARP packet to network peers with netif_notify_peers().
  912. */
  913. static void netvsc_link_change(struct work_struct *w)
  914. {
  915. struct net_device_context *ndev_ctx =
  916. container_of(w, struct net_device_context, dwork.work);
  917. struct hv_device *device_obj = ndev_ctx->device_ctx;
  918. struct net_device *net = hv_get_drvdata(device_obj);
  919. struct netvsc_device *net_device;
  920. struct rndis_device *rdev;
  921. struct netvsc_reconfig *event = NULL;
  922. bool notify = false, reschedule = false;
  923. unsigned long flags, next_reconfig, delay;
  924. rtnl_lock();
  925. if (ndev_ctx->start_remove)
  926. goto out_unlock;
  927. net_device = ndev_ctx->nvdev;
  928. rdev = net_device->extension;
  929. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  930. if (time_is_after_jiffies(next_reconfig)) {
  931. /* link_watch only sends one notification with current state
  932. * per second, avoid doing reconfig more frequently. Handle
  933. * wrap around.
  934. */
  935. delay = next_reconfig - jiffies;
  936. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  937. schedule_delayed_work(&ndev_ctx->dwork, delay);
  938. goto out_unlock;
  939. }
  940. ndev_ctx->last_reconfig = jiffies;
  941. spin_lock_irqsave(&ndev_ctx->lock, flags);
  942. if (!list_empty(&ndev_ctx->reconfig_events)) {
  943. event = list_first_entry(&ndev_ctx->reconfig_events,
  944. struct netvsc_reconfig, list);
  945. list_del(&event->list);
  946. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  947. }
  948. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  949. if (!event)
  950. goto out_unlock;
  951. switch (event->event) {
  952. /* Only the following events are possible due to the check in
  953. * netvsc_linkstatus_callback()
  954. */
  955. case RNDIS_STATUS_MEDIA_CONNECT:
  956. if (rdev->link_state) {
  957. rdev->link_state = false;
  958. netif_carrier_on(net);
  959. netif_tx_wake_all_queues(net);
  960. } else {
  961. notify = true;
  962. }
  963. kfree(event);
  964. break;
  965. case RNDIS_STATUS_MEDIA_DISCONNECT:
  966. if (!rdev->link_state) {
  967. rdev->link_state = true;
  968. netif_carrier_off(net);
  969. netif_tx_stop_all_queues(net);
  970. }
  971. kfree(event);
  972. break;
  973. case RNDIS_STATUS_NETWORK_CHANGE:
  974. /* Only makes sense if carrier is present */
  975. if (!rdev->link_state) {
  976. rdev->link_state = true;
  977. netif_carrier_off(net);
  978. netif_tx_stop_all_queues(net);
  979. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  980. spin_lock_irqsave(&ndev_ctx->lock, flags);
  981. list_add(&event->list, &ndev_ctx->reconfig_events);
  982. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  983. reschedule = true;
  984. }
  985. break;
  986. }
  987. rtnl_unlock();
  988. if (notify)
  989. netdev_notify_peers(net);
  990. /* link_watch only sends one notification with current state per
  991. * second, handle next reconfig event in 2 seconds.
  992. */
  993. if (reschedule)
  994. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  995. return;
  996. out_unlock:
  997. rtnl_unlock();
  998. }
  999. static void netvsc_free_netdev(struct net_device *netdev)
  1000. {
  1001. struct net_device_context *net_device_ctx = netdev_priv(netdev);
  1002. free_percpu(net_device_ctx->tx_stats);
  1003. free_percpu(net_device_ctx->rx_stats);
  1004. free_netdev(netdev);
  1005. }
  1006. static struct net_device *get_netvsc_net_device(char *mac)
  1007. {
  1008. struct net_device *dev, *found = NULL;
  1009. ASSERT_RTNL();
  1010. for_each_netdev(&init_net, dev) {
  1011. if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) {
  1012. if (dev->netdev_ops != &device_ops)
  1013. continue;
  1014. found = dev;
  1015. break;
  1016. }
  1017. }
  1018. return found;
  1019. }
  1020. static int netvsc_register_vf(struct net_device *vf_netdev)
  1021. {
  1022. struct net_device *ndev;
  1023. struct net_device_context *net_device_ctx;
  1024. struct netvsc_device *netvsc_dev;
  1025. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1026. if (eth_ops == NULL || eth_ops == &ethtool_ops)
  1027. return NOTIFY_DONE;
  1028. /*
  1029. * We will use the MAC address to locate the synthetic interface to
  1030. * associate with the VF interface. If we don't find a matching
  1031. * synthetic interface, move on.
  1032. */
  1033. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1034. if (!ndev)
  1035. return NOTIFY_DONE;
  1036. net_device_ctx = netdev_priv(ndev);
  1037. netvsc_dev = net_device_ctx->nvdev;
  1038. if (!netvsc_dev || net_device_ctx->vf_netdev)
  1039. return NOTIFY_DONE;
  1040. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1041. /*
  1042. * Take a reference on the module.
  1043. */
  1044. try_module_get(THIS_MODULE);
  1045. net_device_ctx->vf_netdev = vf_netdev;
  1046. return NOTIFY_OK;
  1047. }
  1048. static void netvsc_inject_enable(struct net_device_context *net_device_ctx)
  1049. {
  1050. net_device_ctx->vf_inject = true;
  1051. }
  1052. static void netvsc_inject_disable(struct net_device_context *net_device_ctx)
  1053. {
  1054. net_device_ctx->vf_inject = false;
  1055. /* Wait for currently active users to drain out. */
  1056. while (atomic_read(&net_device_ctx->vf_use_cnt) != 0)
  1057. udelay(50);
  1058. }
  1059. static int netvsc_vf_up(struct net_device *vf_netdev)
  1060. {
  1061. struct net_device *ndev;
  1062. struct netvsc_device *netvsc_dev;
  1063. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1064. struct net_device_context *net_device_ctx;
  1065. if (eth_ops == &ethtool_ops)
  1066. return NOTIFY_DONE;
  1067. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1068. if (!ndev)
  1069. return NOTIFY_DONE;
  1070. net_device_ctx = netdev_priv(ndev);
  1071. netvsc_dev = net_device_ctx->nvdev;
  1072. if (!netvsc_dev || !net_device_ctx->vf_netdev)
  1073. return NOTIFY_DONE;
  1074. netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
  1075. netvsc_inject_enable(net_device_ctx);
  1076. /*
  1077. * Open the device before switching data path.
  1078. */
  1079. rndis_filter_open(netvsc_dev);
  1080. /*
  1081. * notify the host to switch the data path.
  1082. */
  1083. netvsc_switch_datapath(ndev, true);
  1084. netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
  1085. netif_carrier_off(ndev);
  1086. /* Now notify peers through VF device. */
  1087. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
  1088. return NOTIFY_OK;
  1089. }
  1090. static int netvsc_vf_down(struct net_device *vf_netdev)
  1091. {
  1092. struct net_device *ndev;
  1093. struct netvsc_device *netvsc_dev;
  1094. struct net_device_context *net_device_ctx;
  1095. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1096. if (eth_ops == &ethtool_ops)
  1097. return NOTIFY_DONE;
  1098. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1099. if (!ndev)
  1100. return NOTIFY_DONE;
  1101. net_device_ctx = netdev_priv(ndev);
  1102. netvsc_dev = net_device_ctx->nvdev;
  1103. if (!netvsc_dev || !net_device_ctx->vf_netdev)
  1104. return NOTIFY_DONE;
  1105. netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
  1106. netvsc_inject_disable(net_device_ctx);
  1107. netvsc_switch_datapath(ndev, false);
  1108. netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
  1109. rndis_filter_close(netvsc_dev);
  1110. netif_carrier_on(ndev);
  1111. /* Now notify peers through netvsc device. */
  1112. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
  1113. return NOTIFY_OK;
  1114. }
  1115. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1116. {
  1117. struct net_device *ndev;
  1118. struct netvsc_device *netvsc_dev;
  1119. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1120. struct net_device_context *net_device_ctx;
  1121. if (eth_ops == &ethtool_ops)
  1122. return NOTIFY_DONE;
  1123. ndev = get_netvsc_net_device(vf_netdev->dev_addr);
  1124. if (!ndev)
  1125. return NOTIFY_DONE;
  1126. net_device_ctx = netdev_priv(ndev);
  1127. netvsc_dev = net_device_ctx->nvdev;
  1128. if (!netvsc_dev || !net_device_ctx->vf_netdev)
  1129. return NOTIFY_DONE;
  1130. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1131. netvsc_inject_disable(net_device_ctx);
  1132. net_device_ctx->vf_netdev = NULL;
  1133. module_put(THIS_MODULE);
  1134. return NOTIFY_OK;
  1135. }
  1136. static int netvsc_probe(struct hv_device *dev,
  1137. const struct hv_vmbus_device_id *dev_id)
  1138. {
  1139. struct net_device *net = NULL;
  1140. struct net_device_context *net_device_ctx;
  1141. struct netvsc_device_info device_info;
  1142. struct netvsc_device *nvdev;
  1143. int ret;
  1144. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1145. num_online_cpus());
  1146. if (!net)
  1147. return -ENOMEM;
  1148. netif_carrier_off(net);
  1149. netvsc_init_settings(net);
  1150. net_device_ctx = netdev_priv(net);
  1151. net_device_ctx->device_ctx = dev;
  1152. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1153. if (netif_msg_probe(net_device_ctx))
  1154. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1155. net_device_ctx->msg_enable);
  1156. net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1157. if (!net_device_ctx->tx_stats) {
  1158. free_netdev(net);
  1159. return -ENOMEM;
  1160. }
  1161. net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1162. if (!net_device_ctx->rx_stats) {
  1163. free_percpu(net_device_ctx->tx_stats);
  1164. free_netdev(net);
  1165. return -ENOMEM;
  1166. }
  1167. hv_set_drvdata(dev, net);
  1168. net_device_ctx->start_remove = false;
  1169. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1170. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  1171. spin_lock_init(&net_device_ctx->lock);
  1172. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1173. atomic_set(&net_device_ctx->vf_use_cnt, 0);
  1174. net_device_ctx->vf_netdev = NULL;
  1175. net_device_ctx->vf_inject = false;
  1176. net->netdev_ops = &device_ops;
  1177. net->hw_features = NETVSC_HW_FEATURES;
  1178. net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
  1179. net->ethtool_ops = &ethtool_ops;
  1180. SET_NETDEV_DEV(net, &dev->device);
  1181. /* We always need headroom for rndis header */
  1182. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1183. /* Notify the netvsc driver of the new device */
  1184. memset(&device_info, 0, sizeof(device_info));
  1185. device_info.ring_size = ring_size;
  1186. device_info.max_num_vrss_chns = max_num_vrss_chns;
  1187. ret = rndis_filter_device_add(dev, &device_info);
  1188. if (ret != 0) {
  1189. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1190. netvsc_free_netdev(net);
  1191. hv_set_drvdata(dev, NULL);
  1192. return ret;
  1193. }
  1194. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1195. nvdev = net_device_ctx->nvdev;
  1196. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  1197. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  1198. ret = register_netdev(net);
  1199. if (ret != 0) {
  1200. pr_err("Unable to register netdev.\n");
  1201. rndis_filter_device_remove(dev);
  1202. netvsc_free_netdev(net);
  1203. }
  1204. return ret;
  1205. }
  1206. static int netvsc_remove(struct hv_device *dev)
  1207. {
  1208. struct net_device *net;
  1209. struct net_device_context *ndev_ctx;
  1210. struct netvsc_device *net_device;
  1211. net = hv_get_drvdata(dev);
  1212. if (net == NULL) {
  1213. dev_err(&dev->device, "No net device to remove\n");
  1214. return 0;
  1215. }
  1216. ndev_ctx = netdev_priv(net);
  1217. net_device = ndev_ctx->nvdev;
  1218. /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
  1219. * removing the device.
  1220. */
  1221. rtnl_lock();
  1222. ndev_ctx->start_remove = true;
  1223. rtnl_unlock();
  1224. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1225. cancel_work_sync(&ndev_ctx->work);
  1226. /* Stop outbound asap */
  1227. netif_tx_disable(net);
  1228. unregister_netdev(net);
  1229. /*
  1230. * Call to the vsc driver to let it know that the device is being
  1231. * removed
  1232. */
  1233. rndis_filter_device_remove(dev);
  1234. hv_set_drvdata(dev, NULL);
  1235. netvsc_free_netdev(net);
  1236. return 0;
  1237. }
  1238. static const struct hv_vmbus_device_id id_table[] = {
  1239. /* Network guid */
  1240. { HV_NIC_GUID, },
  1241. { },
  1242. };
  1243. MODULE_DEVICE_TABLE(vmbus, id_table);
  1244. /* The one and only one */
  1245. static struct hv_driver netvsc_drv = {
  1246. .name = KBUILD_MODNAME,
  1247. .id_table = id_table,
  1248. .probe = netvsc_probe,
  1249. .remove = netvsc_remove,
  1250. };
  1251. /*
  1252. * On Hyper-V, every VF interface is matched with a corresponding
  1253. * synthetic interface. The synthetic interface is presented first
  1254. * to the guest. When the corresponding VF instance is registered,
  1255. * we will take care of switching the data path.
  1256. */
  1257. static int netvsc_netdev_event(struct notifier_block *this,
  1258. unsigned long event, void *ptr)
  1259. {
  1260. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1261. /* Avoid Vlan dev with same MAC registering as VF */
  1262. if (event_dev->priv_flags & IFF_802_1Q_VLAN)
  1263. return NOTIFY_DONE;
  1264. /* Avoid Bonding master dev with same MAC registering as VF */
  1265. if (event_dev->priv_flags & IFF_BONDING &&
  1266. event_dev->flags & IFF_MASTER)
  1267. return NOTIFY_DONE;
  1268. switch (event) {
  1269. case NETDEV_REGISTER:
  1270. return netvsc_register_vf(event_dev);
  1271. case NETDEV_UNREGISTER:
  1272. return netvsc_unregister_vf(event_dev);
  1273. case NETDEV_UP:
  1274. return netvsc_vf_up(event_dev);
  1275. case NETDEV_DOWN:
  1276. return netvsc_vf_down(event_dev);
  1277. default:
  1278. return NOTIFY_DONE;
  1279. }
  1280. }
  1281. static struct notifier_block netvsc_netdev_notifier = {
  1282. .notifier_call = netvsc_netdev_event,
  1283. };
  1284. static void __exit netvsc_drv_exit(void)
  1285. {
  1286. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1287. vmbus_driver_unregister(&netvsc_drv);
  1288. }
  1289. static int __init netvsc_drv_init(void)
  1290. {
  1291. int ret;
  1292. if (ring_size < RING_SIZE_MIN) {
  1293. ring_size = RING_SIZE_MIN;
  1294. pr_info("Increased ring_size to %d (min allowed)\n",
  1295. ring_size);
  1296. }
  1297. ret = vmbus_driver_register(&netvsc_drv);
  1298. if (ret)
  1299. return ret;
  1300. register_netdevice_notifier(&netvsc_netdev_notifier);
  1301. return 0;
  1302. }
  1303. MODULE_LICENSE("GPL");
  1304. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1305. module_init(netvsc_drv_init);
  1306. module_exit(netvsc_drv_exit);