netvsc_drv.c 28 KB

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