netvsc_drv.c 43 KB

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