netvsc_drv.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. struct net_device_context {
  41. /* point back to our device context */
  42. struct hv_device *device_ctx;
  43. struct delayed_work dwork;
  44. struct work_struct work;
  45. };
  46. #define RING_SIZE_MIN 64
  47. static int ring_size = 128;
  48. module_param(ring_size, int, S_IRUGO);
  49. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  50. static void do_set_multicast(struct work_struct *w)
  51. {
  52. struct net_device_context *ndevctx =
  53. container_of(w, struct net_device_context, work);
  54. struct netvsc_device *nvdev;
  55. struct rndis_device *rdev;
  56. nvdev = hv_get_drvdata(ndevctx->device_ctx);
  57. if (nvdev == NULL || nvdev->ndev == NULL)
  58. return;
  59. rdev = nvdev->extension;
  60. if (rdev == NULL)
  61. return;
  62. if (nvdev->ndev->flags & IFF_PROMISC)
  63. rndis_filter_set_packet_filter(rdev,
  64. NDIS_PACKET_TYPE_PROMISCUOUS);
  65. else
  66. rndis_filter_set_packet_filter(rdev,
  67. NDIS_PACKET_TYPE_BROADCAST |
  68. NDIS_PACKET_TYPE_ALL_MULTICAST |
  69. NDIS_PACKET_TYPE_DIRECTED);
  70. }
  71. static void netvsc_set_multicast_list(struct net_device *net)
  72. {
  73. struct net_device_context *net_device_ctx = netdev_priv(net);
  74. schedule_work(&net_device_ctx->work);
  75. }
  76. static int netvsc_open(struct net_device *net)
  77. {
  78. struct net_device_context *net_device_ctx = netdev_priv(net);
  79. struct hv_device *device_obj = net_device_ctx->device_ctx;
  80. struct netvsc_device *nvdev;
  81. struct rndis_device *rdev;
  82. int ret = 0;
  83. netif_carrier_off(net);
  84. /* Open up the device */
  85. ret = rndis_filter_open(device_obj);
  86. if (ret != 0) {
  87. netdev_err(net, "unable to open device (ret %d).\n", ret);
  88. return ret;
  89. }
  90. netif_tx_start_all_queues(net);
  91. nvdev = hv_get_drvdata(device_obj);
  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 hv_device *device_obj = net_device_ctx->device_ctx;
  101. int ret;
  102. netif_tx_disable(net);
  103. /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
  104. cancel_work_sync(&net_device_ctx->work);
  105. ret = rndis_filter_close(device_obj);
  106. if (ret != 0)
  107. netdev_err(net, "unable to close device (ret %d).\n", ret);
  108. return ret;
  109. }
  110. static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
  111. int pkt_type)
  112. {
  113. struct rndis_packet *rndis_pkt;
  114. struct rndis_per_packet_info *ppi;
  115. rndis_pkt = &msg->msg.pkt;
  116. rndis_pkt->data_offset += ppi_size;
  117. ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
  118. rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
  119. ppi->size = ppi_size;
  120. ppi->type = pkt_type;
  121. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  122. rndis_pkt->per_pkt_info_len += ppi_size;
  123. return ppi;
  124. }
  125. union sub_key {
  126. u64 k;
  127. struct {
  128. u8 pad[3];
  129. u8 kb;
  130. u32 ka;
  131. };
  132. };
  133. /* Toeplitz hash function
  134. * data: network byte order
  135. * return: host byte order
  136. */
  137. static u32 comp_hash(u8 *key, int klen, void *data, int dlen)
  138. {
  139. union sub_key subk;
  140. int k_next = 4;
  141. u8 dt;
  142. int i, j;
  143. u32 ret = 0;
  144. subk.k = 0;
  145. subk.ka = ntohl(*(u32 *)key);
  146. for (i = 0; i < dlen; i++) {
  147. subk.kb = key[k_next];
  148. k_next = (k_next + 1) % klen;
  149. dt = ((u8 *)data)[i];
  150. for (j = 0; j < 8; j++) {
  151. if (dt & 0x80)
  152. ret ^= subk.ka;
  153. dt <<= 1;
  154. subk.k <<= 1;
  155. }
  156. }
  157. return ret;
  158. }
  159. static bool netvsc_set_hash(u32 *hash, struct sk_buff *skb)
  160. {
  161. struct flow_keys flow;
  162. int data_len;
  163. if (!skb_flow_dissect(skb, &flow) ||
  164. !(flow.n_proto == htons(ETH_P_IP) ||
  165. flow.n_proto == htons(ETH_P_IPV6)))
  166. return false;
  167. if (flow.ip_proto == IPPROTO_TCP)
  168. data_len = 12;
  169. else
  170. data_len = 8;
  171. *hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
  172. return true;
  173. }
  174. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  175. void *accel_priv, select_queue_fallback_t fallback)
  176. {
  177. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  178. struct hv_device *hdev = net_device_ctx->device_ctx;
  179. struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
  180. u32 hash;
  181. u16 q_idx = 0;
  182. if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
  183. return 0;
  184. if (netvsc_set_hash(&hash, skb)) {
  185. q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
  186. ndev->real_num_tx_queues;
  187. skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
  188. }
  189. return q_idx;
  190. }
  191. void netvsc_xmit_completion(void *context)
  192. {
  193. struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
  194. struct sk_buff *skb = (struct sk_buff *)
  195. (unsigned long)packet->send_completion_tid;
  196. if (!packet->part_of_skb)
  197. kfree(packet);
  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 head_room;
  325. u32 pkt_sz;
  326. struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
  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. head_room = skb_headroom(skb);
  335. num_data_pgs = netvsc_get_slots(skb) + 2;
  336. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
  337. net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
  338. num_data_pgs, skb->len);
  339. ret = -EFAULT;
  340. goto drop;
  341. } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  342. if (skb_linearize(skb)) {
  343. net_alert_ratelimited("failed to linearize skb\n");
  344. ret = -ENOMEM;
  345. goto drop;
  346. }
  347. linear = true;
  348. goto check_size;
  349. }
  350. pkt_sz = sizeof(struct hv_netvsc_packet) + RNDIS_AND_PPI_SIZE;
  351. if (head_room < pkt_sz) {
  352. packet = kmalloc(pkt_sz, GFP_ATOMIC);
  353. if (!packet) {
  354. /* out of memory, drop packet */
  355. netdev_err(net, "unable to alloc hv_netvsc_packet\n");
  356. ret = -ENOMEM;
  357. goto drop;
  358. }
  359. packet->part_of_skb = false;
  360. } else {
  361. /* Use the headroom for building up the packet */
  362. packet = (struct hv_netvsc_packet *)skb->head;
  363. packet->part_of_skb = true;
  364. }
  365. packet->status = 0;
  366. packet->xmit_more = skb->xmit_more;
  367. packet->vlan_tci = skb->vlan_tci;
  368. packet->page_buf = page_buf;
  369. packet->q_idx = skb_get_queue_mapping(skb);
  370. packet->is_data_pkt = true;
  371. packet->total_data_buflen = skb->len;
  372. packet->rndis_msg = (struct rndis_message *)((unsigned long)packet +
  373. sizeof(struct hv_netvsc_packet));
  374. memset(packet->rndis_msg, 0, RNDIS_AND_PPI_SIZE);
  375. /* Set the completion routine */
  376. packet->send_completion = netvsc_xmit_completion;
  377. packet->send_completion_ctx = packet;
  378. packet->send_completion_tid = (unsigned long)skb;
  379. isvlan = packet->vlan_tci & VLAN_TAG_PRESENT;
  380. /* Add the rndis header */
  381. rndis_msg = packet->rndis_msg;
  382. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  383. rndis_msg->msg_len = packet->total_data_buflen;
  384. rndis_pkt = &rndis_msg->msg.pkt;
  385. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  386. rndis_pkt->data_len = packet->total_data_buflen;
  387. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  388. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  389. hash = skb_get_hash_raw(skb);
  390. if (hash != 0 && net->real_num_tx_queues > 1) {
  391. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  392. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  393. NBL_HASH_VALUE);
  394. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  395. }
  396. if (isvlan) {
  397. struct ndis_pkt_8021q_info *vlan;
  398. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  399. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  400. IEEE_8021Q_INFO);
  401. vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
  402. ppi->ppi_offset);
  403. vlan->vlanid = packet->vlan_tci & VLAN_VID_MASK;
  404. vlan->pri = (packet->vlan_tci & VLAN_PRIO_MASK) >>
  405. VLAN_PRIO_SHIFT;
  406. }
  407. net_trans_info = get_net_transport_info(skb, &hdr_offset);
  408. if (net_trans_info == TRANSPORT_INFO_NOT_IP)
  409. goto do_send;
  410. /*
  411. * Setup the sendside checksum offload only if this is not a
  412. * GSO packet.
  413. */
  414. if (skb_is_gso(skb))
  415. goto do_lso;
  416. if ((skb->ip_summed == CHECKSUM_NONE) ||
  417. (skb->ip_summed == CHECKSUM_UNNECESSARY))
  418. goto do_send;
  419. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  420. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  421. TCPIP_CHKSUM_PKTINFO);
  422. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  423. ppi->ppi_offset);
  424. if (net_trans_info & (INFO_IPV4 << 16))
  425. csum_info->transmit.is_ipv4 = 1;
  426. else
  427. csum_info->transmit.is_ipv6 = 1;
  428. if (net_trans_info & INFO_TCP) {
  429. csum_info->transmit.tcp_checksum = 1;
  430. csum_info->transmit.tcp_header_offset = hdr_offset;
  431. } else if (net_trans_info & INFO_UDP) {
  432. /* UDP checksum offload is not supported on ws2008r2.
  433. * Furthermore, on ws2012 and ws2012r2, there are some
  434. * issues with udp checksum offload from Linux guests.
  435. * (these are host issues).
  436. * For now compute the checksum here.
  437. */
  438. struct udphdr *uh;
  439. u16 udp_len;
  440. ret = skb_cow_head(skb, 0);
  441. if (ret)
  442. goto drop;
  443. uh = udp_hdr(skb);
  444. udp_len = ntohs(uh->len);
  445. uh->check = 0;
  446. uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
  447. ip_hdr(skb)->daddr,
  448. udp_len, IPPROTO_UDP,
  449. csum_partial(uh, udp_len, 0));
  450. if (uh->check == 0)
  451. uh->check = CSUM_MANGLED_0;
  452. csum_info->transmit.udp_checksum = 0;
  453. }
  454. goto do_send;
  455. do_lso:
  456. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  457. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  458. TCP_LARGESEND_PKTINFO);
  459. lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
  460. ppi->ppi_offset);
  461. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  462. if (net_trans_info & (INFO_IPV4 << 16)) {
  463. lso_info->lso_v2_transmit.ip_version =
  464. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  465. ip_hdr(skb)->tot_len = 0;
  466. ip_hdr(skb)->check = 0;
  467. tcp_hdr(skb)->check =
  468. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  469. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  470. } else {
  471. lso_info->lso_v2_transmit.ip_version =
  472. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  473. ipv6_hdr(skb)->payload_len = 0;
  474. tcp_hdr(skb)->check =
  475. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  476. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  477. }
  478. lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
  479. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  480. do_send:
  481. /* Start filling in the page buffers with the rndis hdr */
  482. rndis_msg->msg_len += rndis_msg_size;
  483. packet->total_data_buflen = rndis_msg->msg_len;
  484. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  485. skb, packet);
  486. ret = netvsc_send(net_device_ctx->device_ctx, packet);
  487. drop:
  488. if (ret == 0) {
  489. net->stats.tx_bytes += skb_length;
  490. net->stats.tx_packets++;
  491. } else {
  492. if (packet && !packet->part_of_skb)
  493. kfree(packet);
  494. if (ret != -EAGAIN) {
  495. dev_kfree_skb_any(skb);
  496. net->stats.tx_dropped++;
  497. }
  498. }
  499. return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  500. }
  501. /*
  502. * netvsc_linkstatus_callback - Link up/down notification
  503. */
  504. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  505. struct rndis_message *resp)
  506. {
  507. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  508. struct net_device *net;
  509. struct net_device_context *ndev_ctx;
  510. struct netvsc_device *net_device;
  511. struct rndis_device *rdev;
  512. net_device = hv_get_drvdata(device_obj);
  513. rdev = net_device->extension;
  514. switch (indicate->status) {
  515. case RNDIS_STATUS_MEDIA_CONNECT:
  516. rdev->link_state = false;
  517. break;
  518. case RNDIS_STATUS_MEDIA_DISCONNECT:
  519. rdev->link_state = true;
  520. break;
  521. case RNDIS_STATUS_NETWORK_CHANGE:
  522. rdev->link_change = true;
  523. break;
  524. default:
  525. return;
  526. }
  527. net = net_device->ndev;
  528. if (!net || net->reg_state != NETREG_REGISTERED)
  529. return;
  530. ndev_ctx = netdev_priv(net);
  531. if (!rdev->link_state) {
  532. schedule_delayed_work(&ndev_ctx->dwork, 0);
  533. schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
  534. } else {
  535. schedule_delayed_work(&ndev_ctx->dwork, 0);
  536. }
  537. }
  538. /*
  539. * netvsc_recv_callback - Callback when we receive a packet from the
  540. * "wire" on the specified device.
  541. */
  542. int netvsc_recv_callback(struct hv_device *device_obj,
  543. struct hv_netvsc_packet *packet,
  544. struct ndis_tcp_ip_checksum_info *csum_info)
  545. {
  546. struct net_device *net;
  547. struct sk_buff *skb;
  548. net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
  549. if (!net || net->reg_state != NETREG_REGISTERED) {
  550. packet->status = NVSP_STAT_FAIL;
  551. return 0;
  552. }
  553. /* Allocate a skb - TODO direct I/O to pages? */
  554. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  555. if (unlikely(!skb)) {
  556. ++net->stats.rx_dropped;
  557. packet->status = NVSP_STAT_FAIL;
  558. return 0;
  559. }
  560. /*
  561. * Copy to skb. This copy is needed here since the memory pointed by
  562. * hv_netvsc_packet cannot be deallocated
  563. */
  564. memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
  565. packet->total_data_buflen);
  566. skb->protocol = eth_type_trans(skb, net);
  567. if (csum_info) {
  568. /* We only look at the IP checksum here.
  569. * Should we be dropping the packet if checksum
  570. * failed? How do we deal with other checksums - TCP/UDP?
  571. */
  572. if (csum_info->receive.ip_checksum_succeeded)
  573. skb->ip_summed = CHECKSUM_UNNECESSARY;
  574. else
  575. skb->ip_summed = CHECKSUM_NONE;
  576. }
  577. if (packet->vlan_tci & VLAN_TAG_PRESENT)
  578. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  579. packet->vlan_tci);
  580. skb_record_rx_queue(skb, packet->channel->
  581. offermsg.offer.sub_channel_index);
  582. net->stats.rx_packets++;
  583. net->stats.rx_bytes += packet->total_data_buflen;
  584. /*
  585. * Pass the skb back up. Network stack will deallocate the skb when it
  586. * is done.
  587. * TODO - use NAPI?
  588. */
  589. netif_rx(skb);
  590. return 0;
  591. }
  592. static void netvsc_get_drvinfo(struct net_device *net,
  593. struct ethtool_drvinfo *info)
  594. {
  595. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  596. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  597. }
  598. static void netvsc_get_channels(struct net_device *net,
  599. struct ethtool_channels *channel)
  600. {
  601. struct net_device_context *net_device_ctx = netdev_priv(net);
  602. struct hv_device *dev = net_device_ctx->device_ctx;
  603. struct netvsc_device *nvdev = hv_get_drvdata(dev);
  604. if (nvdev) {
  605. channel->max_combined = nvdev->max_chn;
  606. channel->combined_count = nvdev->num_chn;
  607. }
  608. }
  609. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  610. {
  611. struct net_device_context *ndevctx = netdev_priv(ndev);
  612. struct hv_device *hdev = ndevctx->device_ctx;
  613. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  614. struct netvsc_device_info device_info;
  615. int limit = ETH_DATA_LEN;
  616. if (nvdev == NULL || nvdev->destroy)
  617. return -ENODEV;
  618. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  619. limit = NETVSC_MTU - ETH_HLEN;
  620. /* Hyper-V hosts don't support MTU < ETH_DATA_LEN (1500) */
  621. if (mtu < ETH_DATA_LEN || mtu > limit)
  622. return -EINVAL;
  623. nvdev->start_remove = true;
  624. cancel_work_sync(&ndevctx->work);
  625. netif_tx_disable(ndev);
  626. rndis_filter_device_remove(hdev);
  627. ndev->mtu = mtu;
  628. ndevctx->device_ctx = hdev;
  629. hv_set_drvdata(hdev, ndev);
  630. device_info.ring_size = ring_size;
  631. rndis_filter_device_add(hdev, &device_info);
  632. netif_tx_wake_all_queues(ndev);
  633. return 0;
  634. }
  635. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  636. {
  637. struct net_device_context *ndevctx = netdev_priv(ndev);
  638. struct hv_device *hdev = ndevctx->device_ctx;
  639. struct sockaddr *addr = p;
  640. char save_adr[ETH_ALEN];
  641. unsigned char save_aatype;
  642. int err;
  643. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  644. save_aatype = ndev->addr_assign_type;
  645. err = eth_mac_addr(ndev, p);
  646. if (err != 0)
  647. return err;
  648. err = rndis_filter_set_device_mac(hdev, addr->sa_data);
  649. if (err != 0) {
  650. /* roll back to saved MAC */
  651. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  652. ndev->addr_assign_type = save_aatype;
  653. }
  654. return err;
  655. }
  656. #ifdef CONFIG_NET_POLL_CONTROLLER
  657. static void netvsc_poll_controller(struct net_device *net)
  658. {
  659. /* As netvsc_start_xmit() works synchronous we don't have to
  660. * trigger anything here.
  661. */
  662. }
  663. #endif
  664. static const struct ethtool_ops ethtool_ops = {
  665. .get_drvinfo = netvsc_get_drvinfo,
  666. .get_link = ethtool_op_get_link,
  667. .get_channels = netvsc_get_channels,
  668. };
  669. static const struct net_device_ops device_ops = {
  670. .ndo_open = netvsc_open,
  671. .ndo_stop = netvsc_close,
  672. .ndo_start_xmit = netvsc_start_xmit,
  673. .ndo_set_rx_mode = netvsc_set_multicast_list,
  674. .ndo_change_mtu = netvsc_change_mtu,
  675. .ndo_validate_addr = eth_validate_addr,
  676. .ndo_set_mac_address = netvsc_set_mac_addr,
  677. .ndo_select_queue = netvsc_select_queue,
  678. #ifdef CONFIG_NET_POLL_CONTROLLER
  679. .ndo_poll_controller = netvsc_poll_controller,
  680. #endif
  681. };
  682. /*
  683. * Send GARP packet to network peers after migrations.
  684. * After Quick Migration, the network is not immediately operational in the
  685. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  686. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  687. * will not be sent after quick migration, and cause network disconnection.
  688. * Also, we update the carrier status here.
  689. */
  690. static void netvsc_link_change(struct work_struct *w)
  691. {
  692. struct net_device_context *ndev_ctx;
  693. struct net_device *net;
  694. struct netvsc_device *net_device;
  695. struct rndis_device *rdev;
  696. bool notify, refresh = false;
  697. char *argv[] = { "/etc/init.d/network", "restart", NULL };
  698. char *envp[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  699. rtnl_lock();
  700. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  701. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  702. rdev = net_device->extension;
  703. net = net_device->ndev;
  704. if (rdev->link_state) {
  705. netif_carrier_off(net);
  706. notify = false;
  707. } else {
  708. netif_carrier_on(net);
  709. notify = true;
  710. if (rdev->link_change) {
  711. rdev->link_change = false;
  712. refresh = true;
  713. }
  714. }
  715. rtnl_unlock();
  716. if (refresh)
  717. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  718. if (notify)
  719. netdev_notify_peers(net);
  720. }
  721. static int netvsc_probe(struct hv_device *dev,
  722. const struct hv_vmbus_device_id *dev_id)
  723. {
  724. struct net_device *net = NULL;
  725. struct net_device_context *net_device_ctx;
  726. struct netvsc_device_info device_info;
  727. struct netvsc_device *nvdev;
  728. int ret;
  729. u32 max_needed_headroom;
  730. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  731. num_online_cpus());
  732. if (!net)
  733. return -ENOMEM;
  734. max_needed_headroom = sizeof(struct hv_netvsc_packet) +
  735. RNDIS_AND_PPI_SIZE;
  736. netif_carrier_off(net);
  737. net_device_ctx = netdev_priv(net);
  738. net_device_ctx->device_ctx = dev;
  739. hv_set_drvdata(dev, net);
  740. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  741. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  742. net->netdev_ops = &device_ops;
  743. net->hw_features = NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_IP_CSUM |
  744. NETIF_F_TSO;
  745. net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_RXCSUM |
  746. NETIF_F_IP_CSUM | NETIF_F_TSO;
  747. net->ethtool_ops = &ethtool_ops;
  748. SET_NETDEV_DEV(net, &dev->device);
  749. /*
  750. * Request additional head room in the skb.
  751. * We will use this space to build the rndis
  752. * heaser and other state we need to maintain.
  753. */
  754. net->needed_headroom = max_needed_headroom;
  755. /* Notify the netvsc driver of the new device */
  756. device_info.ring_size = ring_size;
  757. ret = rndis_filter_device_add(dev, &device_info);
  758. if (ret != 0) {
  759. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  760. free_netdev(net);
  761. hv_set_drvdata(dev, NULL);
  762. return ret;
  763. }
  764. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  765. nvdev = hv_get_drvdata(dev);
  766. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  767. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  768. ret = register_netdev(net);
  769. if (ret != 0) {
  770. pr_err("Unable to register netdev.\n");
  771. rndis_filter_device_remove(dev);
  772. free_netdev(net);
  773. } else {
  774. schedule_delayed_work(&net_device_ctx->dwork, 0);
  775. }
  776. return ret;
  777. }
  778. static int netvsc_remove(struct hv_device *dev)
  779. {
  780. struct net_device *net;
  781. struct net_device_context *ndev_ctx;
  782. struct netvsc_device *net_device;
  783. net_device = hv_get_drvdata(dev);
  784. net = net_device->ndev;
  785. if (net == NULL) {
  786. dev_err(&dev->device, "No net device to remove\n");
  787. return 0;
  788. }
  789. net_device->start_remove = true;
  790. ndev_ctx = netdev_priv(net);
  791. cancel_delayed_work_sync(&ndev_ctx->dwork);
  792. cancel_work_sync(&ndev_ctx->work);
  793. /* Stop outbound asap */
  794. netif_tx_disable(net);
  795. unregister_netdev(net);
  796. /*
  797. * Call to the vsc driver to let it know that the device is being
  798. * removed
  799. */
  800. rndis_filter_device_remove(dev);
  801. free_netdev(net);
  802. return 0;
  803. }
  804. static const struct hv_vmbus_device_id id_table[] = {
  805. /* Network guid */
  806. { HV_NIC_GUID, },
  807. { },
  808. };
  809. MODULE_DEVICE_TABLE(vmbus, id_table);
  810. /* The one and only one */
  811. static struct hv_driver netvsc_drv = {
  812. .name = KBUILD_MODNAME,
  813. .id_table = id_table,
  814. .probe = netvsc_probe,
  815. .remove = netvsc_remove,
  816. };
  817. static void __exit netvsc_drv_exit(void)
  818. {
  819. vmbus_driver_unregister(&netvsc_drv);
  820. }
  821. static int __init netvsc_drv_init(void)
  822. {
  823. if (ring_size < RING_SIZE_MIN) {
  824. ring_size = RING_SIZE_MIN;
  825. pr_info("Increased ring_size to %d (min allowed)\n",
  826. ring_size);
  827. }
  828. return vmbus_driver_register(&netvsc_drv);
  829. }
  830. MODULE_LICENSE("GPL");
  831. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  832. module_init(netvsc_drv_init);
  833. module_exit(netvsc_drv_exit);