netvsc_drv.c 23 KB

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