netvsc_drv.c 25 KB

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