netvsc_drv.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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 = 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 = 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. const struct ndis_tcp_ip_checksum_info *csum_info,
  478. const struct ndis_pkt_8021q_info *vlan,
  479. void *data, u32 buflen)
  480. {
  481. struct sk_buff *skb;
  482. skb = netdev_alloc_skb_ip_align(net, buflen);
  483. if (!skb)
  484. return skb;
  485. /*
  486. * Copy to skb. This copy is needed here since the memory pointed by
  487. * hv_netvsc_packet cannot be deallocated
  488. */
  489. memcpy(skb_put(skb, buflen), data, buflen);
  490. skb->protocol = eth_type_trans(skb, net);
  491. /* skb is already created with CHECKSUM_NONE */
  492. skb_checksum_none_assert(skb);
  493. /*
  494. * In Linux, the IP checksum is always checked.
  495. * Do L4 checksum offload if enabled and present.
  496. */
  497. if (csum_info && (net->features & NETIF_F_RXCSUM)) {
  498. if (csum_info->receive.tcp_checksum_succeeded ||
  499. csum_info->receive.udp_checksum_succeeded)
  500. skb->ip_summed = CHECKSUM_UNNECESSARY;
  501. }
  502. if (vlan) {
  503. u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
  504. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  505. vlan_tci);
  506. }
  507. return skb;
  508. }
  509. /*
  510. * netvsc_recv_callback - Callback when we receive a packet from the
  511. * "wire" on the specified device.
  512. */
  513. int netvsc_recv_callback(struct net_device *net,
  514. struct vmbus_channel *channel,
  515. void *data, u32 len,
  516. const struct ndis_tcp_ip_checksum_info *csum_info,
  517. const struct ndis_pkt_8021q_info *vlan)
  518. {
  519. struct net_device_context *net_device_ctx = netdev_priv(net);
  520. struct netvsc_device *net_device = net_device_ctx->nvdev;
  521. struct net_device *vf_netdev;
  522. struct sk_buff *skb;
  523. struct netvsc_stats *rx_stats;
  524. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  525. if (net->reg_state != NETREG_REGISTERED)
  526. return NVSP_STAT_FAIL;
  527. /*
  528. * If necessary, inject this packet into the VF interface.
  529. * On Hyper-V, multicast and brodcast packets are only delivered
  530. * to the synthetic interface (after subjecting these to
  531. * policy filters on the host). Deliver these via the VF
  532. * interface in the guest.
  533. */
  534. rcu_read_lock();
  535. vf_netdev = rcu_dereference(net_device_ctx->vf_netdev);
  536. if (vf_netdev && (vf_netdev->flags & IFF_UP))
  537. net = vf_netdev;
  538. /* Allocate a skb - TODO direct I/O to pages? */
  539. skb = netvsc_alloc_recv_skb(net, csum_info, vlan, data, len);
  540. if (unlikely(!skb)) {
  541. ++net->stats.rx_dropped;
  542. rcu_read_unlock();
  543. return NVSP_STAT_FAIL;
  544. }
  545. if (net != vf_netdev)
  546. skb_record_rx_queue(skb, q_idx);
  547. /*
  548. * Even if injecting the packet, record the statistics
  549. * on the synthetic device because modifying the VF device
  550. * statistics will not work correctly.
  551. */
  552. rx_stats = &net_device->chan_table[q_idx].rx_stats;
  553. u64_stats_update_begin(&rx_stats->syncp);
  554. rx_stats->packets++;
  555. rx_stats->bytes += len;
  556. if (skb->pkt_type == PACKET_BROADCAST)
  557. ++rx_stats->broadcast;
  558. else if (skb->pkt_type == PACKET_MULTICAST)
  559. ++rx_stats->multicast;
  560. u64_stats_update_end(&rx_stats->syncp);
  561. /*
  562. * Pass the skb back up. Network stack will deallocate the skb when it
  563. * is done.
  564. * TODO - use NAPI?
  565. */
  566. netif_receive_skb(skb);
  567. rcu_read_unlock();
  568. return 0;
  569. }
  570. static void netvsc_get_drvinfo(struct net_device *net,
  571. struct ethtool_drvinfo *info)
  572. {
  573. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  574. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  575. }
  576. static void netvsc_get_channels(struct net_device *net,
  577. struct ethtool_channels *channel)
  578. {
  579. struct net_device_context *net_device_ctx = netdev_priv(net);
  580. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  581. if (nvdev) {
  582. channel->max_combined = nvdev->max_chn;
  583. channel->combined_count = nvdev->num_chn;
  584. }
  585. }
  586. static int netvsc_set_queues(struct net_device *net, struct hv_device *dev,
  587. u32 num_chn)
  588. {
  589. struct netvsc_device_info device_info;
  590. int ret;
  591. memset(&device_info, 0, sizeof(device_info));
  592. device_info.num_chn = num_chn;
  593. device_info.ring_size = ring_size;
  594. device_info.max_num_vrss_chns = num_chn;
  595. ret = rndis_filter_device_add(dev, &device_info);
  596. if (ret)
  597. return ret;
  598. ret = netif_set_real_num_tx_queues(net, num_chn);
  599. if (ret)
  600. return ret;
  601. ret = netif_set_real_num_rx_queues(net, num_chn);
  602. return ret;
  603. }
  604. static int netvsc_set_channels(struct net_device *net,
  605. struct ethtool_channels *channels)
  606. {
  607. struct net_device_context *net_device_ctx = netdev_priv(net);
  608. struct hv_device *dev = net_device_ctx->device_ctx;
  609. struct netvsc_device *nvdev = net_device_ctx->nvdev;
  610. unsigned int count = channels->combined_count;
  611. int ret;
  612. /* We do not support separate count for rx, tx, or other */
  613. if (count == 0 ||
  614. channels->rx_count || channels->tx_count || channels->other_count)
  615. return -EINVAL;
  616. if (count > net->num_tx_queues || count > net->num_rx_queues)
  617. return -EINVAL;
  618. if (net_device_ctx->start_remove || !nvdev || nvdev->destroy)
  619. return -ENODEV;
  620. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  621. return -EINVAL;
  622. if (count > nvdev->max_chn)
  623. return -EINVAL;
  624. ret = netvsc_close(net);
  625. if (ret)
  626. return ret;
  627. net_device_ctx->start_remove = true;
  628. rndis_filter_device_remove(dev, nvdev);
  629. ret = netvsc_set_queues(net, dev, count);
  630. if (ret == 0)
  631. nvdev->num_chn = count;
  632. else
  633. netvsc_set_queues(net, dev, nvdev->num_chn);
  634. netvsc_open(net);
  635. net_device_ctx->start_remove = false;
  636. /* We may have missed link change notifications */
  637. schedule_delayed_work(&net_device_ctx->dwork, 0);
  638. return ret;
  639. }
  640. static bool netvsc_validate_ethtool_ss_cmd(const struct ethtool_cmd *cmd)
  641. {
  642. struct ethtool_cmd diff1 = *cmd;
  643. struct ethtool_cmd diff2 = {};
  644. ethtool_cmd_speed_set(&diff1, 0);
  645. diff1.duplex = 0;
  646. /* advertising and cmd are usually set */
  647. diff1.advertising = 0;
  648. diff1.cmd = 0;
  649. /* We set port to PORT_OTHER */
  650. diff2.port = PORT_OTHER;
  651. return !memcmp(&diff1, &diff2, sizeof(diff1));
  652. }
  653. static void netvsc_init_settings(struct net_device *dev)
  654. {
  655. struct net_device_context *ndc = netdev_priv(dev);
  656. ndc->speed = SPEED_UNKNOWN;
  657. ndc->duplex = DUPLEX_UNKNOWN;
  658. }
  659. static int netvsc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  660. {
  661. struct net_device_context *ndc = netdev_priv(dev);
  662. ethtool_cmd_speed_set(cmd, ndc->speed);
  663. cmd->duplex = ndc->duplex;
  664. cmd->port = PORT_OTHER;
  665. return 0;
  666. }
  667. static int netvsc_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  668. {
  669. struct net_device_context *ndc = netdev_priv(dev);
  670. u32 speed;
  671. speed = ethtool_cmd_speed(cmd);
  672. if (!ethtool_validate_speed(speed) ||
  673. !ethtool_validate_duplex(cmd->duplex) ||
  674. !netvsc_validate_ethtool_ss_cmd(cmd))
  675. return -EINVAL;
  676. ndc->speed = speed;
  677. ndc->duplex = cmd->duplex;
  678. return 0;
  679. }
  680. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  681. {
  682. struct net_device_context *ndevctx = netdev_priv(ndev);
  683. struct netvsc_device *nvdev = ndevctx->nvdev;
  684. struct hv_device *hdev = ndevctx->device_ctx;
  685. struct netvsc_device_info device_info;
  686. int ret;
  687. if (ndevctx->start_remove || !nvdev || nvdev->destroy)
  688. return -ENODEV;
  689. ret = netvsc_close(ndev);
  690. if (ret)
  691. goto out;
  692. memset(&device_info, 0, sizeof(device_info));
  693. device_info.ring_size = ring_size;
  694. device_info.num_chn = nvdev->num_chn;
  695. device_info.max_num_vrss_chns = nvdev->num_chn;
  696. ndevctx->start_remove = true;
  697. rndis_filter_device_remove(hdev, nvdev);
  698. /* 'nvdev' has been freed in rndis_filter_device_remove() ->
  699. * netvsc_device_remove () -> free_netvsc_device().
  700. * We mustn't access it before it's re-created in
  701. * rndis_filter_device_add() -> netvsc_device_add().
  702. */
  703. ndev->mtu = mtu;
  704. rndis_filter_device_add(hdev, &device_info);
  705. out:
  706. netvsc_open(ndev);
  707. ndevctx->start_remove = false;
  708. /* We may have missed link change notifications */
  709. schedule_delayed_work(&ndevctx->dwork, 0);
  710. return ret;
  711. }
  712. static void netvsc_get_stats64(struct net_device *net,
  713. struct rtnl_link_stats64 *t)
  714. {
  715. struct net_device_context *ndev_ctx = netdev_priv(net);
  716. struct netvsc_device *nvdev = ndev_ctx->nvdev;
  717. int i;
  718. if (!nvdev)
  719. return;
  720. for (i = 0; i < nvdev->num_chn; i++) {
  721. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  722. const struct netvsc_stats *stats;
  723. u64 packets, bytes, multicast;
  724. unsigned int start;
  725. stats = &nvchan->tx_stats;
  726. do {
  727. start = u64_stats_fetch_begin_irq(&stats->syncp);
  728. packets = stats->packets;
  729. bytes = stats->bytes;
  730. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  731. t->tx_bytes += bytes;
  732. t->tx_packets += packets;
  733. stats = &nvchan->rx_stats;
  734. do {
  735. start = u64_stats_fetch_begin_irq(&stats->syncp);
  736. packets = stats->packets;
  737. bytes = stats->bytes;
  738. multicast = stats->multicast + stats->broadcast;
  739. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  740. t->rx_bytes += bytes;
  741. t->rx_packets += packets;
  742. t->multicast += multicast;
  743. }
  744. t->tx_dropped = net->stats.tx_dropped;
  745. t->tx_errors = net->stats.tx_errors;
  746. t->rx_dropped = net->stats.rx_dropped;
  747. t->rx_errors = net->stats.rx_errors;
  748. }
  749. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  750. {
  751. struct sockaddr *addr = p;
  752. char save_adr[ETH_ALEN];
  753. unsigned char save_aatype;
  754. int err;
  755. memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
  756. save_aatype = ndev->addr_assign_type;
  757. err = eth_mac_addr(ndev, p);
  758. if (err != 0)
  759. return err;
  760. err = rndis_filter_set_device_mac(ndev, addr->sa_data);
  761. if (err != 0) {
  762. /* roll back to saved MAC */
  763. memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
  764. ndev->addr_assign_type = save_aatype;
  765. }
  766. return err;
  767. }
  768. static const struct {
  769. char name[ETH_GSTRING_LEN];
  770. u16 offset;
  771. } netvsc_stats[] = {
  772. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  773. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  774. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  775. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  776. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  777. };
  778. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  779. /* 4 statistics per queue (rx/tx packets/bytes) */
  780. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
  781. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  782. {
  783. struct net_device_context *ndc = netdev_priv(dev);
  784. struct netvsc_device *nvdev = ndc->nvdev;
  785. switch (string_set) {
  786. case ETH_SS_STATS:
  787. return NETVSC_GLOBAL_STATS_LEN + NETVSC_QUEUE_STATS_LEN(nvdev);
  788. default:
  789. return -EINVAL;
  790. }
  791. }
  792. static void netvsc_get_ethtool_stats(struct net_device *dev,
  793. struct ethtool_stats *stats, u64 *data)
  794. {
  795. struct net_device_context *ndc = netdev_priv(dev);
  796. struct netvsc_device *nvdev = ndc->nvdev;
  797. const void *nds = &ndc->eth_stats;
  798. const struct netvsc_stats *qstats;
  799. unsigned int start;
  800. u64 packets, bytes;
  801. int i, j;
  802. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  803. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  804. for (j = 0; j < nvdev->num_chn; j++) {
  805. qstats = &nvdev->chan_table[j].tx_stats;
  806. do {
  807. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  808. packets = qstats->packets;
  809. bytes = qstats->bytes;
  810. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  811. data[i++] = packets;
  812. data[i++] = bytes;
  813. qstats = &nvdev->chan_table[j].rx_stats;
  814. do {
  815. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  816. packets = qstats->packets;
  817. bytes = qstats->bytes;
  818. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  819. data[i++] = packets;
  820. data[i++] = bytes;
  821. }
  822. }
  823. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  824. {
  825. struct net_device_context *ndc = netdev_priv(dev);
  826. struct netvsc_device *nvdev = ndc->nvdev;
  827. u8 *p = data;
  828. int i;
  829. switch (stringset) {
  830. case ETH_SS_STATS:
  831. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
  832. memcpy(p + i * ETH_GSTRING_LEN,
  833. netvsc_stats[i].name, ETH_GSTRING_LEN);
  834. p += i * ETH_GSTRING_LEN;
  835. for (i = 0; i < nvdev->num_chn; i++) {
  836. sprintf(p, "tx_queue_%u_packets", i);
  837. p += ETH_GSTRING_LEN;
  838. sprintf(p, "tx_queue_%u_bytes", i);
  839. p += ETH_GSTRING_LEN;
  840. sprintf(p, "rx_queue_%u_packets", i);
  841. p += ETH_GSTRING_LEN;
  842. sprintf(p, "rx_queue_%u_bytes", i);
  843. p += ETH_GSTRING_LEN;
  844. }
  845. break;
  846. }
  847. }
  848. static int
  849. netvsc_get_rss_hash_opts(struct netvsc_device *nvdev,
  850. struct ethtool_rxnfc *info)
  851. {
  852. info->data = RXH_IP_SRC | RXH_IP_DST;
  853. switch (info->flow_type) {
  854. case TCP_V4_FLOW:
  855. case TCP_V6_FLOW:
  856. info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
  857. /* fallthrough */
  858. case UDP_V4_FLOW:
  859. case UDP_V6_FLOW:
  860. case IPV4_FLOW:
  861. case IPV6_FLOW:
  862. break;
  863. default:
  864. info->data = 0;
  865. break;
  866. }
  867. return 0;
  868. }
  869. static int
  870. netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  871. u32 *rules)
  872. {
  873. struct net_device_context *ndc = netdev_priv(dev);
  874. struct netvsc_device *nvdev = ndc->nvdev;
  875. switch (info->cmd) {
  876. case ETHTOOL_GRXRINGS:
  877. info->data = nvdev->num_chn;
  878. return 0;
  879. case ETHTOOL_GRXFH:
  880. return netvsc_get_rss_hash_opts(nvdev, info);
  881. }
  882. return -EOPNOTSUPP;
  883. }
  884. #ifdef CONFIG_NET_POLL_CONTROLLER
  885. static void netvsc_poll_controller(struct net_device *net)
  886. {
  887. /* As netvsc_start_xmit() works synchronous we don't have to
  888. * trigger anything here.
  889. */
  890. }
  891. #endif
  892. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  893. {
  894. return NETVSC_HASH_KEYLEN;
  895. }
  896. static u32 netvsc_rss_indir_size(struct net_device *dev)
  897. {
  898. return ITAB_NUM;
  899. }
  900. static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  901. u8 *hfunc)
  902. {
  903. struct net_device_context *ndc = netdev_priv(dev);
  904. struct netvsc_device *ndev = ndc->nvdev;
  905. struct rndis_device *rndis_dev = ndev->extension;
  906. int i;
  907. if (hfunc)
  908. *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  909. if (indir) {
  910. for (i = 0; i < ITAB_NUM; i++)
  911. indir[i] = rndis_dev->ind_table[i];
  912. }
  913. if (key)
  914. memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  915. return 0;
  916. }
  917. static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
  918. const u8 *key, const u8 hfunc)
  919. {
  920. struct net_device_context *ndc = netdev_priv(dev);
  921. struct netvsc_device *ndev = ndc->nvdev;
  922. struct rndis_device *rndis_dev = ndev->extension;
  923. int i;
  924. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  925. return -EOPNOTSUPP;
  926. if (indir) {
  927. for (i = 0; i < ITAB_NUM; i++)
  928. if (indir[i] >= dev->num_rx_queues)
  929. return -EINVAL;
  930. for (i = 0; i < ITAB_NUM; i++)
  931. rndis_dev->ind_table[i] = indir[i];
  932. }
  933. if (!key) {
  934. if (!indir)
  935. return 0;
  936. key = rndis_dev->rss_key;
  937. }
  938. return rndis_filter_set_rss_param(rndis_dev, key, ndev->num_chn);
  939. }
  940. static const struct ethtool_ops ethtool_ops = {
  941. .get_drvinfo = netvsc_get_drvinfo,
  942. .get_link = ethtool_op_get_link,
  943. .get_ethtool_stats = netvsc_get_ethtool_stats,
  944. .get_sset_count = netvsc_get_sset_count,
  945. .get_strings = netvsc_get_strings,
  946. .get_channels = netvsc_get_channels,
  947. .set_channels = netvsc_set_channels,
  948. .get_ts_info = ethtool_op_get_ts_info,
  949. .get_settings = netvsc_get_settings,
  950. .set_settings = netvsc_set_settings,
  951. .get_rxnfc = netvsc_get_rxnfc,
  952. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  953. .get_rxfh_indir_size = netvsc_rss_indir_size,
  954. .get_rxfh = netvsc_get_rxfh,
  955. .set_rxfh = netvsc_set_rxfh,
  956. };
  957. static const struct net_device_ops device_ops = {
  958. .ndo_open = netvsc_open,
  959. .ndo_stop = netvsc_close,
  960. .ndo_start_xmit = netvsc_start_xmit,
  961. .ndo_set_rx_mode = netvsc_set_multicast_list,
  962. .ndo_change_mtu = netvsc_change_mtu,
  963. .ndo_validate_addr = eth_validate_addr,
  964. .ndo_set_mac_address = netvsc_set_mac_addr,
  965. .ndo_select_queue = netvsc_select_queue,
  966. .ndo_get_stats64 = netvsc_get_stats64,
  967. #ifdef CONFIG_NET_POLL_CONTROLLER
  968. .ndo_poll_controller = netvsc_poll_controller,
  969. #endif
  970. };
  971. /*
  972. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  973. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  974. * present send GARP packet to network peers with netif_notify_peers().
  975. */
  976. static void netvsc_link_change(struct work_struct *w)
  977. {
  978. struct net_device_context *ndev_ctx =
  979. container_of(w, struct net_device_context, dwork.work);
  980. struct hv_device *device_obj = ndev_ctx->device_ctx;
  981. struct net_device *net = hv_get_drvdata(device_obj);
  982. struct netvsc_device *net_device;
  983. struct rndis_device *rdev;
  984. struct netvsc_reconfig *event = NULL;
  985. bool notify = false, reschedule = false;
  986. unsigned long flags, next_reconfig, delay;
  987. rtnl_lock();
  988. if (ndev_ctx->start_remove)
  989. goto out_unlock;
  990. net_device = ndev_ctx->nvdev;
  991. rdev = net_device->extension;
  992. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  993. if (time_is_after_jiffies(next_reconfig)) {
  994. /* link_watch only sends one notification with current state
  995. * per second, avoid doing reconfig more frequently. Handle
  996. * wrap around.
  997. */
  998. delay = next_reconfig - jiffies;
  999. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1000. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1001. goto out_unlock;
  1002. }
  1003. ndev_ctx->last_reconfig = jiffies;
  1004. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1005. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1006. event = list_first_entry(&ndev_ctx->reconfig_events,
  1007. struct netvsc_reconfig, list);
  1008. list_del(&event->list);
  1009. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1010. }
  1011. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1012. if (!event)
  1013. goto out_unlock;
  1014. switch (event->event) {
  1015. /* Only the following events are possible due to the check in
  1016. * netvsc_linkstatus_callback()
  1017. */
  1018. case RNDIS_STATUS_MEDIA_CONNECT:
  1019. if (rdev->link_state) {
  1020. rdev->link_state = false;
  1021. netif_carrier_on(net);
  1022. netif_tx_wake_all_queues(net);
  1023. } else {
  1024. notify = true;
  1025. }
  1026. kfree(event);
  1027. break;
  1028. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1029. if (!rdev->link_state) {
  1030. rdev->link_state = true;
  1031. netif_carrier_off(net);
  1032. netif_tx_stop_all_queues(net);
  1033. }
  1034. kfree(event);
  1035. break;
  1036. case RNDIS_STATUS_NETWORK_CHANGE:
  1037. /* Only makes sense if carrier is present */
  1038. if (!rdev->link_state) {
  1039. rdev->link_state = true;
  1040. netif_carrier_off(net);
  1041. netif_tx_stop_all_queues(net);
  1042. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1043. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1044. list_add(&event->list, &ndev_ctx->reconfig_events);
  1045. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1046. reschedule = true;
  1047. }
  1048. break;
  1049. }
  1050. rtnl_unlock();
  1051. if (notify)
  1052. netdev_notify_peers(net);
  1053. /* link_watch only sends one notification with current state per
  1054. * second, handle next reconfig event in 2 seconds.
  1055. */
  1056. if (reschedule)
  1057. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1058. return;
  1059. out_unlock:
  1060. rtnl_unlock();
  1061. }
  1062. static struct net_device *get_netvsc_bymac(const u8 *mac)
  1063. {
  1064. struct net_device *dev;
  1065. ASSERT_RTNL();
  1066. for_each_netdev(&init_net, dev) {
  1067. if (dev->netdev_ops != &device_ops)
  1068. continue; /* not a netvsc device */
  1069. if (ether_addr_equal(mac, dev->perm_addr))
  1070. return dev;
  1071. }
  1072. return NULL;
  1073. }
  1074. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  1075. {
  1076. struct net_device *dev;
  1077. ASSERT_RTNL();
  1078. for_each_netdev(&init_net, dev) {
  1079. struct net_device_context *net_device_ctx;
  1080. if (dev->netdev_ops != &device_ops)
  1081. continue; /* not a netvsc device */
  1082. net_device_ctx = netdev_priv(dev);
  1083. if (net_device_ctx->nvdev == NULL)
  1084. continue; /* device is removed */
  1085. if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
  1086. return dev; /* a match */
  1087. }
  1088. return NULL;
  1089. }
  1090. static int netvsc_register_vf(struct net_device *vf_netdev)
  1091. {
  1092. struct net_device *ndev;
  1093. struct net_device_context *net_device_ctx;
  1094. struct netvsc_device *netvsc_dev;
  1095. if (vf_netdev->addr_len != ETH_ALEN)
  1096. return NOTIFY_DONE;
  1097. /*
  1098. * We will use the MAC address to locate the synthetic interface to
  1099. * associate with the VF interface. If we don't find a matching
  1100. * synthetic interface, move on.
  1101. */
  1102. ndev = get_netvsc_bymac(vf_netdev->perm_addr);
  1103. if (!ndev)
  1104. return NOTIFY_DONE;
  1105. net_device_ctx = netdev_priv(ndev);
  1106. netvsc_dev = net_device_ctx->nvdev;
  1107. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1108. return NOTIFY_DONE;
  1109. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1110. /*
  1111. * Take a reference on the module.
  1112. */
  1113. try_module_get(THIS_MODULE);
  1114. dev_hold(vf_netdev);
  1115. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1116. return NOTIFY_OK;
  1117. }
  1118. static int netvsc_vf_up(struct net_device *vf_netdev)
  1119. {
  1120. struct net_device *ndev;
  1121. struct netvsc_device *netvsc_dev;
  1122. struct net_device_context *net_device_ctx;
  1123. ndev = get_netvsc_byref(vf_netdev);
  1124. if (!ndev)
  1125. return NOTIFY_DONE;
  1126. net_device_ctx = netdev_priv(ndev);
  1127. netvsc_dev = net_device_ctx->nvdev;
  1128. netdev_info(ndev, "VF up: %s\n", vf_netdev->name);
  1129. /*
  1130. * Open the device before switching data path.
  1131. */
  1132. rndis_filter_open(netvsc_dev);
  1133. /*
  1134. * notify the host to switch the data path.
  1135. */
  1136. netvsc_switch_datapath(ndev, true);
  1137. netdev_info(ndev, "Data path switched to VF: %s\n", vf_netdev->name);
  1138. netif_carrier_off(ndev);
  1139. /* Now notify peers through VF device. */
  1140. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, vf_netdev);
  1141. return NOTIFY_OK;
  1142. }
  1143. static int netvsc_vf_down(struct net_device *vf_netdev)
  1144. {
  1145. struct net_device *ndev;
  1146. struct netvsc_device *netvsc_dev;
  1147. struct net_device_context *net_device_ctx;
  1148. ndev = get_netvsc_byref(vf_netdev);
  1149. if (!ndev)
  1150. return NOTIFY_DONE;
  1151. net_device_ctx = netdev_priv(ndev);
  1152. netvsc_dev = net_device_ctx->nvdev;
  1153. netdev_info(ndev, "VF down: %s\n", vf_netdev->name);
  1154. netvsc_switch_datapath(ndev, false);
  1155. netdev_info(ndev, "Data path switched from VF: %s\n", vf_netdev->name);
  1156. rndis_filter_close(netvsc_dev);
  1157. netif_carrier_on(ndev);
  1158. /* Now notify peers through netvsc device. */
  1159. call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, ndev);
  1160. return NOTIFY_OK;
  1161. }
  1162. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1163. {
  1164. struct net_device *ndev;
  1165. struct net_device_context *net_device_ctx;
  1166. ndev = get_netvsc_byref(vf_netdev);
  1167. if (!ndev)
  1168. return NOTIFY_DONE;
  1169. net_device_ctx = netdev_priv(ndev);
  1170. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1171. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1172. dev_put(vf_netdev);
  1173. module_put(THIS_MODULE);
  1174. return NOTIFY_OK;
  1175. }
  1176. static int netvsc_probe(struct hv_device *dev,
  1177. const struct hv_vmbus_device_id *dev_id)
  1178. {
  1179. struct net_device *net = NULL;
  1180. struct net_device_context *net_device_ctx;
  1181. struct netvsc_device_info device_info;
  1182. struct netvsc_device *nvdev;
  1183. int ret;
  1184. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1185. VRSS_CHANNEL_MAX);
  1186. if (!net)
  1187. return -ENOMEM;
  1188. netif_carrier_off(net);
  1189. netvsc_init_settings(net);
  1190. net_device_ctx = netdev_priv(net);
  1191. net_device_ctx->device_ctx = dev;
  1192. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1193. if (netif_msg_probe(net_device_ctx))
  1194. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1195. net_device_ctx->msg_enable);
  1196. hv_set_drvdata(dev, net);
  1197. net_device_ctx->start_remove = false;
  1198. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1199. INIT_WORK(&net_device_ctx->work, do_set_multicast);
  1200. spin_lock_init(&net_device_ctx->lock);
  1201. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1202. net->netdev_ops = &device_ops;
  1203. net->ethtool_ops = &ethtool_ops;
  1204. SET_NETDEV_DEV(net, &dev->device);
  1205. /* We always need headroom for rndis header */
  1206. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1207. /* Notify the netvsc driver of the new device */
  1208. memset(&device_info, 0, sizeof(device_info));
  1209. device_info.ring_size = ring_size;
  1210. device_info.max_num_vrss_chns = min_t(u32, VRSS_CHANNEL_DEFAULT,
  1211. num_online_cpus());
  1212. ret = rndis_filter_device_add(dev, &device_info);
  1213. if (ret != 0) {
  1214. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1215. free_netdev(net);
  1216. hv_set_drvdata(dev, NULL);
  1217. return ret;
  1218. }
  1219. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1220. /* hw_features computed in rndis_filter_device_add */
  1221. net->features = net->hw_features |
  1222. NETIF_F_HIGHDMA | NETIF_F_SG |
  1223. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1224. net->vlan_features = net->features;
  1225. nvdev = net_device_ctx->nvdev;
  1226. netif_set_real_num_tx_queues(net, nvdev->num_chn);
  1227. netif_set_real_num_rx_queues(net, nvdev->num_chn);
  1228. /* MTU range: 68 - 1500 or 65521 */
  1229. net->min_mtu = NETVSC_MTU_MIN;
  1230. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1231. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1232. else
  1233. net->max_mtu = ETH_DATA_LEN;
  1234. ret = register_netdev(net);
  1235. if (ret != 0) {
  1236. pr_err("Unable to register netdev.\n");
  1237. rndis_filter_device_remove(dev, nvdev);
  1238. free_netdev(net);
  1239. }
  1240. return ret;
  1241. }
  1242. static int netvsc_remove(struct hv_device *dev)
  1243. {
  1244. struct net_device *net;
  1245. struct net_device_context *ndev_ctx;
  1246. net = hv_get_drvdata(dev);
  1247. if (net == NULL) {
  1248. dev_err(&dev->device, "No net device to remove\n");
  1249. return 0;
  1250. }
  1251. ndev_ctx = netdev_priv(net);
  1252. /* Avoid racing with netvsc_change_mtu()/netvsc_set_channels()
  1253. * removing the device.
  1254. */
  1255. rtnl_lock();
  1256. ndev_ctx->start_remove = true;
  1257. rtnl_unlock();
  1258. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1259. cancel_work_sync(&ndev_ctx->work);
  1260. /* Stop outbound asap */
  1261. netif_tx_disable(net);
  1262. unregister_netdev(net);
  1263. /*
  1264. * Call to the vsc driver to let it know that the device is being
  1265. * removed
  1266. */
  1267. rndis_filter_device_remove(dev, ndev_ctx->nvdev);
  1268. hv_set_drvdata(dev, NULL);
  1269. free_netdev(net);
  1270. return 0;
  1271. }
  1272. static const struct hv_vmbus_device_id id_table[] = {
  1273. /* Network guid */
  1274. { HV_NIC_GUID, },
  1275. { },
  1276. };
  1277. MODULE_DEVICE_TABLE(vmbus, id_table);
  1278. /* The one and only one */
  1279. static struct hv_driver netvsc_drv = {
  1280. .name = KBUILD_MODNAME,
  1281. .id_table = id_table,
  1282. .probe = netvsc_probe,
  1283. .remove = netvsc_remove,
  1284. };
  1285. /*
  1286. * On Hyper-V, every VF interface is matched with a corresponding
  1287. * synthetic interface. The synthetic interface is presented first
  1288. * to the guest. When the corresponding VF instance is registered,
  1289. * we will take care of switching the data path.
  1290. */
  1291. static int netvsc_netdev_event(struct notifier_block *this,
  1292. unsigned long event, void *ptr)
  1293. {
  1294. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1295. /* Skip our own events */
  1296. if (event_dev->netdev_ops == &device_ops)
  1297. return NOTIFY_DONE;
  1298. /* Avoid non-Ethernet type devices */
  1299. if (event_dev->type != ARPHRD_ETHER)
  1300. return NOTIFY_DONE;
  1301. /* Avoid Vlan dev with same MAC registering as VF */
  1302. if (is_vlan_dev(event_dev))
  1303. return NOTIFY_DONE;
  1304. /* Avoid Bonding master dev with same MAC registering as VF */
  1305. if ((event_dev->priv_flags & IFF_BONDING) &&
  1306. (event_dev->flags & IFF_MASTER))
  1307. return NOTIFY_DONE;
  1308. switch (event) {
  1309. case NETDEV_REGISTER:
  1310. return netvsc_register_vf(event_dev);
  1311. case NETDEV_UNREGISTER:
  1312. return netvsc_unregister_vf(event_dev);
  1313. case NETDEV_UP:
  1314. return netvsc_vf_up(event_dev);
  1315. case NETDEV_DOWN:
  1316. return netvsc_vf_down(event_dev);
  1317. default:
  1318. return NOTIFY_DONE;
  1319. }
  1320. }
  1321. static struct notifier_block netvsc_netdev_notifier = {
  1322. .notifier_call = netvsc_netdev_event,
  1323. };
  1324. static void __exit netvsc_drv_exit(void)
  1325. {
  1326. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1327. vmbus_driver_unregister(&netvsc_drv);
  1328. }
  1329. static int __init netvsc_drv_init(void)
  1330. {
  1331. int ret;
  1332. if (ring_size < RING_SIZE_MIN) {
  1333. ring_size = RING_SIZE_MIN;
  1334. pr_info("Increased ring_size to %d (min allowed)\n",
  1335. ring_size);
  1336. }
  1337. ret = vmbus_driver_register(&netvsc_drv);
  1338. if (ret)
  1339. return ret;
  1340. register_netdevice_notifier(&netvsc_netdev_notifier);
  1341. return 0;
  1342. }
  1343. MODULE_LICENSE("GPL");
  1344. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1345. module_init(netvsc_drv_init);
  1346. module_exit(netvsc_drv_exit);