netvsc_drv.c 43 KB

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