netvsc_drv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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) || flow.n_proto != htons(ETH_P_IP))
  164. return false;
  165. if (flow.ip_proto == IPPROTO_TCP)
  166. data_len = 12;
  167. else
  168. data_len = 8;
  169. *hash = comp_hash(netvsc_hash_key, HASH_KEYLEN, &flow, data_len);
  170. return true;
  171. }
  172. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  173. void *accel_priv, select_queue_fallback_t fallback)
  174. {
  175. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  176. struct hv_device *hdev = net_device_ctx->device_ctx;
  177. struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
  178. u32 hash;
  179. u16 q_idx = 0;
  180. if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
  181. return 0;
  182. if (netvsc_set_hash(&hash, skb)) {
  183. q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
  184. ndev->real_num_tx_queues;
  185. skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
  186. }
  187. return q_idx;
  188. }
  189. static void netvsc_xmit_completion(void *context)
  190. {
  191. struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
  192. struct sk_buff *skb = (struct sk_buff *)
  193. (unsigned long)packet->send_completion_tid;
  194. u32 index = packet->send_buf_index;
  195. kfree(packet);
  196. if (skb && (index == NETVSC_INVALID_INDEX))
  197. dev_kfree_skb_any(skb);
  198. }
  199. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  200. struct hv_page_buffer *pb)
  201. {
  202. int j = 0;
  203. /* Deal with compund pages by ignoring unused part
  204. * of the page.
  205. */
  206. page += (offset >> PAGE_SHIFT);
  207. offset &= ~PAGE_MASK;
  208. while (len > 0) {
  209. unsigned long bytes;
  210. bytes = PAGE_SIZE - offset;
  211. if (bytes > len)
  212. bytes = len;
  213. pb[j].pfn = page_to_pfn(page);
  214. pb[j].offset = offset;
  215. pb[j].len = bytes;
  216. offset += bytes;
  217. len -= bytes;
  218. if (offset == PAGE_SIZE && len) {
  219. page++;
  220. offset = 0;
  221. j++;
  222. }
  223. }
  224. return j + 1;
  225. }
  226. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  227. struct hv_page_buffer *pb)
  228. {
  229. u32 slots_used = 0;
  230. char *data = skb->data;
  231. int frags = skb_shinfo(skb)->nr_frags;
  232. int i;
  233. /* The packet is laid out thus:
  234. * 1. hdr
  235. * 2. skb linear data
  236. * 3. skb fragment data
  237. */
  238. if (hdr != NULL)
  239. slots_used += fill_pg_buf(virt_to_page(hdr),
  240. offset_in_page(hdr),
  241. len, &pb[slots_used]);
  242. slots_used += fill_pg_buf(virt_to_page(data),
  243. offset_in_page(data),
  244. skb_headlen(skb), &pb[slots_used]);
  245. for (i = 0; i < frags; i++) {
  246. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  247. slots_used += fill_pg_buf(skb_frag_page(frag),
  248. frag->page_offset,
  249. skb_frag_size(frag), &pb[slots_used]);
  250. }
  251. return slots_used;
  252. }
  253. static int count_skb_frag_slots(struct sk_buff *skb)
  254. {
  255. int i, frags = skb_shinfo(skb)->nr_frags;
  256. int pages = 0;
  257. for (i = 0; i < frags; i++) {
  258. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  259. unsigned long size = skb_frag_size(frag);
  260. unsigned long offset = frag->page_offset;
  261. /* Skip unused frames from start of page */
  262. offset &= ~PAGE_MASK;
  263. pages += PFN_UP(offset + size);
  264. }
  265. return pages;
  266. }
  267. static int netvsc_get_slots(struct sk_buff *skb)
  268. {
  269. char *data = skb->data;
  270. unsigned int offset = offset_in_page(data);
  271. unsigned int len = skb_headlen(skb);
  272. int slots;
  273. int frag_slots;
  274. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  275. frag_slots = count_skb_frag_slots(skb);
  276. return slots + frag_slots;
  277. }
  278. static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
  279. {
  280. u32 ret_val = TRANSPORT_INFO_NOT_IP;
  281. if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
  282. (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
  283. goto not_ip;
  284. }
  285. *trans_off = skb_transport_offset(skb);
  286. if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
  287. struct iphdr *iphdr = ip_hdr(skb);
  288. if (iphdr->protocol == IPPROTO_TCP)
  289. ret_val = TRANSPORT_INFO_IPV4_TCP;
  290. else if (iphdr->protocol == IPPROTO_UDP)
  291. ret_val = TRANSPORT_INFO_IPV4_UDP;
  292. } else {
  293. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  294. ret_val = TRANSPORT_INFO_IPV6_TCP;
  295. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
  296. ret_val = TRANSPORT_INFO_IPV6_UDP;
  297. }
  298. not_ip:
  299. return ret_val;
  300. }
  301. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  302. {
  303. struct net_device_context *net_device_ctx = netdev_priv(net);
  304. struct hv_netvsc_packet *packet;
  305. int ret;
  306. unsigned int num_data_pgs;
  307. struct rndis_message *rndis_msg;
  308. struct rndis_packet *rndis_pkt;
  309. u32 rndis_msg_size;
  310. bool isvlan;
  311. struct rndis_per_packet_info *ppi;
  312. struct ndis_tcp_ip_checksum_info *csum_info;
  313. struct ndis_tcp_lso_info *lso_info;
  314. int hdr_offset;
  315. u32 net_trans_info;
  316. u32 hash;
  317. u32 skb_length = skb->len;
  318. /* We will atmost need two pages to describe the rndis
  319. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  320. * of pages in a single packet.
  321. */
  322. num_data_pgs = netvsc_get_slots(skb) + 2;
  323. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  324. netdev_err(net, "Packet too big: %u\n", skb->len);
  325. dev_kfree_skb(skb);
  326. net->stats.tx_dropped++;
  327. return NETDEV_TX_OK;
  328. }
  329. /* Allocate a netvsc packet based on # of frags. */
  330. packet = kzalloc(sizeof(struct hv_netvsc_packet) +
  331. (num_data_pgs * sizeof(struct hv_page_buffer)) +
  332. sizeof(struct rndis_message) +
  333. NDIS_VLAN_PPI_SIZE + NDIS_CSUM_PPI_SIZE +
  334. NDIS_LSO_PPI_SIZE + NDIS_HASH_PPI_SIZE, GFP_ATOMIC);
  335. if (!packet) {
  336. /* out of memory, drop packet */
  337. netdev_err(net, "unable to allocate hv_netvsc_packet\n");
  338. dev_kfree_skb(skb);
  339. net->stats.tx_dropped++;
  340. return NETDEV_TX_OK;
  341. }
  342. packet->vlan_tci = skb->vlan_tci;
  343. packet->q_idx = skb_get_queue_mapping(skb);
  344. packet->is_data_pkt = true;
  345. packet->total_data_buflen = skb->len;
  346. packet->rndis_msg = (struct rndis_message *)((unsigned long)packet +
  347. sizeof(struct hv_netvsc_packet) +
  348. (num_data_pgs * sizeof(struct hv_page_buffer)));
  349. /* Set the completion routine */
  350. packet->send_completion = netvsc_xmit_completion;
  351. packet->send_completion_ctx = packet;
  352. packet->send_completion_tid = (unsigned long)skb;
  353. isvlan = packet->vlan_tci & VLAN_TAG_PRESENT;
  354. /* Add the rndis header */
  355. rndis_msg = packet->rndis_msg;
  356. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  357. rndis_msg->msg_len = packet->total_data_buflen;
  358. rndis_pkt = &rndis_msg->msg.pkt;
  359. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  360. rndis_pkt->data_len = packet->total_data_buflen;
  361. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  362. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  363. hash = skb_get_hash_raw(skb);
  364. if (hash != 0 && net->real_num_tx_queues > 1) {
  365. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  366. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  367. NBL_HASH_VALUE);
  368. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  369. }
  370. if (isvlan) {
  371. struct ndis_pkt_8021q_info *vlan;
  372. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  373. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  374. IEEE_8021Q_INFO);
  375. vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
  376. ppi->ppi_offset);
  377. vlan->vlanid = packet->vlan_tci & VLAN_VID_MASK;
  378. vlan->pri = (packet->vlan_tci & VLAN_PRIO_MASK) >>
  379. VLAN_PRIO_SHIFT;
  380. }
  381. net_trans_info = get_net_transport_info(skb, &hdr_offset);
  382. if (net_trans_info == TRANSPORT_INFO_NOT_IP)
  383. goto do_send;
  384. /*
  385. * Setup the sendside checksum offload only if this is not a
  386. * GSO packet.
  387. */
  388. if (skb_is_gso(skb))
  389. goto do_lso;
  390. if ((skb->ip_summed == CHECKSUM_NONE) ||
  391. (skb->ip_summed == CHECKSUM_UNNECESSARY))
  392. goto do_send;
  393. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  394. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  395. TCPIP_CHKSUM_PKTINFO);
  396. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  397. ppi->ppi_offset);
  398. if (net_trans_info & (INFO_IPV4 << 16))
  399. csum_info->transmit.is_ipv4 = 1;
  400. else
  401. csum_info->transmit.is_ipv6 = 1;
  402. if (net_trans_info & INFO_TCP) {
  403. csum_info->transmit.tcp_checksum = 1;
  404. csum_info->transmit.tcp_header_offset = hdr_offset;
  405. } else if (net_trans_info & INFO_UDP) {
  406. /* UDP checksum offload is not supported on ws2008r2.
  407. * Furthermore, on ws2012 and ws2012r2, there are some
  408. * issues with udp checksum offload from Linux guests.
  409. * (these are host issues).
  410. * For now compute the checksum here.
  411. */
  412. struct udphdr *uh;
  413. u16 udp_len;
  414. ret = skb_cow_head(skb, 0);
  415. if (ret)
  416. goto drop;
  417. uh = udp_hdr(skb);
  418. udp_len = ntohs(uh->len);
  419. uh->check = 0;
  420. uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
  421. ip_hdr(skb)->daddr,
  422. udp_len, IPPROTO_UDP,
  423. csum_partial(uh, udp_len, 0));
  424. if (uh->check == 0)
  425. uh->check = CSUM_MANGLED_0;
  426. csum_info->transmit.udp_checksum = 0;
  427. }
  428. goto do_send;
  429. do_lso:
  430. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  431. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  432. TCP_LARGESEND_PKTINFO);
  433. lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
  434. ppi->ppi_offset);
  435. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  436. if (net_trans_info & (INFO_IPV4 << 16)) {
  437. lso_info->lso_v2_transmit.ip_version =
  438. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  439. ip_hdr(skb)->tot_len = 0;
  440. ip_hdr(skb)->check = 0;
  441. tcp_hdr(skb)->check =
  442. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  443. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  444. } else {
  445. lso_info->lso_v2_transmit.ip_version =
  446. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  447. ipv6_hdr(skb)->payload_len = 0;
  448. tcp_hdr(skb)->check =
  449. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  450. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  451. }
  452. lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
  453. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  454. do_send:
  455. /* Start filling in the page buffers with the rndis hdr */
  456. rndis_msg->msg_len += rndis_msg_size;
  457. packet->total_data_buflen = rndis_msg->msg_len;
  458. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  459. skb, &packet->page_buf[0]);
  460. ret = netvsc_send(net_device_ctx->device_ctx, packet);
  461. drop:
  462. if (ret == 0) {
  463. net->stats.tx_bytes += skb_length;
  464. net->stats.tx_packets++;
  465. } else {
  466. kfree(packet);
  467. if (ret != -EAGAIN) {
  468. dev_kfree_skb_any(skb);
  469. net->stats.tx_dropped++;
  470. }
  471. }
  472. return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  473. }
  474. /*
  475. * netvsc_linkstatus_callback - Link up/down notification
  476. */
  477. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  478. struct rndis_message *resp)
  479. {
  480. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  481. struct net_device *net;
  482. struct net_device_context *ndev_ctx;
  483. struct netvsc_device *net_device;
  484. struct rndis_device *rdev;
  485. net_device = hv_get_drvdata(device_obj);
  486. rdev = net_device->extension;
  487. switch (indicate->status) {
  488. case RNDIS_STATUS_MEDIA_CONNECT:
  489. rdev->link_state = false;
  490. break;
  491. case RNDIS_STATUS_MEDIA_DISCONNECT:
  492. rdev->link_state = true;
  493. break;
  494. case RNDIS_STATUS_NETWORK_CHANGE:
  495. rdev->link_change = true;
  496. break;
  497. default:
  498. return;
  499. }
  500. net = net_device->ndev;
  501. if (!net || net->reg_state != NETREG_REGISTERED)
  502. return;
  503. ndev_ctx = netdev_priv(net);
  504. if (!rdev->link_state) {
  505. schedule_delayed_work(&ndev_ctx->dwork, 0);
  506. schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
  507. } else {
  508. schedule_delayed_work(&ndev_ctx->dwork, 0);
  509. }
  510. }
  511. /*
  512. * netvsc_recv_callback - Callback when we receive a packet from the
  513. * "wire" on the specified device.
  514. */
  515. int netvsc_recv_callback(struct hv_device *device_obj,
  516. struct hv_netvsc_packet *packet,
  517. struct ndis_tcp_ip_checksum_info *csum_info)
  518. {
  519. struct net_device *net;
  520. struct sk_buff *skb;
  521. net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
  522. if (!net || net->reg_state != NETREG_REGISTERED) {
  523. packet->status = NVSP_STAT_FAIL;
  524. return 0;
  525. }
  526. /* Allocate a skb - TODO direct I/O to pages? */
  527. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  528. if (unlikely(!skb)) {
  529. ++net->stats.rx_dropped;
  530. packet->status = NVSP_STAT_FAIL;
  531. return 0;
  532. }
  533. /*
  534. * Copy to skb. This copy is needed here since the memory pointed by
  535. * hv_netvsc_packet cannot be deallocated
  536. */
  537. memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
  538. packet->total_data_buflen);
  539. skb->protocol = eth_type_trans(skb, net);
  540. if (csum_info) {
  541. /* We only look at the IP checksum here.
  542. * Should we be dropping the packet if checksum
  543. * failed? How do we deal with other checksums - TCP/UDP?
  544. */
  545. if (csum_info->receive.ip_checksum_succeeded)
  546. skb->ip_summed = CHECKSUM_UNNECESSARY;
  547. else
  548. skb->ip_summed = CHECKSUM_NONE;
  549. }
  550. if (packet->vlan_tci & VLAN_TAG_PRESENT)
  551. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  552. packet->vlan_tci);
  553. skb_record_rx_queue(skb, packet->channel->
  554. offermsg.offer.sub_channel_index);
  555. net->stats.rx_packets++;
  556. net->stats.rx_bytes += packet->total_data_buflen;
  557. /*
  558. * Pass the skb back up. Network stack will deallocate the skb when it
  559. * is done.
  560. * TODO - use NAPI?
  561. */
  562. netif_rx(skb);
  563. return 0;
  564. }
  565. static void netvsc_get_drvinfo(struct net_device *net,
  566. struct ethtool_drvinfo *info)
  567. {
  568. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  569. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  570. }
  571. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  572. {
  573. struct net_device_context *ndevctx = netdev_priv(ndev);
  574. struct hv_device *hdev = ndevctx->device_ctx;
  575. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  576. struct netvsc_device_info device_info;
  577. int limit = ETH_DATA_LEN;
  578. if (nvdev == NULL || nvdev->destroy)
  579. return -ENODEV;
  580. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  581. limit = NETVSC_MTU;
  582. if (mtu < 68 || mtu > limit)
  583. return -EINVAL;
  584. nvdev->start_remove = true;
  585. cancel_work_sync(&ndevctx->work);
  586. netif_tx_disable(ndev);
  587. rndis_filter_device_remove(hdev);
  588. ndev->mtu = mtu;
  589. ndevctx->device_ctx = hdev;
  590. hv_set_drvdata(hdev, ndev);
  591. device_info.ring_size = ring_size;
  592. rndis_filter_device_add(hdev, &device_info);
  593. netif_tx_wake_all_queues(ndev);
  594. return 0;
  595. }
  596. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  597. {
  598. struct net_device_context *ndevctx = netdev_priv(ndev);
  599. struct hv_device *hdev = ndevctx->device_ctx;
  600. struct sockaddr *addr = p;
  601. char save_adr[ETH_ALEN];
  602. unsigned char save_aatype;
  603. int err;
  604. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  605. save_aatype = ndev->addr_assign_type;
  606. err = eth_mac_addr(ndev, p);
  607. if (err != 0)
  608. return err;
  609. err = rndis_filter_set_device_mac(hdev, addr->sa_data);
  610. if (err != 0) {
  611. /* roll back to saved MAC */
  612. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  613. ndev->addr_assign_type = save_aatype;
  614. }
  615. return err;
  616. }
  617. #ifdef CONFIG_NET_POLL_CONTROLLER
  618. static void netvsc_poll_controller(struct net_device *net)
  619. {
  620. /* As netvsc_start_xmit() works synchronous we don't have to
  621. * trigger anything here.
  622. */
  623. }
  624. #endif
  625. static const struct ethtool_ops ethtool_ops = {
  626. .get_drvinfo = netvsc_get_drvinfo,
  627. .get_link = ethtool_op_get_link,
  628. };
  629. static const struct net_device_ops device_ops = {
  630. .ndo_open = netvsc_open,
  631. .ndo_stop = netvsc_close,
  632. .ndo_start_xmit = netvsc_start_xmit,
  633. .ndo_set_rx_mode = netvsc_set_multicast_list,
  634. .ndo_change_mtu = netvsc_change_mtu,
  635. .ndo_validate_addr = eth_validate_addr,
  636. .ndo_set_mac_address = netvsc_set_mac_addr,
  637. .ndo_select_queue = netvsc_select_queue,
  638. #ifdef CONFIG_NET_POLL_CONTROLLER
  639. .ndo_poll_controller = netvsc_poll_controller,
  640. #endif
  641. };
  642. /*
  643. * Send GARP packet to network peers after migrations.
  644. * After Quick Migration, the network is not immediately operational in the
  645. * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
  646. * another netif_notify_peers() into a delayed work, otherwise GARP packet
  647. * will not be sent after quick migration, and cause network disconnection.
  648. * Also, we update the carrier status here.
  649. */
  650. static void netvsc_link_change(struct work_struct *w)
  651. {
  652. struct net_device_context *ndev_ctx;
  653. struct net_device *net;
  654. struct netvsc_device *net_device;
  655. struct rndis_device *rdev;
  656. bool notify, refresh = false;
  657. char *argv[] = { "/etc/init.d/network", "restart", NULL };
  658. char *envp[] = { "HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  659. rtnl_lock();
  660. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  661. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  662. rdev = net_device->extension;
  663. net = net_device->ndev;
  664. if (rdev->link_state) {
  665. netif_carrier_off(net);
  666. notify = false;
  667. } else {
  668. netif_carrier_on(net);
  669. notify = true;
  670. if (rdev->link_change) {
  671. rdev->link_change = false;
  672. refresh = true;
  673. }
  674. }
  675. rtnl_unlock();
  676. if (refresh)
  677. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  678. if (notify)
  679. netdev_notify_peers(net);
  680. }
  681. static int netvsc_probe(struct hv_device *dev,
  682. const struct hv_vmbus_device_id *dev_id)
  683. {
  684. struct net_device *net = NULL;
  685. struct net_device_context *net_device_ctx;
  686. struct netvsc_device_info device_info;
  687. struct netvsc_device *nvdev;
  688. int ret;
  689. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  690. num_online_cpus());
  691. if (!net)
  692. return -ENOMEM;
  693. netif_carrier_off(net);
  694. net_device_ctx = netdev_priv(net);
  695. net_device_ctx->device_ctx = dev;
  696. hv_set_drvdata(dev, net);
  697. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  698. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  699. net->netdev_ops = &device_ops;
  700. net->hw_features = NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_IP_CSUM |
  701. NETIF_F_TSO;
  702. net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG | NETIF_F_RXCSUM |
  703. NETIF_F_IP_CSUM | NETIF_F_TSO;
  704. net->ethtool_ops = &ethtool_ops;
  705. SET_NETDEV_DEV(net, &dev->device);
  706. /* Notify the netvsc driver of the new device */
  707. device_info.ring_size = ring_size;
  708. ret = rndis_filter_device_add(dev, &device_info);
  709. if (ret != 0) {
  710. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  711. free_netdev(net);
  712. hv_set_drvdata(dev, NULL);
  713. return ret;
  714. }
  715. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  716. nvdev = hv_get_drvdata(dev);
  717. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  718. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  719. ret = register_netdev(net);
  720. if (ret != 0) {
  721. pr_err("Unable to register netdev.\n");
  722. rndis_filter_device_remove(dev);
  723. free_netdev(net);
  724. } else {
  725. schedule_delayed_work(&net_device_ctx->dwork, 0);
  726. }
  727. return ret;
  728. }
  729. static int netvsc_remove(struct hv_device *dev)
  730. {
  731. struct net_device *net;
  732. struct net_device_context *ndev_ctx;
  733. struct netvsc_device *net_device;
  734. net_device = hv_get_drvdata(dev);
  735. net = net_device->ndev;
  736. if (net == NULL) {
  737. dev_err(&dev->device, "No net device to remove\n");
  738. return 0;
  739. }
  740. net_device->start_remove = true;
  741. ndev_ctx = netdev_priv(net);
  742. cancel_delayed_work_sync(&ndev_ctx->dwork);
  743. cancel_work_sync(&ndev_ctx->work);
  744. /* Stop outbound asap */
  745. netif_tx_disable(net);
  746. unregister_netdev(net);
  747. /*
  748. * Call to the vsc driver to let it know that the device is being
  749. * removed
  750. */
  751. rndis_filter_device_remove(dev);
  752. free_netdev(net);
  753. return 0;
  754. }
  755. static const struct hv_vmbus_device_id id_table[] = {
  756. /* Network guid */
  757. { HV_NIC_GUID, },
  758. { },
  759. };
  760. MODULE_DEVICE_TABLE(vmbus, id_table);
  761. /* The one and only one */
  762. static struct hv_driver netvsc_drv = {
  763. .name = KBUILD_MODNAME,
  764. .id_table = id_table,
  765. .probe = netvsc_probe,
  766. .remove = netvsc_remove,
  767. };
  768. static void __exit netvsc_drv_exit(void)
  769. {
  770. vmbus_driver_unregister(&netvsc_drv);
  771. }
  772. static int __init netvsc_drv_init(void)
  773. {
  774. if (ring_size < RING_SIZE_MIN) {
  775. ring_size = RING_SIZE_MIN;
  776. pr_info("Increased ring_size to %d (min allowed)\n",
  777. ring_size);
  778. }
  779. return vmbus_driver_register(&netvsc_drv);
  780. }
  781. MODULE_LICENSE("GPL");
  782. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  783. module_init(netvsc_drv_init);
  784. module_exit(netvsc_drv_exit);