netvsc_drv.c 29 KB

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