netvsc_drv.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. static int ring_size = 128;
  42. module_param(ring_size, int, S_IRUGO);
  43. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  44. static int max_num_vrss_chns = 8;
  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 netvsc_device *nvdev;
  57. struct rndis_device *rdev;
  58. nvdev = hv_get_drvdata(ndevctx->device_ctx);
  59. if (nvdev == NULL || nvdev->ndev == NULL)
  60. return;
  61. rdev = nvdev->extension;
  62. if (rdev == NULL)
  63. return;
  64. if (nvdev->ndev->flags & IFF_PROMISC)
  65. rndis_filter_set_packet_filter(rdev,
  66. NDIS_PACKET_TYPE_PROMISCUOUS);
  67. else
  68. rndis_filter_set_packet_filter(rdev,
  69. NDIS_PACKET_TYPE_BROADCAST |
  70. NDIS_PACKET_TYPE_ALL_MULTICAST |
  71. NDIS_PACKET_TYPE_DIRECTED);
  72. }
  73. static void netvsc_set_multicast_list(struct net_device *net)
  74. {
  75. struct net_device_context *net_device_ctx = netdev_priv(net);
  76. schedule_work(&net_device_ctx->work);
  77. }
  78. static int netvsc_open(struct net_device *net)
  79. {
  80. struct net_device_context *net_device_ctx = netdev_priv(net);
  81. struct hv_device *device_obj = net_device_ctx->device_ctx;
  82. struct netvsc_device *nvdev;
  83. struct rndis_device *rdev;
  84. int ret = 0;
  85. netif_carrier_off(net);
  86. /* Open up the device */
  87. ret = rndis_filter_open(device_obj);
  88. if (ret != 0) {
  89. netdev_err(net, "unable to open device (ret %d).\n", ret);
  90. return ret;
  91. }
  92. netif_tx_start_all_queues(net);
  93. nvdev = hv_get_drvdata(device_obj);
  94. rdev = nvdev->extension;
  95. if (!rdev->link_state)
  96. netif_carrier_on(net);
  97. return ret;
  98. }
  99. static int netvsc_close(struct net_device *net)
  100. {
  101. struct net_device_context *net_device_ctx = netdev_priv(net);
  102. struct hv_device *device_obj = net_device_ctx->device_ctx;
  103. int ret;
  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(device_obj);
  108. if (ret != 0)
  109. netdev_err(net, "unable to close device (ret %d).\n", ret);
  110. return ret;
  111. }
  112. static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
  113. int pkt_type)
  114. {
  115. struct rndis_packet *rndis_pkt;
  116. struct rndis_per_packet_info *ppi;
  117. rndis_pkt = &msg->msg.pkt;
  118. rndis_pkt->data_offset += ppi_size;
  119. ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
  120. rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
  121. ppi->size = ppi_size;
  122. ppi->type = pkt_type;
  123. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  124. rndis_pkt->per_pkt_info_len += ppi_size;
  125. return ppi;
  126. }
  127. union sub_key {
  128. u64 k;
  129. struct {
  130. u8 pad[3];
  131. u8 kb;
  132. u32 ka;
  133. };
  134. };
  135. /* Toeplitz hash function
  136. * data: network byte order
  137. * return: host byte order
  138. */
  139. static u32 comp_hash(u8 *key, int klen, void *data, int dlen)
  140. {
  141. union sub_key subk;
  142. int k_next = 4;
  143. u8 dt;
  144. int i, j;
  145. u32 ret = 0;
  146. subk.k = 0;
  147. subk.ka = ntohl(*(u32 *)key);
  148. for (i = 0; i < dlen; i++) {
  149. subk.kb = key[k_next];
  150. k_next = (k_next + 1) % klen;
  151. dt = ((u8 *)data)[i];
  152. for (j = 0; j < 8; j++) {
  153. if (dt & 0x80)
  154. ret ^= subk.ka;
  155. dt <<= 1;
  156. subk.k <<= 1;
  157. }
  158. }
  159. return ret;
  160. }
  161. static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)
  162. {
  163. struct flow_keys flow;
  164. int data_len;
  165. if (!skb_flow_dissect_flow_keys(skb, &flow) ||
  166. !(flow.basic.n_proto == htons(ETH_P_IP) ||
  167. flow.basic.n_proto == htons(ETH_P_IPV6)))
  168. return false;
  169. if (flow.basic.ip_proto == IPPROTO_TCP)
  170. data_len = 12;
  171. else
  172. data_len = 8;
  173. *hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
  174. return true;
  175. }
  176. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  177. void *accel_priv, select_queue_fallback_t fallback)
  178. {
  179. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  180. struct hv_device *hdev = net_device_ctx->device_ctx;
  181. struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
  182. u32 hash;
  183. u16 q_idx = 0;
  184. if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
  185. return 0;
  186. if (netvsc_set_hash(&hash, skb)) {
  187. q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
  188. ndev->real_num_tx_queues;
  189. skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
  190. }
  191. return q_idx;
  192. }
  193. void netvsc_xmit_completion(void *context)
  194. {
  195. struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
  196. struct sk_buff *skb = (struct sk_buff *)
  197. (unsigned long)packet->send_completion_tid;
  198. if (skb)
  199. dev_kfree_skb_any(skb);
  200. }
  201. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  202. struct hv_page_buffer *pb)
  203. {
  204. int j = 0;
  205. /* Deal with compund pages by ignoring unused part
  206. * of the page.
  207. */
  208. page += (offset >> PAGE_SHIFT);
  209. offset &= ~PAGE_MASK;
  210. while (len > 0) {
  211. unsigned long bytes;
  212. bytes = PAGE_SIZE - offset;
  213. if (bytes > len)
  214. bytes = len;
  215. pb[j].pfn = page_to_pfn(page);
  216. pb[j].offset = offset;
  217. pb[j].len = bytes;
  218. offset += bytes;
  219. len -= bytes;
  220. if (offset == PAGE_SIZE && len) {
  221. page++;
  222. offset = 0;
  223. j++;
  224. }
  225. }
  226. return j + 1;
  227. }
  228. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  229. struct hv_netvsc_packet *packet)
  230. {
  231. struct hv_page_buffer *pb = packet->page_buf;
  232. u32 slots_used = 0;
  233. char *data = skb->data;
  234. int frags = skb_shinfo(skb)->nr_frags;
  235. int i;
  236. /* The packet is laid out thus:
  237. * 1. hdr: RNDIS header and PPI
  238. * 2. skb linear data
  239. * 3. skb fragment data
  240. */
  241. if (hdr != NULL)
  242. slots_used += fill_pg_buf(virt_to_page(hdr),
  243. offset_in_page(hdr),
  244. len, &pb[slots_used]);
  245. packet->rmsg_size = len;
  246. packet->rmsg_pgcnt = slots_used;
  247. slots_used += fill_pg_buf(virt_to_page(data),
  248. offset_in_page(data),
  249. skb_headlen(skb), &pb[slots_used]);
  250. for (i = 0; i < frags; i++) {
  251. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  252. slots_used += fill_pg_buf(skb_frag_page(frag),
  253. frag->page_offset,
  254. skb_frag_size(frag), &pb[slots_used]);
  255. }
  256. return slots_used;
  257. }
  258. static int count_skb_frag_slots(struct sk_buff *skb)
  259. {
  260. int i, frags = skb_shinfo(skb)->nr_frags;
  261. int pages = 0;
  262. for (i = 0; i < frags; i++) {
  263. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  264. unsigned long size = skb_frag_size(frag);
  265. unsigned long offset = frag->page_offset;
  266. /* Skip unused frames from start of page */
  267. offset &= ~PAGE_MASK;
  268. pages += PFN_UP(offset + size);
  269. }
  270. return pages;
  271. }
  272. static int netvsc_get_slots(struct sk_buff *skb)
  273. {
  274. char *data = skb->data;
  275. unsigned int offset = offset_in_page(data);
  276. unsigned int len = skb_headlen(skb);
  277. int slots;
  278. int frag_slots;
  279. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  280. frag_slots = count_skb_frag_slots(skb);
  281. return slots + frag_slots;
  282. }
  283. static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
  284. {
  285. u32 ret_val = TRANSPORT_INFO_NOT_IP;
  286. if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
  287. (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
  288. goto not_ip;
  289. }
  290. *trans_off = skb_transport_offset(skb);
  291. if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
  292. struct iphdr *iphdr = ip_hdr(skb);
  293. if (iphdr->protocol == IPPROTO_TCP)
  294. ret_val = TRANSPORT_INFO_IPV4_TCP;
  295. else if (iphdr->protocol == IPPROTO_UDP)
  296. ret_val = TRANSPORT_INFO_IPV4_UDP;
  297. } else {
  298. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  299. ret_val = TRANSPORT_INFO_IPV6_TCP;
  300. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
  301. ret_val = TRANSPORT_INFO_IPV6_UDP;
  302. }
  303. not_ip:
  304. return ret_val;
  305. }
  306. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  307. {
  308. struct net_device_context *net_device_ctx = netdev_priv(net);
  309. struct hv_netvsc_packet *packet = NULL;
  310. int ret;
  311. unsigned int num_data_pgs;
  312. struct rndis_message *rndis_msg;
  313. struct rndis_packet *rndis_pkt;
  314. u32 rndis_msg_size;
  315. bool isvlan;
  316. bool linear = false;
  317. struct rndis_per_packet_info *ppi;
  318. struct ndis_tcp_ip_checksum_info *csum_info;
  319. struct ndis_tcp_lso_info *lso_info;
  320. int hdr_offset;
  321. u32 net_trans_info;
  322. u32 hash;
  323. u32 skb_length;
  324. u32 pkt_sz;
  325. struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
  326. struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
  327. /* We will atmost need two pages to describe the rndis
  328. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  329. * of pages in a single packet. If skb is scattered around
  330. * more pages we try linearizing it.
  331. */
  332. check_size:
  333. skb_length = skb->len;
  334. num_data_pgs = netvsc_get_slots(skb) + 2;
  335. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
  336. net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
  337. num_data_pgs, skb->len);
  338. ret = -EFAULT;
  339. goto drop;
  340. } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  341. if (skb_linearize(skb)) {
  342. net_alert_ratelimited("failed to linearize skb\n");
  343. ret = -ENOMEM;
  344. goto drop;
  345. }
  346. linear = true;
  347. goto check_size;
  348. }
  349. pkt_sz = sizeof(struct hv_netvsc_packet) + RNDIS_AND_PPI_SIZE;
  350. ret = skb_cow_head(skb, pkt_sz);
  351. if (ret) {
  352. netdev_err(net, "unable to alloc hv_netvsc_packet\n");
  353. ret = -ENOMEM;
  354. goto drop;
  355. }
  356. /* Use the headroom for building up the packet */
  357. packet = (struct hv_netvsc_packet *)skb->head;
  358. packet->status = 0;
  359. packet->xmit_more = skb->xmit_more;
  360. packet->vlan_tci = skb->vlan_tci;
  361. packet->page_buf = page_buf;
  362. packet->q_idx = skb_get_queue_mapping(skb);
  363. packet->is_data_pkt = true;
  364. packet->total_data_buflen = skb->len;
  365. packet->rndis_msg = (struct rndis_message *)((unsigned long)packet +
  366. sizeof(struct hv_netvsc_packet));
  367. memset(packet->rndis_msg, 0, RNDIS_AND_PPI_SIZE);
  368. /* Set the completion routine */
  369. packet->send_completion = netvsc_xmit_completion;
  370. packet->send_completion_ctx = packet;
  371. packet->send_completion_tid = (unsigned long)skb;
  372. isvlan = packet->vlan_tci & VLAN_TAG_PRESENT;
  373. /* Add the rndis header */
  374. rndis_msg = packet->rndis_msg;
  375. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  376. rndis_msg->msg_len = packet->total_data_buflen;
  377. rndis_pkt = &rndis_msg->msg.pkt;
  378. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  379. rndis_pkt->data_len = packet->total_data_buflen;
  380. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  381. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  382. hash = skb_get_hash_raw(skb);
  383. if (hash != 0 && net->real_num_tx_queues > 1) {
  384. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  385. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  386. NBL_HASH_VALUE);
  387. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  388. }
  389. if (isvlan) {
  390. struct ndis_pkt_8021q_info *vlan;
  391. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  392. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  393. IEEE_8021Q_INFO);
  394. vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
  395. ppi->ppi_offset);
  396. vlan->vlanid = packet->vlan_tci & VLAN_VID_MASK;
  397. vlan->pri = (packet->vlan_tci & VLAN_PRIO_MASK) >>
  398. VLAN_PRIO_SHIFT;
  399. }
  400. net_trans_info = get_net_transport_info(skb, &hdr_offset);
  401. if (net_trans_info == TRANSPORT_INFO_NOT_IP)
  402. goto do_send;
  403. /*
  404. * Setup the sendside checksum offload only if this is not a
  405. * GSO packet.
  406. */
  407. if (skb_is_gso(skb))
  408. goto do_lso;
  409. if ((skb->ip_summed == CHECKSUM_NONE) ||
  410. (skb->ip_summed == CHECKSUM_UNNECESSARY))
  411. goto do_send;
  412. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  413. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  414. TCPIP_CHKSUM_PKTINFO);
  415. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  416. ppi->ppi_offset);
  417. if (net_trans_info & (INFO_IPV4 << 16))
  418. csum_info->transmit.is_ipv4 = 1;
  419. else
  420. csum_info->transmit.is_ipv6 = 1;
  421. if (net_trans_info & INFO_TCP) {
  422. csum_info->transmit.tcp_checksum = 1;
  423. csum_info->transmit.tcp_header_offset = hdr_offset;
  424. } else if (net_trans_info & INFO_UDP) {
  425. /* UDP checksum offload is not supported on ws2008r2.
  426. * Furthermore, on ws2012 and ws2012r2, there are some
  427. * issues with udp checksum offload from Linux guests.
  428. * (these are host issues).
  429. * For now compute the checksum here.
  430. */
  431. struct udphdr *uh;
  432. u16 udp_len;
  433. ret = skb_cow_head(skb, 0);
  434. if (ret)
  435. goto drop;
  436. uh = udp_hdr(skb);
  437. udp_len = ntohs(uh->len);
  438. uh->check = 0;
  439. uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
  440. ip_hdr(skb)->daddr,
  441. udp_len, IPPROTO_UDP,
  442. csum_partial(uh, udp_len, 0));
  443. if (uh->check == 0)
  444. uh->check = CSUM_MANGLED_0;
  445. csum_info->transmit.udp_checksum = 0;
  446. }
  447. goto do_send;
  448. do_lso:
  449. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  450. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  451. TCP_LARGESEND_PKTINFO);
  452. lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
  453. ppi->ppi_offset);
  454. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  455. if (net_trans_info & (INFO_IPV4 << 16)) {
  456. lso_info->lso_v2_transmit.ip_version =
  457. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  458. ip_hdr(skb)->tot_len = 0;
  459. ip_hdr(skb)->check = 0;
  460. tcp_hdr(skb)->check =
  461. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  462. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  463. } else {
  464. lso_info->lso_v2_transmit.ip_version =
  465. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  466. ipv6_hdr(skb)->payload_len = 0;
  467. tcp_hdr(skb)->check =
  468. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  469. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  470. }
  471. lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
  472. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  473. do_send:
  474. /* Start filling in the page buffers with the rndis hdr */
  475. rndis_msg->msg_len += rndis_msg_size;
  476. packet->total_data_buflen = rndis_msg->msg_len;
  477. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  478. skb, packet);
  479. ret = netvsc_send(net_device_ctx->device_ctx, packet);
  480. drop:
  481. if (ret == 0) {
  482. u64_stats_update_begin(&tx_stats->syncp);
  483. tx_stats->packets++;
  484. tx_stats->bytes += skb_length;
  485. u64_stats_update_end(&tx_stats->syncp);
  486. } else {
  487. if (ret != -EAGAIN) {
  488. dev_kfree_skb_any(skb);
  489. net->stats.tx_dropped++;
  490. }
  491. }
  492. return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  493. }
  494. /*
  495. * netvsc_linkstatus_callback - Link up/down notification
  496. */
  497. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  498. struct rndis_message *resp)
  499. {
  500. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  501. struct net_device *net;
  502. struct net_device_context *ndev_ctx;
  503. struct netvsc_device *net_device;
  504. struct rndis_device *rdev;
  505. net_device = hv_get_drvdata(device_obj);
  506. rdev = net_device->extension;
  507. switch (indicate->status) {
  508. case RNDIS_STATUS_MEDIA_CONNECT:
  509. rdev->link_state = false;
  510. break;
  511. case RNDIS_STATUS_MEDIA_DISCONNECT:
  512. rdev->link_state = true;
  513. break;
  514. case RNDIS_STATUS_NETWORK_CHANGE:
  515. rdev->link_change = true;
  516. break;
  517. default:
  518. return;
  519. }
  520. net = net_device->ndev;
  521. if (!net || net->reg_state != NETREG_REGISTERED)
  522. return;
  523. ndev_ctx = netdev_priv(net);
  524. if (!rdev->link_state) {
  525. schedule_delayed_work(&ndev_ctx->dwork, 0);
  526. schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
  527. } else {
  528. schedule_delayed_work(&ndev_ctx->dwork, 0);
  529. }
  530. }
  531. /*
  532. * netvsc_recv_callback - Callback when we receive a packet from the
  533. * "wire" on the specified device.
  534. */
  535. int netvsc_recv_callback(struct hv_device *device_obj,
  536. struct hv_netvsc_packet *packet,
  537. struct ndis_tcp_ip_checksum_info *csum_info)
  538. {
  539. struct net_device *net;
  540. struct net_device_context *net_device_ctx;
  541. struct sk_buff *skb;
  542. struct netvsc_stats *rx_stats;
  543. net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
  544. if (!net || net->reg_state != NETREG_REGISTERED) {
  545. packet->status = NVSP_STAT_FAIL;
  546. return 0;
  547. }
  548. net_device_ctx = netdev_priv(net);
  549. rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
  550. /* Allocate a skb - TODO direct I/O to pages? */
  551. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  552. if (unlikely(!skb)) {
  553. ++net->stats.rx_dropped;
  554. packet->status = NVSP_STAT_FAIL;
  555. return 0;
  556. }
  557. /*
  558. * Copy to skb. This copy is needed here since the memory pointed by
  559. * hv_netvsc_packet cannot be deallocated
  560. */
  561. memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
  562. packet->total_data_buflen);
  563. skb->protocol = eth_type_trans(skb, net);
  564. if (csum_info) {
  565. /* We only look at the IP checksum here.
  566. * Should we be dropping the packet if checksum
  567. * failed? How do we deal with other checksums - TCP/UDP?
  568. */
  569. if (csum_info->receive.ip_checksum_succeeded)
  570. skb->ip_summed = CHECKSUM_UNNECESSARY;
  571. else
  572. skb->ip_summed = CHECKSUM_NONE;
  573. }
  574. if (packet->vlan_tci & VLAN_TAG_PRESENT)
  575. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  576. packet->vlan_tci);
  577. skb_record_rx_queue(skb, packet->channel->
  578. offermsg.offer.sub_channel_index);
  579. u64_stats_update_begin(&rx_stats->syncp);
  580. rx_stats->packets++;
  581. rx_stats->bytes += packet->total_data_buflen;
  582. u64_stats_update_end(&rx_stats->syncp);
  583. /*
  584. * Pass the skb back up. Network stack will deallocate the skb when it
  585. * is done.
  586. * TODO - use NAPI?
  587. */
  588. netif_rx(skb);
  589. return 0;
  590. }
  591. static void netvsc_get_drvinfo(struct net_device *net,
  592. struct ethtool_drvinfo *info)
  593. {
  594. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  595. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  596. }
  597. static void netvsc_get_channels(struct net_device *net,
  598. struct ethtool_channels *channel)
  599. {
  600. struct net_device_context *net_device_ctx = netdev_priv(net);
  601. struct hv_device *dev = net_device_ctx->device_ctx;
  602. struct netvsc_device *nvdev = hv_get_drvdata(dev);
  603. if (nvdev) {
  604. channel->max_combined = nvdev->max_chn;
  605. channel->combined_count = nvdev->num_chn;
  606. }
  607. }
  608. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  609. {
  610. struct net_device_context *ndevctx = netdev_priv(ndev);
  611. struct hv_device *hdev = ndevctx->device_ctx;
  612. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  613. struct netvsc_device_info device_info;
  614. int limit = ETH_DATA_LEN;
  615. if (nvdev == NULL || nvdev->destroy)
  616. return -ENODEV;
  617. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  618. limit = NETVSC_MTU - ETH_HLEN;
  619. /* Hyper-V hosts don't support MTU < ETH_DATA_LEN (1500) */
  620. if (mtu < ETH_DATA_LEN || mtu > limit)
  621. return -EINVAL;
  622. nvdev->start_remove = true;
  623. cancel_work_sync(&ndevctx->work);
  624. netif_tx_disable(ndev);
  625. rndis_filter_device_remove(hdev);
  626. ndev->mtu = mtu;
  627. ndevctx->device_ctx = hdev;
  628. hv_set_drvdata(hdev, ndev);
  629. device_info.ring_size = ring_size;
  630. device_info.max_num_vrss_chns = max_num_vrss_chns;
  631. rndis_filter_device_add(hdev, &device_info);
  632. netif_tx_wake_all_queues(ndev);
  633. return 0;
  634. }
  635. static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
  636. struct rtnl_link_stats64 *t)
  637. {
  638. struct net_device_context *ndev_ctx = netdev_priv(net);
  639. int cpu;
  640. for_each_possible_cpu(cpu) {
  641. struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
  642. cpu);
  643. struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
  644. cpu);
  645. u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
  646. unsigned int start;
  647. do {
  648. start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
  649. tx_packets = tx_stats->packets;
  650. tx_bytes = tx_stats->bytes;
  651. } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
  652. do {
  653. start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
  654. rx_packets = rx_stats->packets;
  655. rx_bytes = rx_stats->bytes;
  656. } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
  657. t->tx_bytes += tx_bytes;
  658. t->tx_packets += tx_packets;
  659. t->rx_bytes += rx_bytes;
  660. t->rx_packets += rx_packets;
  661. }
  662. t->tx_dropped = net->stats.tx_dropped;
  663. t->tx_errors = net->stats.tx_dropped;
  664. t->rx_dropped = net->stats.rx_dropped;
  665. t->rx_errors = net->stats.rx_errors;
  666. return t;
  667. }
  668. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  669. {
  670. struct net_device_context *ndevctx = netdev_priv(ndev);
  671. struct hv_device *hdev = ndevctx->device_ctx;
  672. struct sockaddr *addr = p;
  673. char save_adr[ETH_ALEN];
  674. unsigned char save_aatype;
  675. int err;
  676. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  677. save_aatype = ndev->addr_assign_type;
  678. err = eth_mac_addr(ndev, p);
  679. if (err != 0)
  680. return err;
  681. err = rndis_filter_set_device_mac(hdev, addr->sa_data);
  682. if (err != 0) {
  683. /* roll back to saved MAC */
  684. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  685. ndev->addr_assign_type = save_aatype;
  686. }
  687. return err;
  688. }
  689. #ifdef CONFIG_NET_POLL_CONTROLLER
  690. static void netvsc_poll_controller(struct net_device *net)
  691. {
  692. /* As netvsc_start_xmit() works synchronous we don't have to
  693. * trigger anything here.
  694. */
  695. }
  696. #endif
  697. static const struct ethtool_ops ethtool_ops = {
  698. .get_drvinfo = netvsc_get_drvinfo,
  699. .get_link = ethtool_op_get_link,
  700. .get_channels = netvsc_get_channels,
  701. };
  702. static const struct net_device_ops device_ops = {
  703. .ndo_open = netvsc_open,
  704. .ndo_stop = netvsc_close,
  705. .ndo_start_xmit = netvsc_start_xmit,
  706. .ndo_set_rx_mode = netvsc_set_multicast_list,
  707. .ndo_change_mtu = netvsc_change_mtu,
  708. .ndo_validate_addr = eth_validate_addr,
  709. .ndo_set_mac_address = netvsc_set_mac_addr,
  710. .ndo_select_queue = netvsc_select_queue,
  711. .ndo_get_stats64 = netvsc_get_stats64,
  712. #ifdef CONFIG_NET_POLL_CONTROLLER
  713. .ndo_poll_controller = netvsc_poll_controller,
  714. #endif
  715. };
  716. /*
  717. * Send GARP packet to network peers after migrations.
  718. * After Quick Migration, the network is not immediately operational in the
  719. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  720. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  721. * will not be sent after quick migration, and cause network disconnection.
  722. * Also, we update the carrier status here.
  723. */
  724. static void netvsc_link_change(struct work_struct *w)
  725. {
  726. struct net_device_context *ndev_ctx;
  727. struct net_device *net;
  728. struct netvsc_device *net_device;
  729. struct rndis_device *rdev;
  730. bool notify, refresh = false;
  731. char *argv[] = { "/etc/init.d/network", "restart", NULL };
  732. char *envp[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  733. rtnl_lock();
  734. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  735. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  736. rdev = net_device->extension;
  737. net = net_device->ndev;
  738. if (rdev->link_state) {
  739. netif_carrier_off(net);
  740. notify = false;
  741. } else {
  742. netif_carrier_on(net);
  743. notify = true;
  744. if (rdev->link_change) {
  745. rdev->link_change = false;
  746. refresh = true;
  747. }
  748. }
  749. rtnl_unlock();
  750. if (refresh)
  751. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  752. if (notify)
  753. netdev_notify_peers(net);
  754. }
  755. static void netvsc_free_netdev(struct net_device *netdev)
  756. {
  757. struct net_device_context *net_device_ctx = netdev_priv(netdev);
  758. free_percpu(net_device_ctx->tx_stats);
  759. free_percpu(net_device_ctx->rx_stats);
  760. free_netdev(netdev);
  761. }
  762. static int netvsc_probe(struct hv_device *dev,
  763. const struct hv_vmbus_device_id *dev_id)
  764. {
  765. struct net_device *net = NULL;
  766. struct net_device_context *net_device_ctx;
  767. struct netvsc_device_info device_info;
  768. struct netvsc_device *nvdev;
  769. int ret;
  770. u32 max_needed_headroom;
  771. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  772. num_online_cpus());
  773. if (!net)
  774. return -ENOMEM;
  775. max_needed_headroom = sizeof(struct hv_netvsc_packet) +
  776. RNDIS_AND_PPI_SIZE;
  777. netif_carrier_off(net);
  778. net_device_ctx = netdev_priv(net);
  779. net_device_ctx->device_ctx = dev;
  780. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  781. if (netif_msg_probe(net_device_ctx))
  782. netdev_dbg(net, "netvsc msg_enable: %d\n",
  783. net_device_ctx->msg_enable);
  784. net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  785. if (!net_device_ctx->tx_stats) {
  786. free_netdev(net);
  787. return -ENOMEM;
  788. }
  789. net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  790. if (!net_device_ctx->rx_stats) {
  791. free_percpu(net_device_ctx->tx_stats);
  792. free_netdev(net);
  793. return -ENOMEM;
  794. }
  795. hv_set_drvdata(dev, net);
  796. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  797. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  798. net->netdev_ops = &device_ops;
  799. net->hw_features = NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_IP_CSUM |
  800. NETIF_F_TSO;
  801. net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_RXCSUM |
  802. NETIF_F_IP_CSUM | NETIF_F_TSO;
  803. net->ethtool_ops = &ethtool_ops;
  804. SET_NETDEV_DEV(net, &dev->device);
  805. /*
  806. * Request additional head room in the skb.
  807. * We will use this space to build the rndis
  808. * heaser and other state we need to maintain.
  809. */
  810. net->needed_headroom = max_needed_headroom;
  811. /* Notify the netvsc driver of the new device */
  812. device_info.ring_size = ring_size;
  813. device_info.max_num_vrss_chns = max_num_vrss_chns;
  814. ret = rndis_filter_device_add(dev, &device_info);
  815. if (ret != 0) {
  816. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  817. netvsc_free_netdev(net);
  818. hv_set_drvdata(dev, NULL);
  819. return ret;
  820. }
  821. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  822. nvdev = hv_get_drvdata(dev);
  823. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  824. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  825. ret = register_netdev(net);
  826. if (ret != 0) {
  827. pr_err("Unable to register netdev.\n");
  828. rndis_filter_device_remove(dev);
  829. netvsc_free_netdev(net);
  830. } else {
  831. schedule_delayed_work(&net_device_ctx->dwork, 0);
  832. }
  833. return ret;
  834. }
  835. static int netvsc_remove(struct hv_device *dev)
  836. {
  837. struct net_device *net;
  838. struct net_device_context *ndev_ctx;
  839. struct netvsc_device *net_device;
  840. net_device = hv_get_drvdata(dev);
  841. net = net_device->ndev;
  842. if (net == NULL) {
  843. dev_err(&dev->device, "No net device to remove\n");
  844. return 0;
  845. }
  846. net_device->start_remove = true;
  847. ndev_ctx = netdev_priv(net);
  848. cancel_delayed_work_sync(&ndev_ctx->dwork);
  849. cancel_work_sync(&ndev_ctx->work);
  850. /* Stop outbound asap */
  851. netif_tx_disable(net);
  852. unregister_netdev(net);
  853. /*
  854. * Call to the vsc driver to let it know that the device is being
  855. * removed
  856. */
  857. rndis_filter_device_remove(dev);
  858. netvsc_free_netdev(net);
  859. return 0;
  860. }
  861. static const struct hv_vmbus_device_id id_table[] = {
  862. /* Network guid */
  863. { HV_NIC_GUID, },
  864. { },
  865. };
  866. MODULE_DEVICE_TABLE(vmbus, id_table);
  867. /* The one and only one */
  868. static struct hv_driver netvsc_drv = {
  869. .name = KBUILD_MODNAME,
  870. .id_table = id_table,
  871. .probe = netvsc_probe,
  872. .remove = netvsc_remove,
  873. };
  874. static void __exit netvsc_drv_exit(void)
  875. {
  876. vmbus_driver_unregister(&netvsc_drv);
  877. }
  878. static int __init netvsc_drv_init(void)
  879. {
  880. if (ring_size < RING_SIZE_MIN) {
  881. ring_size = RING_SIZE_MIN;
  882. pr_info("Increased ring_size to %d (min allowed)\n",
  883. ring_size);
  884. }
  885. return vmbus_driver_register(&netvsc_drv);
  886. }
  887. MODULE_LICENSE("GPL");
  888. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  889. module_init(netvsc_drv_init);
  890. module_exit(netvsc_drv_exit);