netvsc_drv.c 42 KB

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