netvsc_drv.c 27 KB

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