netvsc_drv.c 41 KB

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