netvsc_drv.c 40 KB

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