netvsc_drv.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. #define LINKCHANGE_INT (2 * HZ)
  42. #define NETVSC_HW_FEATURES (NETIF_F_RXCSUM | \
  43. NETIF_F_SG | \
  44. NETIF_F_TSO | \
  45. NETIF_F_TSO6 | \
  46. NETIF_F_HW_CSUM)
  47. static int ring_size = 128;
  48. module_param(ring_size, int, S_IRUGO);
  49. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  50. static int max_num_vrss_chns = 8;
  51. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  52. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  53. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  54. NETIF_MSG_TX_ERR;
  55. static int debug = -1;
  56. module_param(debug, int, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  58. static void do_set_multicast(struct work_struct *w)
  59. {
  60. struct net_device_context *ndevctx =
  61. container_of(w, struct net_device_context, work);
  62. struct netvsc_device *nvdev;
  63. struct rndis_device *rdev;
  64. nvdev = hv_get_drvdata(ndevctx->device_ctx);
  65. if (nvdev == NULL || nvdev->ndev == NULL)
  66. return;
  67. rdev = nvdev->extension;
  68. if (rdev == NULL)
  69. return;
  70. if (nvdev->ndev->flags & IFF_PROMISC)
  71. rndis_filter_set_packet_filter(rdev,
  72. NDIS_PACKET_TYPE_PROMISCUOUS);
  73. else
  74. rndis_filter_set_packet_filter(rdev,
  75. NDIS_PACKET_TYPE_BROADCAST |
  76. NDIS_PACKET_TYPE_ALL_MULTICAST |
  77. NDIS_PACKET_TYPE_DIRECTED);
  78. }
  79. static void netvsc_set_multicast_list(struct net_device *net)
  80. {
  81. struct net_device_context *net_device_ctx = netdev_priv(net);
  82. schedule_work(&net_device_ctx->work);
  83. }
  84. static int netvsc_open(struct net_device *net)
  85. {
  86. struct net_device_context *net_device_ctx = netdev_priv(net);
  87. struct hv_device *device_obj = net_device_ctx->device_ctx;
  88. struct netvsc_device *nvdev;
  89. struct rndis_device *rdev;
  90. int ret = 0;
  91. netif_carrier_off(net);
  92. /* Open up the device */
  93. ret = rndis_filter_open(device_obj);
  94. if (ret != 0) {
  95. netdev_err(net, "unable to open device (ret %d).\n", ret);
  96. return ret;
  97. }
  98. netif_tx_wake_all_queues(net);
  99. nvdev = hv_get_drvdata(device_obj);
  100. rdev = nvdev->extension;
  101. if (!rdev->link_state)
  102. netif_carrier_on(net);
  103. return ret;
  104. }
  105. static int netvsc_close(struct net_device *net)
  106. {
  107. struct net_device_context *net_device_ctx = netdev_priv(net);
  108. struct hv_device *device_obj = net_device_ctx->device_ctx;
  109. struct netvsc_device *nvdev = hv_get_drvdata(device_obj);
  110. int ret;
  111. u32 aread, awrite, i, msec = 10, retry = 0, retry_max = 20;
  112. struct vmbus_channel *chn;
  113. netif_tx_disable(net);
  114. /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
  115. cancel_work_sync(&net_device_ctx->work);
  116. ret = rndis_filter_close(device_obj);
  117. if (ret != 0) {
  118. netdev_err(net, "unable to close device (ret %d).\n", ret);
  119. return ret;
  120. }
  121. /* Ensure pending bytes in ring are read */
  122. while (true) {
  123. aread = 0;
  124. for (i = 0; i < nvdev->num_chn; i++) {
  125. chn = nvdev->chn_table[i];
  126. if (!chn)
  127. continue;
  128. hv_get_ringbuffer_availbytes(&chn->inbound, &aread,
  129. &awrite);
  130. if (aread)
  131. break;
  132. hv_get_ringbuffer_availbytes(&chn->outbound, &aread,
  133. &awrite);
  134. if (aread)
  135. break;
  136. }
  137. retry++;
  138. if (retry > retry_max || aread == 0)
  139. break;
  140. msleep(msec);
  141. if (msec < 1000)
  142. msec *= 2;
  143. }
  144. if (aread) {
  145. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  146. ret = -ETIMEDOUT;
  147. }
  148. return ret;
  149. }
  150. static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
  151. int pkt_type)
  152. {
  153. struct rndis_packet *rndis_pkt;
  154. struct rndis_per_packet_info *ppi;
  155. rndis_pkt = &msg->msg.pkt;
  156. rndis_pkt->data_offset += ppi_size;
  157. ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
  158. rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
  159. ppi->size = ppi_size;
  160. ppi->type = pkt_type;
  161. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  162. rndis_pkt->per_pkt_info_len += ppi_size;
  163. return ppi;
  164. }
  165. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  166. void *accel_priv, select_queue_fallback_t fallback)
  167. {
  168. struct net_device_context *net_device_ctx = netdev_priv(ndev);
  169. struct hv_device *hdev = net_device_ctx->device_ctx;
  170. struct netvsc_device *nvsc_dev = hv_get_drvdata(hdev);
  171. u32 hash;
  172. u16 q_idx = 0;
  173. if (nvsc_dev == NULL || ndev->real_num_tx_queues <= 1)
  174. return 0;
  175. hash = skb_get_hash(skb);
  176. q_idx = nvsc_dev->send_table[hash % VRSS_SEND_TAB_SIZE] %
  177. ndev->real_num_tx_queues;
  178. if (!nvsc_dev->chn_table[q_idx])
  179. q_idx = 0;
  180. return q_idx;
  181. }
  182. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  183. struct hv_page_buffer *pb)
  184. {
  185. int j = 0;
  186. /* Deal with compund pages by ignoring unused part
  187. * of the page.
  188. */
  189. page += (offset >> PAGE_SHIFT);
  190. offset &= ~PAGE_MASK;
  191. while (len > 0) {
  192. unsigned long bytes;
  193. bytes = PAGE_SIZE - offset;
  194. if (bytes > len)
  195. bytes = len;
  196. pb[j].pfn = page_to_pfn(page);
  197. pb[j].offset = offset;
  198. pb[j].len = bytes;
  199. offset += bytes;
  200. len -= bytes;
  201. if (offset == PAGE_SIZE && len) {
  202. page++;
  203. offset = 0;
  204. j++;
  205. }
  206. }
  207. return j + 1;
  208. }
  209. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  210. struct hv_netvsc_packet *packet,
  211. struct hv_page_buffer **page_buf)
  212. {
  213. struct hv_page_buffer *pb = *page_buf;
  214. u32 slots_used = 0;
  215. char *data = skb->data;
  216. int frags = skb_shinfo(skb)->nr_frags;
  217. int i;
  218. /* The packet is laid out thus:
  219. * 1. hdr: RNDIS header and PPI
  220. * 2. skb linear data
  221. * 3. skb fragment data
  222. */
  223. if (hdr != NULL)
  224. slots_used += fill_pg_buf(virt_to_page(hdr),
  225. offset_in_page(hdr),
  226. len, &pb[slots_used]);
  227. packet->rmsg_size = len;
  228. packet->rmsg_pgcnt = slots_used;
  229. slots_used += fill_pg_buf(virt_to_page(data),
  230. offset_in_page(data),
  231. skb_headlen(skb), &pb[slots_used]);
  232. for (i = 0; i < frags; i++) {
  233. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  234. slots_used += fill_pg_buf(skb_frag_page(frag),
  235. frag->page_offset,
  236. skb_frag_size(frag), &pb[slots_used]);
  237. }
  238. return slots_used;
  239. }
  240. static int count_skb_frag_slots(struct sk_buff *skb)
  241. {
  242. int i, frags = skb_shinfo(skb)->nr_frags;
  243. int pages = 0;
  244. for (i = 0; i < frags; i++) {
  245. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  246. unsigned long size = skb_frag_size(frag);
  247. unsigned long offset = frag->page_offset;
  248. /* Skip unused frames from start of page */
  249. offset &= ~PAGE_MASK;
  250. pages += PFN_UP(offset + size);
  251. }
  252. return pages;
  253. }
  254. static int netvsc_get_slots(struct sk_buff *skb)
  255. {
  256. char *data = skb->data;
  257. unsigned int offset = offset_in_page(data);
  258. unsigned int len = skb_headlen(skb);
  259. int slots;
  260. int frag_slots;
  261. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  262. frag_slots = count_skb_frag_slots(skb);
  263. return slots + frag_slots;
  264. }
  265. static u32 get_net_transport_info(struct sk_buff *skb, u32 *trans_off)
  266. {
  267. u32 ret_val = TRANSPORT_INFO_NOT_IP;
  268. if ((eth_hdr(skb)->h_proto != htons(ETH_P_IP)) &&
  269. (eth_hdr(skb)->h_proto != htons(ETH_P_IPV6))) {
  270. goto not_ip;
  271. }
  272. *trans_off = skb_transport_offset(skb);
  273. if ((eth_hdr(skb)->h_proto == htons(ETH_P_IP))) {
  274. struct iphdr *iphdr = ip_hdr(skb);
  275. if (iphdr->protocol == IPPROTO_TCP)
  276. ret_val = TRANSPORT_INFO_IPV4_TCP;
  277. else if (iphdr->protocol == IPPROTO_UDP)
  278. ret_val = TRANSPORT_INFO_IPV4_UDP;
  279. } else {
  280. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  281. ret_val = TRANSPORT_INFO_IPV6_TCP;
  282. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
  283. ret_val = TRANSPORT_INFO_IPV6_UDP;
  284. }
  285. not_ip:
  286. return ret_val;
  287. }
  288. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  289. {
  290. struct net_device_context *net_device_ctx = netdev_priv(net);
  291. struct hv_netvsc_packet *packet = NULL;
  292. int ret;
  293. unsigned int num_data_pgs;
  294. struct rndis_message *rndis_msg;
  295. struct rndis_packet *rndis_pkt;
  296. u32 rndis_msg_size;
  297. bool isvlan;
  298. bool linear = false;
  299. struct rndis_per_packet_info *ppi;
  300. struct ndis_tcp_ip_checksum_info *csum_info;
  301. struct ndis_tcp_lso_info *lso_info;
  302. int hdr_offset;
  303. u32 net_trans_info;
  304. u32 hash;
  305. u32 skb_length;
  306. struct hv_page_buffer page_buf[MAX_PAGE_BUFFER_COUNT];
  307. struct hv_page_buffer *pb = page_buf;
  308. struct netvsc_stats *tx_stats = this_cpu_ptr(net_device_ctx->tx_stats);
  309. /* We will atmost need two pages to describe the rndis
  310. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  311. * of pages in a single packet. If skb is scattered around
  312. * more pages we try linearizing it.
  313. */
  314. check_size:
  315. skb_length = skb->len;
  316. num_data_pgs = netvsc_get_slots(skb) + 2;
  317. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT && linear) {
  318. net_alert_ratelimited("packet too big: %u pages (%u bytes)\n",
  319. num_data_pgs, skb->len);
  320. ret = -EFAULT;
  321. goto drop;
  322. } else if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  323. if (skb_linearize(skb)) {
  324. net_alert_ratelimited("failed to linearize skb\n");
  325. ret = -ENOMEM;
  326. goto drop;
  327. }
  328. linear = true;
  329. goto check_size;
  330. }
  331. /*
  332. * Place the rndis header in the skb head room and
  333. * the skb->cb will be used for hv_netvsc_packet
  334. * structure.
  335. */
  336. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  337. if (ret) {
  338. netdev_err(net, "unable to alloc hv_netvsc_packet\n");
  339. ret = -ENOMEM;
  340. goto drop;
  341. }
  342. /* Use the skb control buffer for building up the packet */
  343. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  344. FIELD_SIZEOF(struct sk_buff, cb));
  345. packet = (struct hv_netvsc_packet *)skb->cb;
  346. packet->q_idx = skb_get_queue_mapping(skb);
  347. packet->total_data_buflen = skb->len;
  348. rndis_msg = (struct rndis_message *)skb->head;
  349. memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
  350. isvlan = skb->vlan_tci & VLAN_TAG_PRESENT;
  351. /* Add the rndis header */
  352. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  353. rndis_msg->msg_len = packet->total_data_buflen;
  354. rndis_pkt = &rndis_msg->msg.pkt;
  355. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  356. rndis_pkt->data_len = packet->total_data_buflen;
  357. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  358. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  359. hash = skb_get_hash_raw(skb);
  360. if (hash != 0 && net->real_num_tx_queues > 1) {
  361. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  362. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  363. NBL_HASH_VALUE);
  364. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  365. }
  366. if (isvlan) {
  367. struct ndis_pkt_8021q_info *vlan;
  368. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  369. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  370. IEEE_8021Q_INFO);
  371. vlan = (struct ndis_pkt_8021q_info *)((void *)ppi +
  372. ppi->ppi_offset);
  373. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  374. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  375. VLAN_PRIO_SHIFT;
  376. }
  377. net_trans_info = get_net_transport_info(skb, &hdr_offset);
  378. if (net_trans_info == TRANSPORT_INFO_NOT_IP)
  379. goto do_send;
  380. /*
  381. * Setup the sendside checksum offload only if this is not a
  382. * GSO packet.
  383. */
  384. if (skb_is_gso(skb))
  385. goto do_lso;
  386. if ((skb->ip_summed == CHECKSUM_NONE) ||
  387. (skb->ip_summed == CHECKSUM_UNNECESSARY))
  388. goto do_send;
  389. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  390. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  391. TCPIP_CHKSUM_PKTINFO);
  392. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  393. ppi->ppi_offset);
  394. if (net_trans_info & (INFO_IPV4 << 16))
  395. csum_info->transmit.is_ipv4 = 1;
  396. else
  397. csum_info->transmit.is_ipv6 = 1;
  398. if (net_trans_info & INFO_TCP) {
  399. csum_info->transmit.tcp_checksum = 1;
  400. csum_info->transmit.tcp_header_offset = hdr_offset;
  401. } else if (net_trans_info & INFO_UDP) {
  402. /* UDP checksum offload is not supported on ws2008r2.
  403. * Furthermore, on ws2012 and ws2012r2, there are some
  404. * issues with udp checksum offload from Linux guests.
  405. * (these are host issues).
  406. * For now compute the checksum here.
  407. */
  408. struct udphdr *uh;
  409. u16 udp_len;
  410. ret = skb_cow_head(skb, 0);
  411. if (ret)
  412. goto drop;
  413. uh = udp_hdr(skb);
  414. udp_len = ntohs(uh->len);
  415. uh->check = 0;
  416. uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr,
  417. ip_hdr(skb)->daddr,
  418. udp_len, IPPROTO_UDP,
  419. csum_partial(uh, udp_len, 0));
  420. if (uh->check == 0)
  421. uh->check = CSUM_MANGLED_0;
  422. csum_info->transmit.udp_checksum = 0;
  423. }
  424. goto do_send;
  425. do_lso:
  426. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  427. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  428. TCP_LARGESEND_PKTINFO);
  429. lso_info = (struct ndis_tcp_lso_info *)((void *)ppi +
  430. ppi->ppi_offset);
  431. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  432. if (net_trans_info & (INFO_IPV4 << 16)) {
  433. lso_info->lso_v2_transmit.ip_version =
  434. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  435. ip_hdr(skb)->tot_len = 0;
  436. ip_hdr(skb)->check = 0;
  437. tcp_hdr(skb)->check =
  438. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  439. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  440. } else {
  441. lso_info->lso_v2_transmit.ip_version =
  442. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  443. ipv6_hdr(skb)->payload_len = 0;
  444. tcp_hdr(skb)->check =
  445. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  446. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  447. }
  448. lso_info->lso_v2_transmit.tcp_header_offset = hdr_offset;
  449. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  450. do_send:
  451. /* Start filling in the page buffers with the rndis hdr */
  452. rndis_msg->msg_len += rndis_msg_size;
  453. packet->total_data_buflen = rndis_msg->msg_len;
  454. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  455. skb, packet, &pb);
  456. /* timestamp packet in software */
  457. skb_tx_timestamp(skb);
  458. ret = netvsc_send(net_device_ctx->device_ctx, packet,
  459. rndis_msg, &pb, skb);
  460. drop:
  461. if (ret == 0) {
  462. u64_stats_update_begin(&tx_stats->syncp);
  463. tx_stats->packets++;
  464. tx_stats->bytes += skb_length;
  465. u64_stats_update_end(&tx_stats->syncp);
  466. } else {
  467. if (ret != -EAGAIN) {
  468. dev_kfree_skb_any(skb);
  469. net->stats.tx_dropped++;
  470. }
  471. }
  472. return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
  473. }
  474. /*
  475. * netvsc_linkstatus_callback - Link up/down notification
  476. */
  477. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  478. struct rndis_message *resp)
  479. {
  480. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  481. struct net_device *net;
  482. struct net_device_context *ndev_ctx;
  483. struct netvsc_device *net_device;
  484. struct netvsc_reconfig *event;
  485. unsigned long flags;
  486. /* Handle link change statuses only */
  487. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  488. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  489. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  490. return;
  491. net_device = hv_get_drvdata(device_obj);
  492. net = net_device->ndev;
  493. if (!net || net->reg_state != NETREG_REGISTERED)
  494. return;
  495. ndev_ctx = netdev_priv(net);
  496. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  497. if (!event)
  498. return;
  499. event->event = indicate->status;
  500. spin_lock_irqsave(&ndev_ctx->lock, flags);
  501. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  502. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  503. schedule_delayed_work(&ndev_ctx->dwork, 0);
  504. }
  505. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  506. struct hv_netvsc_packet *packet,
  507. struct ndis_tcp_ip_checksum_info *csum_info,
  508. void *data, u16 vlan_tci)
  509. {
  510. struct sk_buff *skb;
  511. skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
  512. if (!skb)
  513. return skb;
  514. /*
  515. * Copy to skb. This copy is needed here since the memory pointed by
  516. * hv_netvsc_packet cannot be deallocated
  517. */
  518. memcpy(skb_put(skb, packet->total_data_buflen), data,
  519. packet->total_data_buflen);
  520. skb->protocol = eth_type_trans(skb, net);
  521. if (csum_info) {
  522. /* We only look at the IP checksum here.
  523. * Should we be dropping the packet if checksum
  524. * failed? How do we deal with other checksums - TCP/UDP?
  525. */
  526. if (csum_info->receive.ip_checksum_succeeded)
  527. skb->ip_summed = CHECKSUM_UNNECESSARY;
  528. else
  529. skb->ip_summed = CHECKSUM_NONE;
  530. }
  531. if (vlan_tci & VLAN_TAG_PRESENT)
  532. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  533. vlan_tci);
  534. return skb;
  535. }
  536. /*
  537. * netvsc_recv_callback - Callback when we receive a packet from the
  538. * "wire" on the specified device.
  539. */
  540. int netvsc_recv_callback(struct hv_device *device_obj,
  541. struct hv_netvsc_packet *packet,
  542. void **data,
  543. struct ndis_tcp_ip_checksum_info *csum_info,
  544. struct vmbus_channel *channel,
  545. u16 vlan_tci)
  546. {
  547. struct net_device *net;
  548. struct net_device_context *net_device_ctx;
  549. struct sk_buff *skb;
  550. struct sk_buff *vf_skb;
  551. struct netvsc_stats *rx_stats;
  552. struct netvsc_device *netvsc_dev = hv_get_drvdata(device_obj);
  553. u32 bytes_recvd = packet->total_data_buflen;
  554. int ret = 0;
  555. net = netvsc_dev->ndev;
  556. if (!net || net->reg_state != NETREG_REGISTERED)
  557. return NVSP_STAT_FAIL;
  558. if (READ_ONCE(netvsc_dev->vf_inject)) {
  559. atomic_inc(&netvsc_dev->vf_use_cnt);
  560. if (!READ_ONCE(netvsc_dev->vf_inject)) {
  561. /*
  562. * We raced; just move on.
  563. */
  564. atomic_dec(&netvsc_dev->vf_use_cnt);
  565. goto vf_injection_done;
  566. }
  567. /*
  568. * Inject this packet into the VF inerface.
  569. * On Hyper-V, multicast and brodcast packets
  570. * are only delivered on the synthetic interface
  571. * (after subjecting these to policy filters on
  572. * the host). Deliver these via the VF interface
  573. * in the guest.
  574. */
  575. vf_skb = netvsc_alloc_recv_skb(netvsc_dev->vf_netdev, packet,
  576. csum_info, *data, vlan_tci);
  577. if (vf_skb != NULL) {
  578. ++netvsc_dev->vf_netdev->stats.rx_packets;
  579. netvsc_dev->vf_netdev->stats.rx_bytes += bytes_recvd;
  580. netif_receive_skb(vf_skb);
  581. } else {
  582. ++net->stats.rx_dropped;
  583. ret = NVSP_STAT_FAIL;
  584. }
  585. atomic_dec(&netvsc_dev->vf_use_cnt);
  586. return ret;
  587. }
  588. vf_injection_done:
  589. net_device_ctx = netdev_priv(net);
  590. rx_stats = this_cpu_ptr(net_device_ctx->rx_stats);
  591. /* Allocate a skb - TODO direct I/O to pages? */
  592. skb = netvsc_alloc_recv_skb(net, packet, csum_info, *data, vlan_tci);
  593. if (unlikely(!skb)) {
  594. ++net->stats.rx_dropped;
  595. return NVSP_STAT_FAIL;
  596. }
  597. skb_record_rx_queue(skb, channel->
  598. offermsg.offer.sub_channel_index);
  599. u64_stats_update_begin(&rx_stats->syncp);
  600. rx_stats->packets++;
  601. rx_stats->bytes += packet->total_data_buflen;
  602. u64_stats_update_end(&rx_stats->syncp);
  603. /*
  604. * Pass the skb back up. Network stack will deallocate the skb when it
  605. * is done.
  606. * TODO - use NAPI?
  607. */
  608. netif_rx(skb);
  609. return 0;
  610. }
  611. static void netvsc_get_drvinfo(struct net_device *net,
  612. struct ethtool_drvinfo *info)
  613. {
  614. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  615. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  616. }
  617. static void netvsc_get_channels(struct net_device *net,
  618. struct ethtool_channels *channel)
  619. {
  620. struct net_device_context *net_device_ctx = netdev_priv(net);
  621. struct hv_device *dev = net_device_ctx->device_ctx;
  622. struct netvsc_device *nvdev = hv_get_drvdata(dev);
  623. if (nvdev) {
  624. channel->max_combined = nvdev->max_chn;
  625. channel->combined_count = nvdev->num_chn;
  626. }
  627. }
  628. static int netvsc_set_channels(struct net_device *net,
  629. struct ethtool_channels *channels)
  630. {
  631. struct net_device_context *net_device_ctx = netdev_priv(net);
  632. struct hv_device *dev = net_device_ctx->device_ctx;
  633. struct netvsc_device *nvdev = hv_get_drvdata(dev);
  634. struct netvsc_device_info device_info;
  635. u32 num_chn;
  636. u32 max_chn;
  637. int ret = 0;
  638. bool recovering = false;
  639. if (!nvdev || nvdev->destroy)
  640. return -ENODEV;
  641. num_chn = nvdev->num_chn;
  642. max_chn = min_t(u32, nvdev->max_chn, num_online_cpus());
  643. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) {
  644. pr_info("vRSS unsupported before NVSP Version 5\n");
  645. return -EINVAL;
  646. }
  647. /* We do not support rx, tx, or other */
  648. if (!channels ||
  649. channels->rx_count ||
  650. channels->tx_count ||
  651. channels->other_count ||
  652. (channels->combined_count < 1))
  653. return -EINVAL;
  654. if (channels->combined_count > max_chn) {
  655. pr_info("combined channels too high, using %d\n", max_chn);
  656. channels->combined_count = max_chn;
  657. }
  658. ret = netvsc_close(net);
  659. if (ret)
  660. goto out;
  661. do_set:
  662. nvdev->start_remove = true;
  663. rndis_filter_device_remove(dev);
  664. nvdev->num_chn = channels->combined_count;
  665. net_device_ctx->device_ctx = dev;
  666. hv_set_drvdata(dev, net);
  667. memset(&device_info, 0, sizeof(device_info));
  668. device_info.num_chn = nvdev->num_chn; /* passed to RNDIS */
  669. device_info.ring_size = ring_size;
  670. device_info.max_num_vrss_chns = max_num_vrss_chns;
  671. ret = rndis_filter_device_add(dev, &device_info);
  672. if (ret) {
  673. if (recovering) {
  674. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  675. return ret;
  676. }
  677. goto recover;
  678. }
  679. nvdev = hv_get_drvdata(dev);
  680. ret = netif_set_real_num_tx_queues(net, nvdev->num_chn);
  681. if (ret) {
  682. if (recovering) {
  683. netdev_err(net, "could not set tx queue count (ret %d)\n", ret);
  684. return ret;
  685. }
  686. goto recover;
  687. }
  688. ret = netif_set_real_num_rx_queues(net, nvdev->num_chn);
  689. if (ret) {
  690. if (recovering) {
  691. netdev_err(net, "could not set rx queue count (ret %d)\n", ret);
  692. return ret;
  693. }
  694. goto recover;
  695. }
  696. out:
  697. netvsc_open(net);
  698. return ret;
  699. recover:
  700. /* If the above failed, we attempt to recover through the same
  701. * process but with the original number of channels.
  702. */
  703. netdev_err(net, "could not set channels, recovering\n");
  704. recovering = true;
  705. channels->combined_count = num_chn;
  706. goto do_set;
  707. }
  708. static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
  709. {
  710. struct ethtool_cmd diff1 = *cmd;
  711. struct ethtool_cmd diff2 = {};
  712. ethtool_cmd_speed_set(&diff1, 0);
  713. diff1.duplex = 0;
  714. /* advertising and cmd are usually set */
  715. diff1.advertising = 0;
  716. diff1.cmd = 0;
  717. /* We set port to PORT_OTHER */
  718. diff2.port = PORT_OTHER;
  719. return !memcmp(&diff1, &diff2, sizeof(diff1));
  720. }
  721. static void netvsc_init_settings(struct net_device *dev)
  722. {
  723. struct net_device_context *ndc = netdev_priv(dev);
  724. ndc->speed = SPEED_UNKNOWN;
  725. ndc->duplex = DUPLEX_UNKNOWN;
  726. }
  727. static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  728. {
  729. struct net_device_context *ndc = netdev_priv(dev);
  730. ethtool_cmd_speed_set(cmd, ndc->speed);
  731. cmd->duplex = ndc->duplex;
  732. cmd->port = PORT_OTHER;
  733. return 0;
  734. }
  735. static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  736. {
  737. struct net_device_context *ndc = netdev_priv(dev);
  738. u32 speed;
  739. speed = ethtool_cmd_speed(cmd);
  740. if (!ethtool_validate_speed(speed) ||
  741. !ethtool_validate_duplex(cmd->duplex) ||
  742. !netvsc_validate_ethtool_ss_cmd(cmd))
  743. return -EINVAL;
  744. ndc->speed = speed;
  745. ndc->duplex = cmd->duplex;
  746. return 0;
  747. }
  748. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  749. {
  750. struct net_device_context *ndevctx = netdev_priv(ndev);
  751. struct hv_device *hdev = ndevctx->device_ctx;
  752. struct netvsc_device *nvdev = hv_get_drvdata(hdev);
  753. struct netvsc_device_info device_info;
  754. int limit = ETH_DATA_LEN;
  755. u32 num_chn;
  756. int ret = 0;
  757. if (nvdev == NULL || nvdev->destroy)
  758. return -ENODEV;
  759. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  760. limit = NETVSC_MTU - ETH_HLEN;
  761. if (mtu < NETVSC_MTU_MIN || mtu > limit)
  762. return -EINVAL;
  763. ret = netvsc_close(ndev);
  764. if (ret)
  765. goto out;
  766. num_chn = nvdev->num_chn;
  767. nvdev->start_remove = true;
  768. rndis_filter_device_remove(hdev);
  769. ndev->mtu = mtu;
  770. ndevctx->device_ctx = hdev;
  771. hv_set_drvdata(hdev, ndev);
  772. memset(&device_info, 0, sizeof(device_info));
  773. device_info.ring_size = ring_size;
  774. device_info.num_chn = num_chn;
  775. device_info.max_num_vrss_chns = max_num_vrss_chns;
  776. rndis_filter_device_add(hdev, &device_info);
  777. out:
  778. netvsc_open(ndev);
  779. return ret;
  780. }
  781. static struct rtnl_link_stats64 *netvsc_get_stats64(struct net_device *net,
  782. struct rtnl_link_stats64 *t)
  783. {
  784. struct net_device_context *ndev_ctx = netdev_priv(net);
  785. int cpu;
  786. for_each_possible_cpu(cpu) {
  787. struct netvsc_stats *tx_stats = per_cpu_ptr(ndev_ctx->tx_stats,
  788. cpu);
  789. struct netvsc_stats *rx_stats = per_cpu_ptr(ndev_ctx->rx_stats,
  790. cpu);
  791. u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
  792. unsigned int start;
  793. do {
  794. start = u64_stats_fetch_begin_irq(&tx_stats->syncp);
  795. tx_packets = tx_stats->packets;
  796. tx_bytes = tx_stats->bytes;
  797. } while (u64_stats_fetch_retry_irq(&tx_stats->syncp, start));
  798. do {
  799. start = u64_stats_fetch_begin_irq(&rx_stats->syncp);
  800. rx_packets = rx_stats->packets;
  801. rx_bytes = rx_stats->bytes;
  802. } while (u64_stats_fetch_retry_irq(&rx_stats->syncp, start));
  803. t->tx_bytes += tx_bytes;
  804. t->tx_packets += tx_packets;
  805. t->rx_bytes += rx_bytes;
  806. t->rx_packets += rx_packets;
  807. }
  808. t->tx_dropped = net->stats.tx_dropped;
  809. t->tx_errors = net->stats.tx_dropped;
  810. t->rx_dropped = net->stats.rx_dropped;
  811. t->rx_errors = net->stats.rx_errors;
  812. return t;
  813. }
  814. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  815. {
  816. struct net_device_context *ndevctx = netdev_priv(ndev);
  817. struct hv_device *hdev = ndevctx->device_ctx;
  818. struct sockaddr *addr = p;
  819. char save_adr[ETH_ALEN];
  820. unsigned char save_aatype;
  821. int err;
  822. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  823. save_aatype = ndev->addr_assign_type;
  824. err = eth_mac_addr(ndev, p);
  825. if (err != 0)
  826. return err;
  827. err = rndis_filter_set_device_mac(hdev, addr->sa_data);
  828. if (err != 0) {
  829. /* roll back to saved MAC */
  830. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  831. ndev->addr_assign_type = save_aatype;
  832. }
  833. return err;
  834. }
  835. #ifdef CONFIG_NET_POLL_CONTROLLER
  836. static void netvsc_poll_controller(struct net_device *net)
  837. {
  838. /* As netvsc_start_xmit() works synchronous we don't have to
  839. * trigger anything here.
  840. */
  841. }
  842. #endif
  843. static const struct ethtool_ops ethtool_ops = {
  844. .get_drvinfo = netvsc_get_drvinfo,
  845. .get_link = ethtool_op_get_link,
  846. .get_channels = netvsc_get_channels,
  847. .set_channels = netvsc_set_channels,
  848. .get_ts_info = ethtool_op_get_ts_info,
  849. .get_settings = netvsc_get_settings,
  850. .set_settings = netvsc_set_settings,
  851. };
  852. static const struct net_device_ops device_ops = {
  853. .ndo_open = netvsc_open,
  854. .ndo_stop = netvsc_close,
  855. .ndo_start_xmit = netvsc_start_xmit,
  856. .ndo_set_rx_mode = netvsc_set_multicast_list,
  857. .ndo_change_mtu = netvsc_change_mtu,
  858. .ndo_validate_addr = eth_validate_addr,
  859. .ndo_set_mac_address = netvsc_set_mac_addr,
  860. .ndo_select_queue = netvsc_select_queue,
  861. .ndo_get_stats64 = netvsc_get_stats64,
  862. #ifdef CONFIG_NET_POLL_CONTROLLER
  863. .ndo_poll_controller = netvsc_poll_controller,
  864. #endif
  865. };
  866. /*
  867. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  868. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  869. * present send GARP packet to network peers with netif_notify_peers().
  870. */
  871. static void netvsc_link_change(struct work_struct *w)
  872. {
  873. struct net_device_context *ndev_ctx;
  874. struct net_device *net;
  875. struct netvsc_device *net_device;
  876. struct rndis_device *rdev;
  877. struct netvsc_reconfig *event = NULL;
  878. bool notify = false, reschedule = false;
  879. unsigned long flags, next_reconfig, delay;
  880. ndev_ctx = container_of(w, struct net_device_context, dwork.work);
  881. net_device = hv_get_drvdata(ndev_ctx->device_ctx);
  882. rdev = net_device->extension;
  883. net = net_device->ndev;
  884. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  885. if (time_is_after_jiffies(next_reconfig)) {
  886. /* link_watch only sends one notification with current state
  887. * per second, avoid doing reconfig more frequently. Handle
  888. * wrap around.
  889. */
  890. delay = next_reconfig - jiffies;
  891. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  892. schedule_delayed_work(&ndev_ctx->dwork, delay);
  893. return;
  894. }
  895. ndev_ctx->last_reconfig = jiffies;
  896. spin_lock_irqsave(&ndev_ctx->lock, flags);
  897. if (!list_empty(&ndev_ctx->reconfig_events)) {
  898. event = list_first_entry(&ndev_ctx->reconfig_events,
  899. struct netvsc_reconfig, list);
  900. list_del(&event->list);
  901. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  902. }
  903. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  904. if (!event)
  905. return;
  906. rtnl_lock();
  907. switch (event->event) {
  908. /* Only the following events are possible due to the check in
  909. * netvsc_linkstatus_callback()
  910. */
  911. case RNDIS_STATUS_MEDIA_CONNECT:
  912. if (rdev->link_state) {
  913. rdev->link_state = false;
  914. netif_carrier_on(net);
  915. netif_tx_wake_all_queues(net);
  916. } else {
  917. notify = true;
  918. }
  919. kfree(event);
  920. break;
  921. case RNDIS_STATUS_MEDIA_DISCONNECT:
  922. if (!rdev->link_state) {
  923. rdev->link_state = true;
  924. netif_carrier_off(net);
  925. netif_tx_stop_all_queues(net);
  926. }
  927. kfree(event);
  928. break;
  929. case RNDIS_STATUS_NETWORK_CHANGE:
  930. /* Only makes sense if carrier is present */
  931. if (!rdev->link_state) {
  932. rdev->link_state = true;
  933. netif_carrier_off(net);
  934. netif_tx_stop_all_queues(net);
  935. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  936. spin_lock_irqsave(&ndev_ctx->lock, flags);
  937. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  938. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  939. reschedule = true;
  940. }
  941. break;
  942. }
  943. rtnl_unlock();
  944. if (notify)
  945. netdev_notify_peers(net);
  946. /* link_watch only sends one notification with current state per
  947. * second, handle next reconfig event in 2 seconds.
  948. */
  949. if (reschedule)
  950. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  951. }
  952. static void netvsc_free_netdev(struct net_device *netdev)
  953. {
  954. struct net_device_context *net_device_ctx = netdev_priv(netdev);
  955. free_percpu(net_device_ctx->tx_stats);
  956. free_percpu(net_device_ctx->rx_stats);
  957. free_netdev(netdev);
  958. }
  959. static void netvsc_notify_peers(struct work_struct *wrk)
  960. {
  961. struct garp_wrk *gwrk;
  962. gwrk = container_of(wrk, struct garp_wrk, dwrk);
  963. netdev_notify_peers(gwrk->netdev);
  964. atomic_dec(&gwrk->netvsc_dev->vf_use_cnt);
  965. }
  966. static struct netvsc_device *get_netvsc_device(char *mac)
  967. {
  968. struct net_device *dev;
  969. struct net_device_context *netvsc_ctx = NULL;
  970. int rtnl_locked;
  971. rtnl_locked = rtnl_trylock();
  972. for_each_netdev(&init_net, dev) {
  973. if (memcmp(dev->dev_addr, mac, ETH_ALEN) == 0) {
  974. if (dev->netdev_ops != &device_ops)
  975. continue;
  976. netvsc_ctx = netdev_priv(dev);
  977. break;
  978. }
  979. }
  980. if (rtnl_locked)
  981. rtnl_unlock();
  982. if (netvsc_ctx == NULL)
  983. return NULL;
  984. return hv_get_drvdata(netvsc_ctx->device_ctx);
  985. }
  986. static int netvsc_register_vf(struct net_device *vf_netdev)
  987. {
  988. struct netvsc_device *netvsc_dev;
  989. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  990. if (eth_ops == NULL || eth_ops == &ethtool_ops)
  991. return NOTIFY_DONE;
  992. /*
  993. * We will use the MAC address to locate the synthetic interface to
  994. * associate with the VF interface. If we don't find a matching
  995. * synthetic interface, move on.
  996. */
  997. netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
  998. if (netvsc_dev == NULL)
  999. return NOTIFY_DONE;
  1000. netdev_info(netvsc_dev->ndev, "VF registering: %s\n", vf_netdev->name);
  1001. /*
  1002. * Take a reference on the module.
  1003. */
  1004. try_module_get(THIS_MODULE);
  1005. netvsc_dev->vf_netdev = vf_netdev;
  1006. return NOTIFY_OK;
  1007. }
  1008. static int netvsc_vf_up(struct net_device *vf_netdev)
  1009. {
  1010. struct netvsc_device *netvsc_dev;
  1011. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1012. struct net_device_context *net_device_ctx;
  1013. if (eth_ops == &ethtool_ops)
  1014. return NOTIFY_DONE;
  1015. netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
  1016. if ((netvsc_dev == NULL) || (netvsc_dev->vf_netdev == NULL))
  1017. return NOTIFY_DONE;
  1018. netdev_info(netvsc_dev->ndev, "VF up: %s\n", vf_netdev->name);
  1019. net_device_ctx = netdev_priv(netvsc_dev->ndev);
  1020. netvsc_dev->vf_inject = true;
  1021. /*
  1022. * Open the device before switching data path.
  1023. */
  1024. rndis_filter_open(net_device_ctx->device_ctx);
  1025. /*
  1026. * notify the host to switch the data path.
  1027. */
  1028. netvsc_switch_datapath(netvsc_dev, true);
  1029. netdev_info(netvsc_dev->ndev, "Data path switched to VF: %s\n",
  1030. vf_netdev->name);
  1031. netif_carrier_off(netvsc_dev->ndev);
  1032. /*
  1033. * Now notify peers. We are scheduling work to
  1034. * notify peers; take a reference to prevent
  1035. * the VF interface from vanishing.
  1036. */
  1037. atomic_inc(&netvsc_dev->vf_use_cnt);
  1038. net_device_ctx->gwrk.netdev = vf_netdev;
  1039. net_device_ctx->gwrk.netvsc_dev = netvsc_dev;
  1040. schedule_work(&net_device_ctx->gwrk.dwrk);
  1041. return NOTIFY_OK;
  1042. }
  1043. static int netvsc_vf_down(struct net_device *vf_netdev)
  1044. {
  1045. struct netvsc_device *netvsc_dev;
  1046. struct net_device_context *net_device_ctx;
  1047. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1048. if (eth_ops == &ethtool_ops)
  1049. return NOTIFY_DONE;
  1050. netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
  1051. if ((netvsc_dev == NULL) || (netvsc_dev->vf_netdev == NULL))
  1052. return NOTIFY_DONE;
  1053. netdev_info(netvsc_dev->ndev, "VF down: %s\n", vf_netdev->name);
  1054. net_device_ctx = netdev_priv(netvsc_dev->ndev);
  1055. netvsc_dev->vf_inject = false;
  1056. /*
  1057. * Wait for currently active users to
  1058. * drain out.
  1059. */
  1060. while (atomic_read(&netvsc_dev->vf_use_cnt) != 0)
  1061. udelay(50);
  1062. netvsc_switch_datapath(netvsc_dev, false);
  1063. netdev_info(netvsc_dev->ndev, "Data path switched from VF: %s\n",
  1064. vf_netdev->name);
  1065. rndis_filter_close(net_device_ctx->device_ctx);
  1066. netif_carrier_on(netvsc_dev->ndev);
  1067. /*
  1068. * Notify peers.
  1069. */
  1070. atomic_inc(&netvsc_dev->vf_use_cnt);
  1071. net_device_ctx->gwrk.netdev = netvsc_dev->ndev;
  1072. net_device_ctx->gwrk.netvsc_dev = netvsc_dev;
  1073. schedule_work(&net_device_ctx->gwrk.dwrk);
  1074. return NOTIFY_OK;
  1075. }
  1076. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1077. {
  1078. struct netvsc_device *netvsc_dev;
  1079. const struct ethtool_ops *eth_ops = vf_netdev->ethtool_ops;
  1080. if (eth_ops == &ethtool_ops)
  1081. return NOTIFY_DONE;
  1082. netvsc_dev = get_netvsc_device(vf_netdev->dev_addr);
  1083. if (netvsc_dev == NULL)
  1084. return NOTIFY_DONE;
  1085. netdev_info(netvsc_dev->ndev, "VF unregistering: %s\n",
  1086. vf_netdev->name);
  1087. netvsc_dev->vf_netdev = NULL;
  1088. module_put(THIS_MODULE);
  1089. return NOTIFY_OK;
  1090. }
  1091. static int netvsc_probe(struct hv_device *dev,
  1092. const struct hv_vmbus_device_id *dev_id)
  1093. {
  1094. struct net_device *net = NULL;
  1095. struct net_device_context *net_device_ctx;
  1096. struct netvsc_device_info device_info;
  1097. struct netvsc_device *nvdev;
  1098. int ret;
  1099. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1100. num_online_cpus());
  1101. if (!net)
  1102. return -ENOMEM;
  1103. netif_carrier_off(net);
  1104. net_device_ctx = netdev_priv(net);
  1105. net_device_ctx->device_ctx = dev;
  1106. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1107. if (netif_msg_probe(net_device_ctx))
  1108. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1109. net_device_ctx->msg_enable);
  1110. net_device_ctx->tx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1111. if (!net_device_ctx->tx_stats) {
  1112. free_netdev(net);
  1113. return -ENOMEM;
  1114. }
  1115. net_device_ctx->rx_stats = netdev_alloc_pcpu_stats(struct netvsc_stats);
  1116. if (!net_device_ctx->rx_stats) {
  1117. free_percpu(net_device_ctx->tx_stats);
  1118. free_netdev(net);
  1119. return -ENOMEM;
  1120. }
  1121. hv_set_drvdata(dev, net);
  1122. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1123. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  1124. INIT_WORK(&net_device_ctx->gwrk.dwrk, netvsc_notify_peers);
  1125. spin_lock_init(&net_device_ctx->lock);
  1126. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1127. net->netdev_ops = &device_ops;
  1128. net->hw_features = NETVSC_HW_FEATURES;
  1129. net->features = NETVSC_HW_FEATURES | NETIF_F_HW_VLAN_CTAG_TX;
  1130. net->ethtool_ops = &ethtool_ops;
  1131. SET_NETDEV_DEV(net, &dev->device);
  1132. /* We always need headroom for rndis header */
  1133. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1134. /* Notify the netvsc driver of the new device */
  1135. memset(&device_info, 0, sizeof(device_info));
  1136. device_info.ring_size = ring_size;
  1137. device_info.max_num_vrss_chns = max_num_vrss_chns;
  1138. ret = rndis_filter_device_add(dev, &device_info);
  1139. if (ret != 0) {
  1140. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1141. netvsc_free_netdev(net);
  1142. hv_set_drvdata(dev, NULL);
  1143. return ret;
  1144. }
  1145. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1146. nvdev = hv_get_drvdata(dev);
  1147. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  1148. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  1149. netvsc_init_settings(net);
  1150. ret = register_netdev(net);
  1151. if (ret != 0) {
  1152. pr_err("Unable to register netdev.\n");
  1153. rndis_filter_device_remove(dev);
  1154. netvsc_free_netdev(net);
  1155. }
  1156. return ret;
  1157. }
  1158. static int netvsc_remove(struct hv_device *dev)
  1159. {
  1160. struct net_device *net;
  1161. struct net_device_context *ndev_ctx;
  1162. struct netvsc_device *net_device;
  1163. net_device = hv_get_drvdata(dev);
  1164. net = net_device->ndev;
  1165. if (net == NULL) {
  1166. dev_err(&dev->device, "No net device to remove\n");
  1167. return 0;
  1168. }
  1169. net_device->start_remove = true;
  1170. ndev_ctx = netdev_priv(net);
  1171. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1172. cancel_work_sync(&ndev_ctx->work);
  1173. /* Stop outbound asap */
  1174. netif_tx_disable(net);
  1175. unregister_netdev(net);
  1176. /*
  1177. * Call to the vsc driver to let it know that the device is being
  1178. * removed
  1179. */
  1180. rndis_filter_device_remove(dev);
  1181. netvsc_free_netdev(net);
  1182. return 0;
  1183. }
  1184. static const struct hv_vmbus_device_id id_table[] = {
  1185. /* Network guid */
  1186. { HV_NIC_GUID, },
  1187. { },
  1188. };
  1189. MODULE_DEVICE_TABLE(vmbus, id_table);
  1190. /* The one and only one */
  1191. static struct hv_driver netvsc_drv = {
  1192. .name = KBUILD_MODNAME,
  1193. .id_table = id_table,
  1194. .probe = netvsc_probe,
  1195. .remove = netvsc_remove,
  1196. };
  1197. /*
  1198. * On Hyper-V, every VF interface is matched with a corresponding
  1199. * synthetic interface. The synthetic interface is presented first
  1200. * to the guest. When the corresponding VF instance is registered,
  1201. * we will take care of switching the data path.
  1202. */
  1203. static int netvsc_netdev_event(struct notifier_block *this,
  1204. unsigned long event, void *ptr)
  1205. {
  1206. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1207. switch (event) {
  1208. case NETDEV_REGISTER:
  1209. return netvsc_register_vf(event_dev);
  1210. case NETDEV_UNREGISTER:
  1211. return netvsc_unregister_vf(event_dev);
  1212. case NETDEV_UP:
  1213. return netvsc_vf_up(event_dev);
  1214. case NETDEV_DOWN:
  1215. return netvsc_vf_down(event_dev);
  1216. default:
  1217. return NOTIFY_DONE;
  1218. }
  1219. }
  1220. static struct notifier_block netvsc_netdev_notifier = {
  1221. .notifier_call = netvsc_netdev_event,
  1222. };
  1223. static void __exit netvsc_drv_exit(void)
  1224. {
  1225. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1226. vmbus_driver_unregister(&netvsc_drv);
  1227. }
  1228. static int __init netvsc_drv_init(void)
  1229. {
  1230. int ret;
  1231. if (ring_size < RING_SIZE_MIN) {
  1232. ring_size = RING_SIZE_MIN;
  1233. pr_info("Increased ring_size to %d (min allowed)\n",
  1234. ring_size);
  1235. }
  1236. ret = vmbus_driver_register(&netvsc_drv);
  1237. if (ret)
  1238. return ret;
  1239. register_netdevice_notifier(&netvsc_netdev_notifier);
  1240. return 0;
  1241. }
  1242. MODULE_LICENSE("GPL");
  1243. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1244. module_init(netvsc_drv_init);
  1245. module_exit(netvsc_drv_exit);