netvsc_drv.c 25 KB

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