netvsc_drv.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  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 <linux/rtnetlink.h>
  36. #include <linux/netpoll.h>
  37. #include <net/arp.h>
  38. #include <net/route.h>
  39. #include <net/sock.h>
  40. #include <net/pkt_sched.h>
  41. #include <net/checksum.h>
  42. #include <net/ip6_checksum.h>
  43. #include "hyperv_net.h"
  44. #define RING_SIZE_MIN 64
  45. #define NETVSC_MIN_TX_SECTIONS 10
  46. #define NETVSC_DEFAULT_TX 192 /* ~1M */
  47. #define NETVSC_MIN_RX_SECTIONS 10 /* ~64K */
  48. #define NETVSC_DEFAULT_RX 10485 /* Max ~16M */
  49. #define LINKCHANGE_INT (2 * HZ)
  50. #define VF_TAKEOVER_INT (HZ / 10)
  51. static int ring_size = 128;
  52. module_param(ring_size, int, S_IRUGO);
  53. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  54. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  55. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  56. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  57. NETIF_MSG_TX_ERR;
  58. static int debug = -1;
  59. module_param(debug, int, S_IRUGO);
  60. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  61. static void netvsc_set_multicast_list(struct net_device *net)
  62. {
  63. struct net_device_context *net_device_ctx = netdev_priv(net);
  64. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  65. rndis_filter_update(nvdev);
  66. }
  67. static int netvsc_open(struct net_device *net)
  68. {
  69. struct net_device_context *ndev_ctx = netdev_priv(net);
  70. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  71. struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
  72. struct rndis_device *rdev;
  73. int ret = 0;
  74. netif_carrier_off(net);
  75. /* Open up the device */
  76. ret = rndis_filter_open(nvdev);
  77. if (ret != 0) {
  78. netdev_err(net, "unable to open device (ret %d).\n", ret);
  79. return ret;
  80. }
  81. netif_tx_wake_all_queues(net);
  82. rdev = nvdev->extension;
  83. if (!rdev->link_state)
  84. netif_carrier_on(net);
  85. if (vf_netdev) {
  86. /* Setting synthetic device up transparently sets
  87. * slave as up. If open fails, then slave will be
  88. * still be offline (and not used).
  89. */
  90. ret = dev_open(vf_netdev);
  91. if (ret)
  92. netdev_warn(net,
  93. "unable to open slave: %s: %d\n",
  94. vf_netdev->name, ret);
  95. }
  96. return 0;
  97. }
  98. static int netvsc_close(struct net_device *net)
  99. {
  100. struct net_device_context *net_device_ctx = netdev_priv(net);
  101. struct net_device *vf_netdev
  102. = rtnl_dereference(net_device_ctx->vf_netdev);
  103. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  104. int ret = 0;
  105. u32 aread, i, msec = 10, retry = 0, retry_max = 20;
  106. struct vmbus_channel *chn;
  107. netif_tx_disable(net);
  108. /* No need to close rndis filter if it is removed already */
  109. if (!nvdev)
  110. goto out;
  111. ret = rndis_filter_close(nvdev);
  112. if (ret != 0) {
  113. netdev_err(net, "unable to close device (ret %d).\n", ret);
  114. return ret;
  115. }
  116. /* Ensure pending bytes in ring are read */
  117. while (true) {
  118. aread = 0;
  119. for (i = 0; i < nvdev->num_chn; i++) {
  120. chn = nvdev->chan_table[i].channel;
  121. if (!chn)
  122. continue;
  123. aread = hv_get_bytes_to_read(&chn->inbound);
  124. if (aread)
  125. break;
  126. aread = hv_get_bytes_to_read(&chn->outbound);
  127. if (aread)
  128. break;
  129. }
  130. retry++;
  131. if (retry > retry_max || aread == 0)
  132. break;
  133. msleep(msec);
  134. if (msec < 1000)
  135. msec *= 2;
  136. }
  137. if (aread) {
  138. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  139. ret = -ETIMEDOUT;
  140. }
  141. out:
  142. if (vf_netdev)
  143. dev_close(vf_netdev);
  144. return ret;
  145. }
  146. static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
  147. int pkt_type)
  148. {
  149. struct rndis_packet *rndis_pkt;
  150. struct rndis_per_packet_info *ppi;
  151. rndis_pkt = &msg->msg.pkt;
  152. rndis_pkt->data_offset += ppi_size;
  153. ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
  154. rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
  155. ppi->size = ppi_size;
  156. ppi->type = pkt_type;
  157. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  158. rndis_pkt->per_pkt_info_len += ppi_size;
  159. return ppi;
  160. }
  161. /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
  162. * packets. We can use ethtool to change UDP hash level when necessary.
  163. */
  164. static inline u32 netvsc_get_hash(
  165. struct sk_buff *skb,
  166. const struct net_device_context *ndc)
  167. {
  168. struct flow_keys flow;
  169. u32 hash, pkt_proto = 0;
  170. static u32 hashrnd __read_mostly;
  171. net_get_random_once(&hashrnd, sizeof(hashrnd));
  172. if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
  173. return 0;
  174. switch (flow.basic.ip_proto) {
  175. case IPPROTO_TCP:
  176. if (flow.basic.n_proto == htons(ETH_P_IP))
  177. pkt_proto = HV_TCP4_L4HASH;
  178. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  179. pkt_proto = HV_TCP6_L4HASH;
  180. break;
  181. case IPPROTO_UDP:
  182. if (flow.basic.n_proto == htons(ETH_P_IP))
  183. pkt_proto = HV_UDP4_L4HASH;
  184. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  185. pkt_proto = HV_UDP6_L4HASH;
  186. break;
  187. }
  188. if (pkt_proto & ndc->l4_hash) {
  189. return skb_get_hash(skb);
  190. } else {
  191. if (flow.basic.n_proto == htons(ETH_P_IP))
  192. hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
  193. else if (flow.basic.n_proto == htons(ETH_P_IPV6))
  194. hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
  195. else
  196. hash = 0;
  197. skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
  198. }
  199. return hash;
  200. }
  201. static inline int netvsc_get_tx_queue(struct net_device *ndev,
  202. struct sk_buff *skb, int old_idx)
  203. {
  204. const struct net_device_context *ndc = netdev_priv(ndev);
  205. struct sock *sk = skb->sk;
  206. int q_idx;
  207. q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
  208. (VRSS_SEND_TAB_SIZE - 1)];
  209. /* If queue index changed record the new value */
  210. if (q_idx != old_idx &&
  211. sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
  212. sk_tx_queue_set(sk, q_idx);
  213. return q_idx;
  214. }
  215. /*
  216. * Select queue for transmit.
  217. *
  218. * If a valid queue has already been assigned, then use that.
  219. * Otherwise compute tx queue based on hash and the send table.
  220. *
  221. * This is basically similar to default (__netdev_pick_tx) with the added step
  222. * of using the host send_table when no other queue has been assigned.
  223. *
  224. * TODO support XPS - but get_xps_queue not exported
  225. */
  226. static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
  227. {
  228. int q_idx = sk_tx_queue_get(skb->sk);
  229. if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
  230. /* If forwarding a packet, we use the recorded queue when
  231. * available for better cache locality.
  232. */
  233. if (skb_rx_queue_recorded(skb))
  234. q_idx = skb_get_rx_queue(skb);
  235. else
  236. q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
  237. }
  238. return q_idx;
  239. }
  240. static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
  241. void *accel_priv,
  242. select_queue_fallback_t fallback)
  243. {
  244. struct net_device_context *ndc = netdev_priv(ndev);
  245. struct net_device *vf_netdev;
  246. u16 txq;
  247. rcu_read_lock();
  248. vf_netdev = rcu_dereference(ndc->vf_netdev);
  249. if (vf_netdev) {
  250. txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
  251. qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
  252. } else {
  253. txq = netvsc_pick_tx(ndev, skb);
  254. }
  255. rcu_read_unlock();
  256. while (unlikely(txq >= ndev->real_num_tx_queues))
  257. txq -= ndev->real_num_tx_queues;
  258. return txq;
  259. }
  260. static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
  261. struct hv_page_buffer *pb)
  262. {
  263. int j = 0;
  264. /* Deal with compund pages by ignoring unused part
  265. * of the page.
  266. */
  267. page += (offset >> PAGE_SHIFT);
  268. offset &= ~PAGE_MASK;
  269. while (len > 0) {
  270. unsigned long bytes;
  271. bytes = PAGE_SIZE - offset;
  272. if (bytes > len)
  273. bytes = len;
  274. pb[j].pfn = page_to_pfn(page);
  275. pb[j].offset = offset;
  276. pb[j].len = bytes;
  277. offset += bytes;
  278. len -= bytes;
  279. if (offset == PAGE_SIZE && len) {
  280. page++;
  281. offset = 0;
  282. j++;
  283. }
  284. }
  285. return j + 1;
  286. }
  287. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  288. struct hv_netvsc_packet *packet,
  289. struct hv_page_buffer *pb)
  290. {
  291. u32 slots_used = 0;
  292. char *data = skb->data;
  293. int frags = skb_shinfo(skb)->nr_frags;
  294. int i;
  295. /* The packet is laid out thus:
  296. * 1. hdr: RNDIS header and PPI
  297. * 2. skb linear data
  298. * 3. skb fragment data
  299. */
  300. slots_used += fill_pg_buf(virt_to_page(hdr),
  301. offset_in_page(hdr),
  302. len, &pb[slots_used]);
  303. packet->rmsg_size = len;
  304. packet->rmsg_pgcnt = slots_used;
  305. slots_used += fill_pg_buf(virt_to_page(data),
  306. offset_in_page(data),
  307. skb_headlen(skb), &pb[slots_used]);
  308. for (i = 0; i < frags; i++) {
  309. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  310. slots_used += fill_pg_buf(skb_frag_page(frag),
  311. frag->page_offset,
  312. skb_frag_size(frag), &pb[slots_used]);
  313. }
  314. return slots_used;
  315. }
  316. static int count_skb_frag_slots(struct sk_buff *skb)
  317. {
  318. int i, frags = skb_shinfo(skb)->nr_frags;
  319. int pages = 0;
  320. for (i = 0; i < frags; i++) {
  321. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  322. unsigned long size = skb_frag_size(frag);
  323. unsigned long offset = frag->page_offset;
  324. /* Skip unused frames from start of page */
  325. offset &= ~PAGE_MASK;
  326. pages += PFN_UP(offset + size);
  327. }
  328. return pages;
  329. }
  330. static int netvsc_get_slots(struct sk_buff *skb)
  331. {
  332. char *data = skb->data;
  333. unsigned int offset = offset_in_page(data);
  334. unsigned int len = skb_headlen(skb);
  335. int slots;
  336. int frag_slots;
  337. slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
  338. frag_slots = count_skb_frag_slots(skb);
  339. return slots + frag_slots;
  340. }
  341. static u32 net_checksum_info(struct sk_buff *skb)
  342. {
  343. if (skb->protocol == htons(ETH_P_IP)) {
  344. struct iphdr *ip = ip_hdr(skb);
  345. if (ip->protocol == IPPROTO_TCP)
  346. return TRANSPORT_INFO_IPV4_TCP;
  347. else if (ip->protocol == IPPROTO_UDP)
  348. return TRANSPORT_INFO_IPV4_UDP;
  349. } else {
  350. struct ipv6hdr *ip6 = ipv6_hdr(skb);
  351. if (ip6->nexthdr == IPPROTO_TCP)
  352. return TRANSPORT_INFO_IPV6_TCP;
  353. else if (ip6->nexthdr == IPPROTO_UDP)
  354. return TRANSPORT_INFO_IPV6_UDP;
  355. }
  356. return TRANSPORT_INFO_NOT_IP;
  357. }
  358. /* Send skb on the slave VF device. */
  359. static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
  360. struct sk_buff *skb)
  361. {
  362. struct net_device_context *ndev_ctx = netdev_priv(net);
  363. unsigned int len = skb->len;
  364. int rc;
  365. skb->dev = vf_netdev;
  366. skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
  367. rc = dev_queue_xmit(skb);
  368. if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
  369. struct netvsc_vf_pcpu_stats *pcpu_stats
  370. = this_cpu_ptr(ndev_ctx->vf_stats);
  371. u64_stats_update_begin(&pcpu_stats->syncp);
  372. pcpu_stats->tx_packets++;
  373. pcpu_stats->tx_bytes += len;
  374. u64_stats_update_end(&pcpu_stats->syncp);
  375. } else {
  376. this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
  377. }
  378. return rc;
  379. }
  380. static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
  381. {
  382. struct net_device_context *net_device_ctx = netdev_priv(net);
  383. struct hv_netvsc_packet *packet = NULL;
  384. int ret;
  385. unsigned int num_data_pgs;
  386. struct rndis_message *rndis_msg;
  387. struct rndis_packet *rndis_pkt;
  388. struct net_device *vf_netdev;
  389. u32 rndis_msg_size;
  390. struct rndis_per_packet_info *ppi;
  391. u32 hash;
  392. struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
  393. /* if VF is present and up then redirect packets
  394. * already called with rcu_read_lock_bh
  395. */
  396. vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
  397. if (vf_netdev && netif_running(vf_netdev) &&
  398. !netpoll_tx_running(net))
  399. return netvsc_vf_xmit(net, vf_netdev, skb);
  400. /* We will atmost need two pages to describe the rndis
  401. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  402. * of pages in a single packet. If skb is scattered around
  403. * more pages we try linearizing it.
  404. */
  405. num_data_pgs = netvsc_get_slots(skb) + 2;
  406. if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
  407. ++net_device_ctx->eth_stats.tx_scattered;
  408. if (skb_linearize(skb))
  409. goto no_memory;
  410. num_data_pgs = netvsc_get_slots(skb) + 2;
  411. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  412. ++net_device_ctx->eth_stats.tx_too_big;
  413. goto drop;
  414. }
  415. }
  416. /*
  417. * Place the rndis header in the skb head room and
  418. * the skb->cb will be used for hv_netvsc_packet
  419. * structure.
  420. */
  421. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  422. if (ret)
  423. goto no_memory;
  424. /* Use the skb control buffer for building up the packet */
  425. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  426. FIELD_SIZEOF(struct sk_buff, cb));
  427. packet = (struct hv_netvsc_packet *)skb->cb;
  428. packet->q_idx = skb_get_queue_mapping(skb);
  429. packet->total_data_buflen = skb->len;
  430. packet->total_bytes = skb->len;
  431. packet->total_packets = 1;
  432. rndis_msg = (struct rndis_message *)skb->head;
  433. memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
  434. /* Add the rndis header */
  435. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  436. rndis_msg->msg_len = packet->total_data_buflen;
  437. rndis_pkt = &rndis_msg->msg.pkt;
  438. rndis_pkt->data_offset = sizeof(struct rndis_packet);
  439. rndis_pkt->data_len = packet->total_data_buflen;
  440. rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
  441. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  442. hash = skb_get_hash_raw(skb);
  443. if (hash != 0 && net->real_num_tx_queues > 1) {
  444. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  445. ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  446. NBL_HASH_VALUE);
  447. *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
  448. }
  449. if (skb_vlan_tag_present(skb)) {
  450. struct ndis_pkt_8021q_info *vlan;
  451. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  452. ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  453. IEEE_8021Q_INFO);
  454. vlan = (void *)ppi + ppi->ppi_offset;
  455. vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
  456. vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
  457. VLAN_PRIO_SHIFT;
  458. }
  459. if (skb_is_gso(skb)) {
  460. struct ndis_tcp_lso_info *lso_info;
  461. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  462. ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  463. TCP_LARGESEND_PKTINFO);
  464. lso_info = (void *)ppi + ppi->ppi_offset;
  465. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  466. if (skb->protocol == htons(ETH_P_IP)) {
  467. lso_info->lso_v2_transmit.ip_version =
  468. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  469. ip_hdr(skb)->tot_len = 0;
  470. ip_hdr(skb)->check = 0;
  471. tcp_hdr(skb)->check =
  472. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  473. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  474. } else {
  475. lso_info->lso_v2_transmit.ip_version =
  476. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  477. ipv6_hdr(skb)->payload_len = 0;
  478. tcp_hdr(skb)->check =
  479. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  480. &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  481. }
  482. lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
  483. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  484. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  485. if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
  486. struct ndis_tcp_ip_checksum_info *csum_info;
  487. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  488. ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  489. TCPIP_CHKSUM_PKTINFO);
  490. csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
  491. ppi->ppi_offset);
  492. csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
  493. if (skb->protocol == htons(ETH_P_IP)) {
  494. csum_info->transmit.is_ipv4 = 1;
  495. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  496. csum_info->transmit.tcp_checksum = 1;
  497. else
  498. csum_info->transmit.udp_checksum = 1;
  499. } else {
  500. csum_info->transmit.is_ipv6 = 1;
  501. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  502. csum_info->transmit.tcp_checksum = 1;
  503. else
  504. csum_info->transmit.udp_checksum = 1;
  505. }
  506. } else {
  507. /* Can't do offload of this type of checksum */
  508. if (skb_checksum_help(skb))
  509. goto drop;
  510. }
  511. }
  512. /* Start filling in the page buffers with the rndis hdr */
  513. rndis_msg->msg_len += rndis_msg_size;
  514. packet->total_data_buflen = rndis_msg->msg_len;
  515. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  516. skb, packet, pb);
  517. /* timestamp packet in software */
  518. skb_tx_timestamp(skb);
  519. ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
  520. if (likely(ret == 0))
  521. return NETDEV_TX_OK;
  522. if (ret == -EAGAIN) {
  523. ++net_device_ctx->eth_stats.tx_busy;
  524. return NETDEV_TX_BUSY;
  525. }
  526. if (ret == -ENOSPC)
  527. ++net_device_ctx->eth_stats.tx_no_space;
  528. drop:
  529. dev_kfree_skb_any(skb);
  530. net->stats.tx_dropped++;
  531. return NETDEV_TX_OK;
  532. no_memory:
  533. ++net_device_ctx->eth_stats.tx_no_memory;
  534. goto drop;
  535. }
  536. /*
  537. * netvsc_linkstatus_callback - Link up/down notification
  538. */
  539. void netvsc_linkstatus_callback(struct hv_device *device_obj,
  540. struct rndis_message *resp)
  541. {
  542. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  543. struct net_device *net;
  544. struct net_device_context *ndev_ctx;
  545. struct netvsc_reconfig *event;
  546. unsigned long flags;
  547. net = hv_get_drvdata(device_obj);
  548. if (!net)
  549. return;
  550. ndev_ctx = netdev_priv(net);
  551. /* Update the physical link speed when changing to another vSwitch */
  552. if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
  553. u32 speed;
  554. speed = *(u32 *)((void *)indicate
  555. + indicate->status_buf_offset) / 10000;
  556. ndev_ctx->speed = speed;
  557. return;
  558. }
  559. /* Handle these link change statuses below */
  560. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  561. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  562. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  563. return;
  564. if (net->reg_state != NETREG_REGISTERED)
  565. return;
  566. event = kzalloc(sizeof(*event), GFP_ATOMIC);
  567. if (!event)
  568. return;
  569. event->event = indicate->status;
  570. spin_lock_irqsave(&ndev_ctx->lock, flags);
  571. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  572. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  573. schedule_delayed_work(&ndev_ctx->dwork, 0);
  574. }
  575. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  576. struct napi_struct *napi,
  577. const struct ndis_tcp_ip_checksum_info *csum_info,
  578. const struct ndis_pkt_8021q_info *vlan,
  579. void *data, u32 buflen)
  580. {
  581. struct sk_buff *skb;
  582. skb = napi_alloc_skb(napi, buflen);
  583. if (!skb)
  584. return skb;
  585. /*
  586. * Copy to skb. This copy is needed here since the memory pointed by
  587. * hv_netvsc_packet cannot be deallocated
  588. */
  589. skb_put_data(skb, data, buflen);
  590. skb->protocol = eth_type_trans(skb, net);
  591. /* skb is already created with CHECKSUM_NONE */
  592. skb_checksum_none_assert(skb);
  593. /*
  594. * In Linux, the IP checksum is always checked.
  595. * Do L4 checksum offload if enabled and present.
  596. */
  597. if (csum_info && (net->features & NETIF_F_RXCSUM)) {
  598. if (csum_info->receive.tcp_checksum_succeeded ||
  599. csum_info->receive.udp_checksum_succeeded)
  600. skb->ip_summed = CHECKSUM_UNNECESSARY;
  601. }
  602. if (vlan) {
  603. u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
  604. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  605. vlan_tci);
  606. }
  607. return skb;
  608. }
  609. /*
  610. * netvsc_recv_callback - Callback when we receive a packet from the
  611. * "wire" on the specified device.
  612. */
  613. int netvsc_recv_callback(struct net_device *net,
  614. struct vmbus_channel *channel,
  615. void *data, u32 len,
  616. const struct ndis_tcp_ip_checksum_info *csum_info,
  617. const struct ndis_pkt_8021q_info *vlan)
  618. {
  619. struct net_device_context *net_device_ctx = netdev_priv(net);
  620. struct netvsc_device *net_device;
  621. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  622. struct netvsc_channel *nvchan;
  623. struct sk_buff *skb;
  624. struct netvsc_stats *rx_stats;
  625. if (net->reg_state != NETREG_REGISTERED)
  626. return NVSP_STAT_FAIL;
  627. rcu_read_lock();
  628. net_device = rcu_dereference(net_device_ctx->nvdev);
  629. if (unlikely(!net_device))
  630. goto drop;
  631. nvchan = &net_device->chan_table[q_idx];
  632. /* Allocate a skb - TODO direct I/O to pages? */
  633. skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
  634. csum_info, vlan, data, len);
  635. if (unlikely(!skb)) {
  636. drop:
  637. ++net->stats.rx_dropped;
  638. rcu_read_unlock();
  639. return NVSP_STAT_FAIL;
  640. }
  641. skb_record_rx_queue(skb, q_idx);
  642. /*
  643. * Even if injecting the packet, record the statistics
  644. * on the synthetic device because modifying the VF device
  645. * statistics will not work correctly.
  646. */
  647. rx_stats = &nvchan->rx_stats;
  648. u64_stats_update_begin(&rx_stats->syncp);
  649. rx_stats->packets++;
  650. rx_stats->bytes += len;
  651. if (skb->pkt_type == PACKET_BROADCAST)
  652. ++rx_stats->broadcast;
  653. else if (skb->pkt_type == PACKET_MULTICAST)
  654. ++rx_stats->multicast;
  655. u64_stats_update_end(&rx_stats->syncp);
  656. napi_gro_receive(&nvchan->napi, skb);
  657. rcu_read_unlock();
  658. return 0;
  659. }
  660. static void netvsc_get_drvinfo(struct net_device *net,
  661. struct ethtool_drvinfo *info)
  662. {
  663. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  664. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  665. }
  666. static void netvsc_get_channels(struct net_device *net,
  667. struct ethtool_channels *channel)
  668. {
  669. struct net_device_context *net_device_ctx = netdev_priv(net);
  670. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  671. if (nvdev) {
  672. channel->max_combined = nvdev->max_chn;
  673. channel->combined_count = nvdev->num_chn;
  674. }
  675. }
  676. static int netvsc_set_channels(struct net_device *net,
  677. struct ethtool_channels *channels)
  678. {
  679. struct net_device_context *net_device_ctx = netdev_priv(net);
  680. struct hv_device *dev = net_device_ctx->device_ctx;
  681. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  682. unsigned int orig, count = channels->combined_count;
  683. struct netvsc_device_info device_info;
  684. bool was_opened;
  685. int ret = 0;
  686. /* We do not support separate count for rx, tx, or other */
  687. if (count == 0 ||
  688. channels->rx_count || channels->tx_count || channels->other_count)
  689. return -EINVAL;
  690. if (!nvdev || nvdev->destroy)
  691. return -ENODEV;
  692. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  693. return -EINVAL;
  694. if (count > nvdev->max_chn)
  695. return -EINVAL;
  696. orig = nvdev->num_chn;
  697. was_opened = rndis_filter_opened(nvdev);
  698. if (was_opened)
  699. rndis_filter_close(nvdev);
  700. memset(&device_info, 0, sizeof(device_info));
  701. device_info.num_chn = count;
  702. device_info.ring_size = ring_size;
  703. device_info.send_sections = nvdev->send_section_cnt;
  704. device_info.send_section_size = nvdev->send_section_size;
  705. device_info.recv_sections = nvdev->recv_section_cnt;
  706. device_info.recv_section_size = nvdev->recv_section_size;
  707. rndis_filter_device_remove(dev, nvdev);
  708. nvdev = rndis_filter_device_add(dev, &device_info);
  709. if (IS_ERR(nvdev)) {
  710. ret = PTR_ERR(nvdev);
  711. device_info.num_chn = orig;
  712. nvdev = rndis_filter_device_add(dev, &device_info);
  713. if (IS_ERR(nvdev)) {
  714. netdev_err(net, "restoring channel setting failed: %ld\n",
  715. PTR_ERR(nvdev));
  716. return ret;
  717. }
  718. }
  719. if (was_opened)
  720. rndis_filter_open(nvdev);
  721. /* We may have missed link change notifications */
  722. net_device_ctx->last_reconfig = 0;
  723. schedule_delayed_work(&net_device_ctx->dwork, 0);
  724. return ret;
  725. }
  726. static bool
  727. netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
  728. {
  729. struct ethtool_link_ksettings diff1 = *cmd;
  730. struct ethtool_link_ksettings diff2 = {};
  731. diff1.base.speed = 0;
  732. diff1.base.duplex = 0;
  733. /* advertising and cmd are usually set */
  734. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  735. diff1.base.cmd = 0;
  736. /* We set port to PORT_OTHER */
  737. diff2.base.port = PORT_OTHER;
  738. return !memcmp(&diff1, &diff2, sizeof(diff1));
  739. }
  740. static void netvsc_init_settings(struct net_device *dev)
  741. {
  742. struct net_device_context *ndc = netdev_priv(dev);
  743. ndc->l4_hash = HV_DEFAULT_L4HASH;
  744. ndc->speed = SPEED_UNKNOWN;
  745. ndc->duplex = DUPLEX_FULL;
  746. }
  747. static int netvsc_get_link_ksettings(struct net_device *dev,
  748. struct ethtool_link_ksettings *cmd)
  749. {
  750. struct net_device_context *ndc = netdev_priv(dev);
  751. cmd->base.speed = ndc->speed;
  752. cmd->base.duplex = ndc->duplex;
  753. cmd->base.port = PORT_OTHER;
  754. return 0;
  755. }
  756. static int netvsc_set_link_ksettings(struct net_device *dev,
  757. const struct ethtool_link_ksettings *cmd)
  758. {
  759. struct net_device_context *ndc = netdev_priv(dev);
  760. u32 speed;
  761. speed = cmd->base.speed;
  762. if (!ethtool_validate_speed(speed) ||
  763. !ethtool_validate_duplex(cmd->base.duplex) ||
  764. !netvsc_validate_ethtool_ss_cmd(cmd))
  765. return -EINVAL;
  766. ndc->speed = speed;
  767. ndc->duplex = cmd->base.duplex;
  768. return 0;
  769. }
  770. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  771. {
  772. struct net_device_context *ndevctx = netdev_priv(ndev);
  773. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  774. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  775. struct hv_device *hdev = ndevctx->device_ctx;
  776. int orig_mtu = ndev->mtu;
  777. struct netvsc_device_info device_info;
  778. bool was_opened;
  779. int ret = 0;
  780. if (!nvdev || nvdev->destroy)
  781. return -ENODEV;
  782. /* Change MTU of underlying VF netdev first. */
  783. if (vf_netdev) {
  784. ret = dev_set_mtu(vf_netdev, mtu);
  785. if (ret)
  786. return ret;
  787. }
  788. netif_device_detach(ndev);
  789. was_opened = rndis_filter_opened(nvdev);
  790. if (was_opened)
  791. rndis_filter_close(nvdev);
  792. memset(&device_info, 0, sizeof(device_info));
  793. device_info.ring_size = ring_size;
  794. device_info.num_chn = nvdev->num_chn;
  795. device_info.send_sections = nvdev->send_section_cnt;
  796. device_info.send_section_size = nvdev->send_section_size;
  797. device_info.recv_sections = nvdev->recv_section_cnt;
  798. device_info.recv_section_size = nvdev->recv_section_size;
  799. rndis_filter_device_remove(hdev, nvdev);
  800. ndev->mtu = mtu;
  801. nvdev = rndis_filter_device_add(hdev, &device_info);
  802. if (IS_ERR(nvdev)) {
  803. ret = PTR_ERR(nvdev);
  804. /* Attempt rollback to original MTU */
  805. ndev->mtu = orig_mtu;
  806. nvdev = rndis_filter_device_add(hdev, &device_info);
  807. if (vf_netdev)
  808. dev_set_mtu(vf_netdev, orig_mtu);
  809. if (IS_ERR(nvdev)) {
  810. netdev_err(ndev, "restoring mtu failed: %ld\n",
  811. PTR_ERR(nvdev));
  812. return ret;
  813. }
  814. }
  815. if (was_opened)
  816. rndis_filter_open(nvdev);
  817. netif_device_attach(ndev);
  818. /* We may have missed link change notifications */
  819. schedule_delayed_work(&ndevctx->dwork, 0);
  820. return ret;
  821. }
  822. static void netvsc_get_vf_stats(struct net_device *net,
  823. struct netvsc_vf_pcpu_stats *tot)
  824. {
  825. struct net_device_context *ndev_ctx = netdev_priv(net);
  826. int i;
  827. memset(tot, 0, sizeof(*tot));
  828. for_each_possible_cpu(i) {
  829. const struct netvsc_vf_pcpu_stats *stats
  830. = per_cpu_ptr(ndev_ctx->vf_stats, i);
  831. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  832. unsigned int start;
  833. do {
  834. start = u64_stats_fetch_begin_irq(&stats->syncp);
  835. rx_packets = stats->rx_packets;
  836. tx_packets = stats->tx_packets;
  837. rx_bytes = stats->rx_bytes;
  838. tx_bytes = stats->tx_bytes;
  839. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  840. tot->rx_packets += rx_packets;
  841. tot->tx_packets += tx_packets;
  842. tot->rx_bytes += rx_bytes;
  843. tot->tx_bytes += tx_bytes;
  844. tot->tx_dropped += stats->tx_dropped;
  845. }
  846. }
  847. static void netvsc_get_stats64(struct net_device *net,
  848. struct rtnl_link_stats64 *t)
  849. {
  850. struct net_device_context *ndev_ctx = netdev_priv(net);
  851. struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
  852. struct netvsc_vf_pcpu_stats vf_tot;
  853. int i;
  854. if (!nvdev)
  855. return;
  856. netdev_stats_to_stats64(t, &net->stats);
  857. netvsc_get_vf_stats(net, &vf_tot);
  858. t->rx_packets += vf_tot.rx_packets;
  859. t->tx_packets += vf_tot.tx_packets;
  860. t->rx_bytes += vf_tot.rx_bytes;
  861. t->tx_bytes += vf_tot.tx_bytes;
  862. t->tx_dropped += vf_tot.tx_dropped;
  863. for (i = 0; i < nvdev->num_chn; i++) {
  864. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  865. const struct netvsc_stats *stats;
  866. u64 packets, bytes, multicast;
  867. unsigned int start;
  868. stats = &nvchan->tx_stats;
  869. do {
  870. start = u64_stats_fetch_begin_irq(&stats->syncp);
  871. packets = stats->packets;
  872. bytes = stats->bytes;
  873. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  874. t->tx_bytes += bytes;
  875. t->tx_packets += packets;
  876. stats = &nvchan->rx_stats;
  877. do {
  878. start = u64_stats_fetch_begin_irq(&stats->syncp);
  879. packets = stats->packets;
  880. bytes = stats->bytes;
  881. multicast = stats->multicast + stats->broadcast;
  882. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  883. t->rx_bytes += bytes;
  884. t->rx_packets += packets;
  885. t->multicast += multicast;
  886. }
  887. }
  888. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  889. {
  890. struct net_device_context *ndc = netdev_priv(ndev);
  891. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  892. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  893. struct sockaddr *addr = p;
  894. int err;
  895. err = eth_prepare_mac_addr_change(ndev, p);
  896. if (err)
  897. return err;
  898. if (!nvdev)
  899. return -ENODEV;
  900. if (vf_netdev) {
  901. err = dev_set_mac_address(vf_netdev, addr);
  902. if (err)
  903. return err;
  904. }
  905. err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
  906. if (!err) {
  907. eth_commit_mac_addr_change(ndev, p);
  908. } else if (vf_netdev) {
  909. /* rollback change on VF */
  910. memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
  911. dev_set_mac_address(vf_netdev, addr);
  912. }
  913. return err;
  914. }
  915. static const struct {
  916. char name[ETH_GSTRING_LEN];
  917. u16 offset;
  918. } netvsc_stats[] = {
  919. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  920. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  921. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  922. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  923. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  924. { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
  925. { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
  926. { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
  927. { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
  928. }, vf_stats[] = {
  929. { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
  930. { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
  931. { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
  932. { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
  933. { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
  934. };
  935. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  936. #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
  937. /* 4 statistics per queue (rx/tx packets/bytes) */
  938. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
  939. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  940. {
  941. struct net_device_context *ndc = netdev_priv(dev);
  942. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  943. if (!nvdev)
  944. return -ENODEV;
  945. switch (string_set) {
  946. case ETH_SS_STATS:
  947. return NETVSC_GLOBAL_STATS_LEN
  948. + NETVSC_VF_STATS_LEN
  949. + NETVSC_QUEUE_STATS_LEN(nvdev);
  950. default:
  951. return -EINVAL;
  952. }
  953. }
  954. static void netvsc_get_ethtool_stats(struct net_device *dev,
  955. struct ethtool_stats *stats, u64 *data)
  956. {
  957. struct net_device_context *ndc = netdev_priv(dev);
  958. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  959. const void *nds = &ndc->eth_stats;
  960. const struct netvsc_stats *qstats;
  961. struct netvsc_vf_pcpu_stats sum;
  962. unsigned int start;
  963. u64 packets, bytes;
  964. int i, j;
  965. if (!nvdev)
  966. return;
  967. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  968. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  969. netvsc_get_vf_stats(dev, &sum);
  970. for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
  971. data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
  972. for (j = 0; j < nvdev->num_chn; j++) {
  973. qstats = &nvdev->chan_table[j].tx_stats;
  974. do {
  975. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  976. packets = qstats->packets;
  977. bytes = qstats->bytes;
  978. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  979. data[i++] = packets;
  980. data[i++] = bytes;
  981. qstats = &nvdev->chan_table[j].rx_stats;
  982. do {
  983. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  984. packets = qstats->packets;
  985. bytes = qstats->bytes;
  986. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  987. data[i++] = packets;
  988. data[i++] = bytes;
  989. }
  990. }
  991. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  992. {
  993. struct net_device_context *ndc = netdev_priv(dev);
  994. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  995. u8 *p = data;
  996. int i;
  997. if (!nvdev)
  998. return;
  999. switch (stringset) {
  1000. case ETH_SS_STATS:
  1001. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
  1002. memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
  1003. p += ETH_GSTRING_LEN;
  1004. }
  1005. for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
  1006. memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
  1007. p += ETH_GSTRING_LEN;
  1008. }
  1009. for (i = 0; i < nvdev->num_chn; i++) {
  1010. sprintf(p, "tx_queue_%u_packets", i);
  1011. p += ETH_GSTRING_LEN;
  1012. sprintf(p, "tx_queue_%u_bytes", i);
  1013. p += ETH_GSTRING_LEN;
  1014. sprintf(p, "rx_queue_%u_packets", i);
  1015. p += ETH_GSTRING_LEN;
  1016. sprintf(p, "rx_queue_%u_bytes", i);
  1017. p += ETH_GSTRING_LEN;
  1018. }
  1019. break;
  1020. }
  1021. }
  1022. static int
  1023. netvsc_get_rss_hash_opts(struct net_device_context *ndc,
  1024. struct ethtool_rxnfc *info)
  1025. {
  1026. const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1027. info->data = RXH_IP_SRC | RXH_IP_DST;
  1028. switch (info->flow_type) {
  1029. case TCP_V4_FLOW:
  1030. if (ndc->l4_hash & HV_TCP4_L4HASH)
  1031. info->data |= l4_flag;
  1032. break;
  1033. case TCP_V6_FLOW:
  1034. if (ndc->l4_hash & HV_TCP6_L4HASH)
  1035. info->data |= l4_flag;
  1036. break;
  1037. case UDP_V4_FLOW:
  1038. if (ndc->l4_hash & HV_UDP4_L4HASH)
  1039. info->data |= l4_flag;
  1040. break;
  1041. case UDP_V6_FLOW:
  1042. if (ndc->l4_hash & HV_UDP6_L4HASH)
  1043. info->data |= l4_flag;
  1044. break;
  1045. case IPV4_FLOW:
  1046. case IPV6_FLOW:
  1047. break;
  1048. default:
  1049. info->data = 0;
  1050. break;
  1051. }
  1052. return 0;
  1053. }
  1054. static int
  1055. netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  1056. u32 *rules)
  1057. {
  1058. struct net_device_context *ndc = netdev_priv(dev);
  1059. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1060. if (!nvdev)
  1061. return -ENODEV;
  1062. switch (info->cmd) {
  1063. case ETHTOOL_GRXRINGS:
  1064. info->data = nvdev->num_chn;
  1065. return 0;
  1066. case ETHTOOL_GRXFH:
  1067. return netvsc_get_rss_hash_opts(ndc, info);
  1068. }
  1069. return -EOPNOTSUPP;
  1070. }
  1071. static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
  1072. struct ethtool_rxnfc *info)
  1073. {
  1074. if (info->data == (RXH_IP_SRC | RXH_IP_DST |
  1075. RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  1076. switch (info->flow_type) {
  1077. case TCP_V4_FLOW:
  1078. ndc->l4_hash |= HV_TCP4_L4HASH;
  1079. break;
  1080. case TCP_V6_FLOW:
  1081. ndc->l4_hash |= HV_TCP6_L4HASH;
  1082. break;
  1083. case UDP_V4_FLOW:
  1084. ndc->l4_hash |= HV_UDP4_L4HASH;
  1085. break;
  1086. case UDP_V6_FLOW:
  1087. ndc->l4_hash |= HV_UDP6_L4HASH;
  1088. break;
  1089. default:
  1090. return -EOPNOTSUPP;
  1091. }
  1092. return 0;
  1093. }
  1094. if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
  1095. switch (info->flow_type) {
  1096. case TCP_V4_FLOW:
  1097. ndc->l4_hash &= ~HV_TCP4_L4HASH;
  1098. break;
  1099. case TCP_V6_FLOW:
  1100. ndc->l4_hash &= ~HV_TCP6_L4HASH;
  1101. break;
  1102. case UDP_V4_FLOW:
  1103. ndc->l4_hash &= ~HV_UDP4_L4HASH;
  1104. break;
  1105. case UDP_V6_FLOW:
  1106. ndc->l4_hash &= ~HV_UDP6_L4HASH;
  1107. break;
  1108. default:
  1109. return -EOPNOTSUPP;
  1110. }
  1111. return 0;
  1112. }
  1113. return -EOPNOTSUPP;
  1114. }
  1115. static int
  1116. netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
  1117. {
  1118. struct net_device_context *ndc = netdev_priv(ndev);
  1119. if (info->cmd == ETHTOOL_SRXFH)
  1120. return netvsc_set_rss_hash_opts(ndc, info);
  1121. return -EOPNOTSUPP;
  1122. }
  1123. #ifdef CONFIG_NET_POLL_CONTROLLER
  1124. static void netvsc_poll_controller(struct net_device *dev)
  1125. {
  1126. struct net_device_context *ndc = netdev_priv(dev);
  1127. struct netvsc_device *ndev;
  1128. int i;
  1129. rcu_read_lock();
  1130. ndev = rcu_dereference(ndc->nvdev);
  1131. if (ndev) {
  1132. for (i = 0; i < ndev->num_chn; i++) {
  1133. struct netvsc_channel *nvchan = &ndev->chan_table[i];
  1134. napi_schedule(&nvchan->napi);
  1135. }
  1136. }
  1137. rcu_read_unlock();
  1138. }
  1139. #endif
  1140. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  1141. {
  1142. return NETVSC_HASH_KEYLEN;
  1143. }
  1144. static u32 netvsc_rss_indir_size(struct net_device *dev)
  1145. {
  1146. return ITAB_NUM;
  1147. }
  1148. static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  1149. u8 *hfunc)
  1150. {
  1151. struct net_device_context *ndc = netdev_priv(dev);
  1152. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1153. struct rndis_device *rndis_dev;
  1154. int i;
  1155. if (!ndev)
  1156. return -ENODEV;
  1157. if (hfunc)
  1158. *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  1159. rndis_dev = ndev->extension;
  1160. if (indir) {
  1161. for (i = 0; i < ITAB_NUM; i++)
  1162. indir[i] = rndis_dev->rx_table[i];
  1163. }
  1164. if (key)
  1165. memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  1166. return 0;
  1167. }
  1168. static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
  1169. const u8 *key, const u8 hfunc)
  1170. {
  1171. struct net_device_context *ndc = netdev_priv(dev);
  1172. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1173. struct rndis_device *rndis_dev;
  1174. int i;
  1175. if (!ndev)
  1176. return -ENODEV;
  1177. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  1178. return -EOPNOTSUPP;
  1179. rndis_dev = ndev->extension;
  1180. if (indir) {
  1181. for (i = 0; i < ITAB_NUM; i++)
  1182. if (indir[i] >= ndev->num_chn)
  1183. return -EINVAL;
  1184. for (i = 0; i < ITAB_NUM; i++)
  1185. rndis_dev->rx_table[i] = indir[i];
  1186. }
  1187. if (!key) {
  1188. if (!indir)
  1189. return 0;
  1190. key = rndis_dev->rss_key;
  1191. }
  1192. return rndis_filter_set_rss_param(rndis_dev, key);
  1193. }
  1194. /* Hyper-V RNDIS protocol does not have ring in the HW sense.
  1195. * It does have pre-allocated receive area which is divided into sections.
  1196. */
  1197. static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
  1198. struct ethtool_ringparam *ring)
  1199. {
  1200. u32 max_buf_size;
  1201. ring->rx_pending = nvdev->recv_section_cnt;
  1202. ring->tx_pending = nvdev->send_section_cnt;
  1203. if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  1204. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  1205. else
  1206. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  1207. ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
  1208. ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
  1209. / nvdev->send_section_size;
  1210. }
  1211. static void netvsc_get_ringparam(struct net_device *ndev,
  1212. struct ethtool_ringparam *ring)
  1213. {
  1214. struct net_device_context *ndevctx = netdev_priv(ndev);
  1215. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1216. if (!nvdev)
  1217. return;
  1218. __netvsc_get_ringparam(nvdev, ring);
  1219. }
  1220. static int netvsc_set_ringparam(struct net_device *ndev,
  1221. struct ethtool_ringparam *ring)
  1222. {
  1223. struct net_device_context *ndevctx = netdev_priv(ndev);
  1224. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1225. struct hv_device *hdev = ndevctx->device_ctx;
  1226. struct netvsc_device_info device_info;
  1227. struct ethtool_ringparam orig;
  1228. u32 new_tx, new_rx;
  1229. bool was_opened;
  1230. int ret = 0;
  1231. if (!nvdev || nvdev->destroy)
  1232. return -ENODEV;
  1233. memset(&orig, 0, sizeof(orig));
  1234. __netvsc_get_ringparam(nvdev, &orig);
  1235. new_tx = clamp_t(u32, ring->tx_pending,
  1236. NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
  1237. new_rx = clamp_t(u32, ring->rx_pending,
  1238. NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
  1239. if (new_tx == orig.tx_pending &&
  1240. new_rx == orig.rx_pending)
  1241. return 0; /* no change */
  1242. memset(&device_info, 0, sizeof(device_info));
  1243. device_info.num_chn = nvdev->num_chn;
  1244. device_info.ring_size = ring_size;
  1245. device_info.send_sections = new_tx;
  1246. device_info.send_section_size = nvdev->send_section_size;
  1247. device_info.recv_sections = new_rx;
  1248. device_info.recv_section_size = nvdev->recv_section_size;
  1249. netif_device_detach(ndev);
  1250. was_opened = rndis_filter_opened(nvdev);
  1251. if (was_opened)
  1252. rndis_filter_close(nvdev);
  1253. rndis_filter_device_remove(hdev, nvdev);
  1254. nvdev = rndis_filter_device_add(hdev, &device_info);
  1255. if (IS_ERR(nvdev)) {
  1256. ret = PTR_ERR(nvdev);
  1257. device_info.send_sections = orig.tx_pending;
  1258. device_info.recv_sections = orig.rx_pending;
  1259. nvdev = rndis_filter_device_add(hdev, &device_info);
  1260. if (IS_ERR(nvdev)) {
  1261. netdev_err(ndev, "restoring ringparam failed: %ld\n",
  1262. PTR_ERR(nvdev));
  1263. return ret;
  1264. }
  1265. }
  1266. if (was_opened)
  1267. rndis_filter_open(nvdev);
  1268. netif_device_attach(ndev);
  1269. /* We may have missed link change notifications */
  1270. ndevctx->last_reconfig = 0;
  1271. schedule_delayed_work(&ndevctx->dwork, 0);
  1272. return ret;
  1273. }
  1274. static const struct ethtool_ops ethtool_ops = {
  1275. .get_drvinfo = netvsc_get_drvinfo,
  1276. .get_link = ethtool_op_get_link,
  1277. .get_ethtool_stats = netvsc_get_ethtool_stats,
  1278. .get_sset_count = netvsc_get_sset_count,
  1279. .get_strings = netvsc_get_strings,
  1280. .get_channels = netvsc_get_channels,
  1281. .set_channels = netvsc_set_channels,
  1282. .get_ts_info = ethtool_op_get_ts_info,
  1283. .get_rxnfc = netvsc_get_rxnfc,
  1284. .set_rxnfc = netvsc_set_rxnfc,
  1285. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  1286. .get_rxfh_indir_size = netvsc_rss_indir_size,
  1287. .get_rxfh = netvsc_get_rxfh,
  1288. .set_rxfh = netvsc_set_rxfh,
  1289. .get_link_ksettings = netvsc_get_link_ksettings,
  1290. .set_link_ksettings = netvsc_set_link_ksettings,
  1291. .get_ringparam = netvsc_get_ringparam,
  1292. .set_ringparam = netvsc_set_ringparam,
  1293. };
  1294. static const struct net_device_ops device_ops = {
  1295. .ndo_open = netvsc_open,
  1296. .ndo_stop = netvsc_close,
  1297. .ndo_start_xmit = netvsc_start_xmit,
  1298. .ndo_set_rx_mode = netvsc_set_multicast_list,
  1299. .ndo_change_mtu = netvsc_change_mtu,
  1300. .ndo_validate_addr = eth_validate_addr,
  1301. .ndo_set_mac_address = netvsc_set_mac_addr,
  1302. .ndo_select_queue = netvsc_select_queue,
  1303. .ndo_get_stats64 = netvsc_get_stats64,
  1304. #ifdef CONFIG_NET_POLL_CONTROLLER
  1305. .ndo_poll_controller = netvsc_poll_controller,
  1306. #endif
  1307. };
  1308. /*
  1309. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  1310. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  1311. * present send GARP packet to network peers with netif_notify_peers().
  1312. */
  1313. static void netvsc_link_change(struct work_struct *w)
  1314. {
  1315. struct net_device_context *ndev_ctx =
  1316. container_of(w, struct net_device_context, dwork.work);
  1317. struct hv_device *device_obj = ndev_ctx->device_ctx;
  1318. struct net_device *net = hv_get_drvdata(device_obj);
  1319. struct netvsc_device *net_device;
  1320. struct rndis_device *rdev;
  1321. struct netvsc_reconfig *event = NULL;
  1322. bool notify = false, reschedule = false;
  1323. unsigned long flags, next_reconfig, delay;
  1324. /* if changes are happening, comeback later */
  1325. if (!rtnl_trylock()) {
  1326. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1327. return;
  1328. }
  1329. net_device = rtnl_dereference(ndev_ctx->nvdev);
  1330. if (!net_device)
  1331. goto out_unlock;
  1332. rdev = net_device->extension;
  1333. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  1334. if (time_is_after_jiffies(next_reconfig)) {
  1335. /* link_watch only sends one notification with current state
  1336. * per second, avoid doing reconfig more frequently. Handle
  1337. * wrap around.
  1338. */
  1339. delay = next_reconfig - jiffies;
  1340. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1341. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1342. goto out_unlock;
  1343. }
  1344. ndev_ctx->last_reconfig = jiffies;
  1345. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1346. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1347. event = list_first_entry(&ndev_ctx->reconfig_events,
  1348. struct netvsc_reconfig, list);
  1349. list_del(&event->list);
  1350. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1351. }
  1352. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1353. if (!event)
  1354. goto out_unlock;
  1355. switch (event->event) {
  1356. /* Only the following events are possible due to the check in
  1357. * netvsc_linkstatus_callback()
  1358. */
  1359. case RNDIS_STATUS_MEDIA_CONNECT:
  1360. if (rdev->link_state) {
  1361. rdev->link_state = false;
  1362. netif_carrier_on(net);
  1363. netif_tx_wake_all_queues(net);
  1364. } else {
  1365. notify = true;
  1366. }
  1367. kfree(event);
  1368. break;
  1369. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1370. if (!rdev->link_state) {
  1371. rdev->link_state = true;
  1372. netif_carrier_off(net);
  1373. netif_tx_stop_all_queues(net);
  1374. }
  1375. kfree(event);
  1376. break;
  1377. case RNDIS_STATUS_NETWORK_CHANGE:
  1378. /* Only makes sense if carrier is present */
  1379. if (!rdev->link_state) {
  1380. rdev->link_state = true;
  1381. netif_carrier_off(net);
  1382. netif_tx_stop_all_queues(net);
  1383. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1384. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1385. list_add(&event->list, &ndev_ctx->reconfig_events);
  1386. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1387. reschedule = true;
  1388. }
  1389. break;
  1390. }
  1391. rtnl_unlock();
  1392. if (notify)
  1393. netdev_notify_peers(net);
  1394. /* link_watch only sends one notification with current state per
  1395. * second, handle next reconfig event in 2 seconds.
  1396. */
  1397. if (reschedule)
  1398. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1399. return;
  1400. out_unlock:
  1401. rtnl_unlock();
  1402. }
  1403. static struct net_device *get_netvsc_bymac(const u8 *mac)
  1404. {
  1405. struct net_device *dev;
  1406. ASSERT_RTNL();
  1407. for_each_netdev(&init_net, dev) {
  1408. if (dev->netdev_ops != &device_ops)
  1409. continue; /* not a netvsc device */
  1410. if (ether_addr_equal(mac, dev->perm_addr))
  1411. return dev;
  1412. }
  1413. return NULL;
  1414. }
  1415. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  1416. {
  1417. struct net_device *dev;
  1418. ASSERT_RTNL();
  1419. for_each_netdev(&init_net, dev) {
  1420. struct net_device_context *net_device_ctx;
  1421. if (dev->netdev_ops != &device_ops)
  1422. continue; /* not a netvsc device */
  1423. net_device_ctx = netdev_priv(dev);
  1424. if (!rtnl_dereference(net_device_ctx->nvdev))
  1425. continue; /* device is removed */
  1426. if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
  1427. return dev; /* a match */
  1428. }
  1429. return NULL;
  1430. }
  1431. /* Called when VF is injecting data into network stack.
  1432. * Change the associated network device from VF to netvsc.
  1433. * note: already called with rcu_read_lock
  1434. */
  1435. static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
  1436. {
  1437. struct sk_buff *skb = *pskb;
  1438. struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
  1439. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1440. struct netvsc_vf_pcpu_stats *pcpu_stats
  1441. = this_cpu_ptr(ndev_ctx->vf_stats);
  1442. skb->dev = ndev;
  1443. u64_stats_update_begin(&pcpu_stats->syncp);
  1444. pcpu_stats->rx_packets++;
  1445. pcpu_stats->rx_bytes += skb->len;
  1446. u64_stats_update_end(&pcpu_stats->syncp);
  1447. return RX_HANDLER_ANOTHER;
  1448. }
  1449. static int netvsc_vf_join(struct net_device *vf_netdev,
  1450. struct net_device *ndev)
  1451. {
  1452. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1453. int ret;
  1454. ret = netdev_rx_handler_register(vf_netdev,
  1455. netvsc_vf_handle_frame, ndev);
  1456. if (ret != 0) {
  1457. netdev_err(vf_netdev,
  1458. "can not register netvsc VF receive handler (err = %d)\n",
  1459. ret);
  1460. goto rx_handler_failed;
  1461. }
  1462. ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
  1463. if (ret != 0) {
  1464. netdev_err(vf_netdev,
  1465. "can not set master device %s (err = %d)\n",
  1466. ndev->name, ret);
  1467. goto upper_link_failed;
  1468. }
  1469. /* set slave flag before open to prevent IPv6 addrconf */
  1470. vf_netdev->flags |= IFF_SLAVE;
  1471. schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
  1472. call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
  1473. netdev_info(vf_netdev, "joined to %s\n", ndev->name);
  1474. return 0;
  1475. upper_link_failed:
  1476. netdev_rx_handler_unregister(vf_netdev);
  1477. rx_handler_failed:
  1478. return ret;
  1479. }
  1480. static void __netvsc_vf_setup(struct net_device *ndev,
  1481. struct net_device *vf_netdev)
  1482. {
  1483. int ret;
  1484. /* Align MTU of VF with master */
  1485. ret = dev_set_mtu(vf_netdev, ndev->mtu);
  1486. if (ret)
  1487. netdev_warn(vf_netdev,
  1488. "unable to change mtu to %u\n", ndev->mtu);
  1489. if (netif_running(ndev)) {
  1490. ret = dev_open(vf_netdev);
  1491. if (ret)
  1492. netdev_warn(vf_netdev,
  1493. "unable to open: %d\n", ret);
  1494. }
  1495. }
  1496. /* Setup VF as slave of the synthetic device.
  1497. * Runs in workqueue to avoid recursion in netlink callbacks.
  1498. */
  1499. static void netvsc_vf_setup(struct work_struct *w)
  1500. {
  1501. struct net_device_context *ndev_ctx
  1502. = container_of(w, struct net_device_context, vf_takeover.work);
  1503. struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1504. struct net_device *vf_netdev;
  1505. if (!rtnl_trylock()) {
  1506. schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
  1507. return;
  1508. }
  1509. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1510. if (vf_netdev)
  1511. __netvsc_vf_setup(ndev, vf_netdev);
  1512. rtnl_unlock();
  1513. }
  1514. static int netvsc_register_vf(struct net_device *vf_netdev)
  1515. {
  1516. struct net_device *ndev;
  1517. struct net_device_context *net_device_ctx;
  1518. struct netvsc_device *netvsc_dev;
  1519. if (vf_netdev->addr_len != ETH_ALEN)
  1520. return NOTIFY_DONE;
  1521. /*
  1522. * We will use the MAC address to locate the synthetic interface to
  1523. * associate with the VF interface. If we don't find a matching
  1524. * synthetic interface, move on.
  1525. */
  1526. ndev = get_netvsc_bymac(vf_netdev->perm_addr);
  1527. if (!ndev)
  1528. return NOTIFY_DONE;
  1529. net_device_ctx = netdev_priv(ndev);
  1530. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1531. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1532. return NOTIFY_DONE;
  1533. if (netvsc_vf_join(vf_netdev, ndev) != 0)
  1534. return NOTIFY_DONE;
  1535. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1536. dev_hold(vf_netdev);
  1537. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1538. return NOTIFY_OK;
  1539. }
  1540. /* VF up/down change detected, schedule to change data path */
  1541. static int netvsc_vf_changed(struct net_device *vf_netdev)
  1542. {
  1543. struct net_device_context *net_device_ctx;
  1544. struct netvsc_device *netvsc_dev;
  1545. struct net_device *ndev;
  1546. bool vf_is_up = netif_running(vf_netdev);
  1547. ndev = get_netvsc_byref(vf_netdev);
  1548. if (!ndev)
  1549. return NOTIFY_DONE;
  1550. net_device_ctx = netdev_priv(ndev);
  1551. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1552. if (!netvsc_dev)
  1553. return NOTIFY_DONE;
  1554. netvsc_switch_datapath(ndev, vf_is_up);
  1555. netdev_info(ndev, "Data path switched %s VF: %s\n",
  1556. vf_is_up ? "to" : "from", vf_netdev->name);
  1557. return NOTIFY_OK;
  1558. }
  1559. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1560. {
  1561. struct net_device *ndev;
  1562. struct net_device_context *net_device_ctx;
  1563. ndev = get_netvsc_byref(vf_netdev);
  1564. if (!ndev)
  1565. return NOTIFY_DONE;
  1566. net_device_ctx = netdev_priv(ndev);
  1567. cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
  1568. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1569. netdev_rx_handler_unregister(vf_netdev);
  1570. netdev_upper_dev_unlink(vf_netdev, ndev);
  1571. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1572. dev_put(vf_netdev);
  1573. return NOTIFY_OK;
  1574. }
  1575. static int netvsc_probe(struct hv_device *dev,
  1576. const struct hv_vmbus_device_id *dev_id)
  1577. {
  1578. struct net_device *net = NULL;
  1579. struct net_device_context *net_device_ctx;
  1580. struct netvsc_device_info device_info;
  1581. struct netvsc_device *nvdev;
  1582. int ret = -ENOMEM;
  1583. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1584. VRSS_CHANNEL_MAX);
  1585. if (!net)
  1586. goto no_net;
  1587. netif_carrier_off(net);
  1588. netvsc_init_settings(net);
  1589. net_device_ctx = netdev_priv(net);
  1590. net_device_ctx->device_ctx = dev;
  1591. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1592. if (netif_msg_probe(net_device_ctx))
  1593. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1594. net_device_ctx->msg_enable);
  1595. hv_set_drvdata(dev, net);
  1596. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1597. spin_lock_init(&net_device_ctx->lock);
  1598. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1599. INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
  1600. net_device_ctx->vf_stats
  1601. = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
  1602. if (!net_device_ctx->vf_stats)
  1603. goto no_stats;
  1604. net->netdev_ops = &device_ops;
  1605. net->ethtool_ops = &ethtool_ops;
  1606. SET_NETDEV_DEV(net, &dev->device);
  1607. /* We always need headroom for rndis header */
  1608. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1609. /* Initialize the number of queues to be 1, we may change it if more
  1610. * channels are offered later.
  1611. */
  1612. netif_set_real_num_tx_queues(net, 1);
  1613. netif_set_real_num_rx_queues(net, 1);
  1614. /* Notify the netvsc driver of the new device */
  1615. memset(&device_info, 0, sizeof(device_info));
  1616. device_info.ring_size = ring_size;
  1617. device_info.num_chn = VRSS_CHANNEL_DEFAULT;
  1618. device_info.send_sections = NETVSC_DEFAULT_TX;
  1619. device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
  1620. device_info.recv_sections = NETVSC_DEFAULT_RX;
  1621. device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
  1622. nvdev = rndis_filter_device_add(dev, &device_info);
  1623. if (IS_ERR(nvdev)) {
  1624. ret = PTR_ERR(nvdev);
  1625. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1626. goto rndis_failed;
  1627. }
  1628. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1629. /* hw_features computed in rndis_filter_device_add */
  1630. net->features = net->hw_features |
  1631. NETIF_F_HIGHDMA | NETIF_F_SG |
  1632. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1633. net->vlan_features = net->features;
  1634. netdev_lockdep_set_classes(net);
  1635. /* MTU range: 68 - 1500 or 65521 */
  1636. net->min_mtu = NETVSC_MTU_MIN;
  1637. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1638. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1639. else
  1640. net->max_mtu = ETH_DATA_LEN;
  1641. ret = register_netdev(net);
  1642. if (ret != 0) {
  1643. pr_err("Unable to register netdev.\n");
  1644. goto register_failed;
  1645. }
  1646. return ret;
  1647. register_failed:
  1648. rndis_filter_device_remove(dev, nvdev);
  1649. rndis_failed:
  1650. free_percpu(net_device_ctx->vf_stats);
  1651. no_stats:
  1652. hv_set_drvdata(dev, NULL);
  1653. free_netdev(net);
  1654. no_net:
  1655. return ret;
  1656. }
  1657. static int netvsc_remove(struct hv_device *dev)
  1658. {
  1659. struct net_device_context *ndev_ctx;
  1660. struct net_device *vf_netdev;
  1661. struct net_device *net;
  1662. net = hv_get_drvdata(dev);
  1663. if (net == NULL) {
  1664. dev_err(&dev->device, "No net device to remove\n");
  1665. return 0;
  1666. }
  1667. ndev_ctx = netdev_priv(net);
  1668. netif_device_detach(net);
  1669. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1670. /*
  1671. * Call to the vsc driver to let it know that the device is being
  1672. * removed. Also blocks mtu and channel changes.
  1673. */
  1674. rtnl_lock();
  1675. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1676. if (vf_netdev)
  1677. netvsc_unregister_vf(vf_netdev);
  1678. unregister_netdevice(net);
  1679. rndis_filter_device_remove(dev,
  1680. rtnl_dereference(ndev_ctx->nvdev));
  1681. rtnl_unlock();
  1682. hv_set_drvdata(dev, NULL);
  1683. free_percpu(ndev_ctx->vf_stats);
  1684. free_netdev(net);
  1685. return 0;
  1686. }
  1687. static const struct hv_vmbus_device_id id_table[] = {
  1688. /* Network guid */
  1689. { HV_NIC_GUID, },
  1690. { },
  1691. };
  1692. MODULE_DEVICE_TABLE(vmbus, id_table);
  1693. /* The one and only one */
  1694. static struct hv_driver netvsc_drv = {
  1695. .name = KBUILD_MODNAME,
  1696. .id_table = id_table,
  1697. .probe = netvsc_probe,
  1698. .remove = netvsc_remove,
  1699. };
  1700. /*
  1701. * On Hyper-V, every VF interface is matched with a corresponding
  1702. * synthetic interface. The synthetic interface is presented first
  1703. * to the guest. When the corresponding VF instance is registered,
  1704. * we will take care of switching the data path.
  1705. */
  1706. static int netvsc_netdev_event(struct notifier_block *this,
  1707. unsigned long event, void *ptr)
  1708. {
  1709. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1710. /* Skip our own events */
  1711. if (event_dev->netdev_ops == &device_ops)
  1712. return NOTIFY_DONE;
  1713. /* Avoid non-Ethernet type devices */
  1714. if (event_dev->type != ARPHRD_ETHER)
  1715. return NOTIFY_DONE;
  1716. /* Avoid Vlan dev with same MAC registering as VF */
  1717. if (is_vlan_dev(event_dev))
  1718. return NOTIFY_DONE;
  1719. /* Avoid Bonding master dev with same MAC registering as VF */
  1720. if ((event_dev->priv_flags & IFF_BONDING) &&
  1721. (event_dev->flags & IFF_MASTER))
  1722. return NOTIFY_DONE;
  1723. switch (event) {
  1724. case NETDEV_REGISTER:
  1725. return netvsc_register_vf(event_dev);
  1726. case NETDEV_UNREGISTER:
  1727. return netvsc_unregister_vf(event_dev);
  1728. case NETDEV_UP:
  1729. case NETDEV_DOWN:
  1730. return netvsc_vf_changed(event_dev);
  1731. default:
  1732. return NOTIFY_DONE;
  1733. }
  1734. }
  1735. static struct notifier_block netvsc_netdev_notifier = {
  1736. .notifier_call = netvsc_netdev_event,
  1737. };
  1738. static void __exit netvsc_drv_exit(void)
  1739. {
  1740. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1741. vmbus_driver_unregister(&netvsc_drv);
  1742. }
  1743. static int __init netvsc_drv_init(void)
  1744. {
  1745. int ret;
  1746. if (ring_size < RING_SIZE_MIN) {
  1747. ring_size = RING_SIZE_MIN;
  1748. pr_info("Increased ring_size to %d (min allowed)\n",
  1749. ring_size);
  1750. }
  1751. ret = vmbus_driver_register(&netvsc_drv);
  1752. if (ret)
  1753. return ret;
  1754. register_netdevice_notifier(&netvsc_netdev_notifier);
  1755. return 0;
  1756. }
  1757. MODULE_LICENSE("GPL");
  1758. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1759. module_init(netvsc_drv_init);
  1760. module_exit(netvsc_drv_exit);