netvsc_drv.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/atomic.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/module.h>
  14. #include <linux/highmem.h>
  15. #include <linux/device.h>
  16. #include <linux/io.h>
  17. #include <linux/delay.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/inetdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/pci.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/in.h>
  25. #include <linux/slab.h>
  26. #include <linux/rtnetlink.h>
  27. #include <linux/netpoll.h>
  28. #include <linux/bpf.h>
  29. #include <net/arp.h>
  30. #include <net/netdev_lock.h>
  31. #include <net/route.h>
  32. #include <net/sock.h>
  33. #include <net/pkt_sched.h>
  34. #include <net/checksum.h>
  35. #include <net/ip6_checksum.h>
  36. #include "hyperv_net.h"
  37. #define RING_SIZE_MIN 64
  38. #define LINKCHANGE_INT (2 * HZ)
  39. #define VF_TAKEOVER_INT (HZ / 10)
  40. /* Macros to define the context of vf registration */
  41. #define VF_REG_IN_PROBE 1
  42. #define VF_REG_IN_NOTIFIER 2
  43. static unsigned int ring_size __ro_after_init = 128;
  44. module_param(ring_size, uint, 0444);
  45. MODULE_PARM_DESC(ring_size, "Ring buffer size (# of 4K pages)");
  46. unsigned int netvsc_ring_bytes __ro_after_init;
  47. static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  48. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  49. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
  50. NETIF_MSG_TX_ERR;
  51. static int debug = -1;
  52. module_param(debug, int, 0444);
  53. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  54. static LIST_HEAD(netvsc_dev_list);
  55. static void netvsc_change_rx_flags(struct net_device *net, int change)
  56. {
  57. struct net_device_context *ndev_ctx = netdev_priv(net);
  58. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  59. int inc;
  60. if (!vf_netdev)
  61. return;
  62. if (change & IFF_PROMISC) {
  63. inc = (net->flags & IFF_PROMISC) ? 1 : -1;
  64. dev_set_promiscuity(vf_netdev, inc);
  65. }
  66. if (change & IFF_ALLMULTI) {
  67. inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
  68. dev_set_allmulti(vf_netdev, inc);
  69. }
  70. }
  71. static void netvsc_set_rx_mode(struct net_device *net)
  72. {
  73. struct net_device_context *ndev_ctx = netdev_priv(net);
  74. struct net_device *vf_netdev;
  75. struct netvsc_device *nvdev;
  76. rcu_read_lock();
  77. vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
  78. if (vf_netdev) {
  79. dev_uc_sync(vf_netdev, net);
  80. dev_mc_sync(vf_netdev, net);
  81. }
  82. nvdev = rcu_dereference(ndev_ctx->nvdev);
  83. if (nvdev)
  84. rndis_filter_update(nvdev);
  85. rcu_read_unlock();
  86. }
  87. static void netvsc_tx_enable(struct netvsc_device *nvscdev,
  88. struct net_device *ndev)
  89. {
  90. nvscdev->tx_disable = false;
  91. virt_wmb(); /* ensure queue wake up mechanism is on */
  92. netif_tx_wake_all_queues(ndev);
  93. }
  94. static int netvsc_open(struct net_device *net)
  95. {
  96. struct net_device_context *ndev_ctx = netdev_priv(net);
  97. struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  98. struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
  99. struct rndis_device *rdev;
  100. int ret = 0;
  101. netif_carrier_off(net);
  102. /* Open up the device */
  103. ret = rndis_filter_open(nvdev);
  104. if (ret != 0) {
  105. netdev_err(net, "unable to open device (ret %d).\n", ret);
  106. return ret;
  107. }
  108. rdev = nvdev->extension;
  109. if (!rdev->link_state) {
  110. netif_carrier_on(net);
  111. netvsc_tx_enable(nvdev, net);
  112. }
  113. if (vf_netdev) {
  114. /* Setting synthetic device up transparently sets
  115. * slave as up. If open fails, then slave will be
  116. * still be offline (and not used).
  117. */
  118. ret = dev_open(vf_netdev, NULL);
  119. if (ret)
  120. netdev_warn(net,
  121. "unable to open slave: %s: %d\n",
  122. vf_netdev->name, ret);
  123. }
  124. return 0;
  125. }
  126. static int netvsc_wait_until_empty(struct netvsc_device *nvdev)
  127. {
  128. unsigned int retry = 0;
  129. int i;
  130. /* Ensure pending bytes in ring are read */
  131. for (;;) {
  132. u32 aread = 0;
  133. for (i = 0; i < nvdev->num_chn; i++) {
  134. struct vmbus_channel *chn
  135. = nvdev->chan_table[i].channel;
  136. if (!chn)
  137. continue;
  138. /* make sure receive not running now */
  139. napi_synchronize(&nvdev->chan_table[i].napi);
  140. aread = hv_get_bytes_to_read(&chn->inbound);
  141. if (aread)
  142. break;
  143. aread = hv_get_bytes_to_read(&chn->outbound);
  144. if (aread)
  145. break;
  146. }
  147. if (aread == 0)
  148. return 0;
  149. if (++retry > RETRY_MAX)
  150. return -ETIMEDOUT;
  151. usleep_range(RETRY_US_LO, RETRY_US_HI);
  152. }
  153. }
  154. static void netvsc_tx_disable(struct netvsc_device *nvscdev,
  155. struct net_device *ndev)
  156. {
  157. if (nvscdev) {
  158. nvscdev->tx_disable = true;
  159. virt_wmb(); /* ensure txq will not wake up after stop */
  160. }
  161. netif_tx_disable(ndev);
  162. }
  163. static int netvsc_close(struct net_device *net)
  164. {
  165. struct net_device_context *net_device_ctx = netdev_priv(net);
  166. struct net_device *vf_netdev
  167. = rtnl_dereference(net_device_ctx->vf_netdev);
  168. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  169. int ret;
  170. netvsc_tx_disable(nvdev, net);
  171. /* No need to close rndis filter if it is removed already */
  172. if (!nvdev)
  173. return 0;
  174. ret = rndis_filter_close(nvdev);
  175. if (ret != 0) {
  176. netdev_err(net, "unable to close device (ret %d).\n", ret);
  177. return ret;
  178. }
  179. ret = netvsc_wait_until_empty(nvdev);
  180. if (ret)
  181. netdev_err(net, "Ring buffer not empty after closing rndis\n");
  182. if (vf_netdev)
  183. dev_close(vf_netdev);
  184. return ret;
  185. }
  186. static inline void *init_ppi_data(struct rndis_message *msg,
  187. u32 ppi_size, u32 pkt_type)
  188. {
  189. struct rndis_packet *rndis_pkt = &msg->msg.pkt;
  190. struct rndis_per_packet_info *ppi;
  191. rndis_pkt->data_offset += ppi_size;
  192. ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
  193. + rndis_pkt->per_pkt_info_len;
  194. ppi->size = ppi_size;
  195. ppi->type = pkt_type;
  196. ppi->internal = 0;
  197. ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
  198. rndis_pkt->per_pkt_info_len += ppi_size;
  199. return ppi + 1;
  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. struct net_device *sb_dev)
  242. {
  243. struct net_device_context *ndc = netdev_priv(ndev);
  244. struct net_device *vf_netdev;
  245. u16 txq;
  246. rcu_read_lock();
  247. vf_netdev = rcu_dereference(ndc->vf_netdev);
  248. if (vf_netdev) {
  249. const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
  250. if (vf_ops->ndo_select_queue)
  251. txq = vf_ops->ndo_select_queue(vf_netdev, skb, sb_dev);
  252. else
  253. txq = netdev_pick_tx(vf_netdev, skb, NULL);
  254. /* Record the queue selected by VF so that it can be
  255. * used for common case where VF has more queues than
  256. * the synthetic device.
  257. */
  258. qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
  259. } else {
  260. txq = netvsc_pick_tx(ndev, skb);
  261. }
  262. rcu_read_unlock();
  263. while (txq >= ndev->real_num_tx_queues)
  264. txq -= ndev->real_num_tx_queues;
  265. return txq;
  266. }
  267. static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
  268. struct hv_netvsc_packet *packet,
  269. struct hv_page_buffer *pb)
  270. {
  271. int frags = skb_shinfo(skb)->nr_frags;
  272. int i;
  273. /* The packet is laid out thus:
  274. * 1. hdr: RNDIS header and PPI
  275. * 2. skb linear data
  276. * 3. skb fragment data
  277. */
  278. pb[0].offset = offset_in_hvpage(hdr);
  279. pb[0].len = len;
  280. pb[0].pfn = virt_to_hvpfn(hdr);
  281. packet->rmsg_size = len;
  282. pb[1].offset = offset_in_hvpage(skb->data);
  283. pb[1].len = skb_headlen(skb);
  284. pb[1].pfn = virt_to_hvpfn(skb->data);
  285. for (i = 0; i < frags; i++) {
  286. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  287. struct hv_page_buffer *cur_pb = &pb[i + 2];
  288. u64 pfn = page_to_hvpfn(skb_frag_page(frag));
  289. u32 offset = skb_frag_off(frag);
  290. cur_pb->offset = offset_in_hvpage(offset);
  291. cur_pb->len = skb_frag_size(frag);
  292. cur_pb->pfn = pfn + (offset >> HV_HYP_PAGE_SHIFT);
  293. }
  294. return frags + 2;
  295. }
  296. static int count_skb_frag_slots(struct sk_buff *skb)
  297. {
  298. int i, frags = skb_shinfo(skb)->nr_frags;
  299. int pages = 0;
  300. for (i = 0; i < frags; i++) {
  301. skb_frag_t *frag = skb_shinfo(skb)->frags + i;
  302. unsigned long size = skb_frag_size(frag);
  303. unsigned long offset = skb_frag_off(frag);
  304. /* Skip unused frames from start of page */
  305. offset &= ~HV_HYP_PAGE_MASK;
  306. pages += HVPFN_UP(offset + size);
  307. }
  308. return pages;
  309. }
  310. static int netvsc_get_slots(struct sk_buff *skb)
  311. {
  312. char *data = skb->data;
  313. unsigned int offset = offset_in_hvpage(data);
  314. unsigned int len = skb_headlen(skb);
  315. int slots;
  316. int frag_slots;
  317. slots = DIV_ROUND_UP(offset + len, HV_HYP_PAGE_SIZE);
  318. frag_slots = count_skb_frag_slots(skb);
  319. return slots + frag_slots;
  320. }
  321. static u32 net_checksum_info(struct sk_buff *skb)
  322. {
  323. if (skb->protocol == htons(ETH_P_IP)) {
  324. struct iphdr *ip = ip_hdr(skb);
  325. if (ip->protocol == IPPROTO_TCP)
  326. return TRANSPORT_INFO_IPV4_TCP;
  327. else if (ip->protocol == IPPROTO_UDP)
  328. return TRANSPORT_INFO_IPV4_UDP;
  329. } else {
  330. struct ipv6hdr *ip6 = ipv6_hdr(skb);
  331. if (ip6->nexthdr == IPPROTO_TCP)
  332. return TRANSPORT_INFO_IPV6_TCP;
  333. else if (ip6->nexthdr == IPPROTO_UDP)
  334. return TRANSPORT_INFO_IPV6_UDP;
  335. }
  336. return TRANSPORT_INFO_NOT_IP;
  337. }
  338. /* Send skb on the slave VF device. */
  339. static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
  340. struct sk_buff *skb)
  341. {
  342. struct net_device_context *ndev_ctx = netdev_priv(net);
  343. unsigned int len = skb->len;
  344. int rc;
  345. skb->dev = vf_netdev;
  346. skb_record_rx_queue(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
  347. rc = dev_queue_xmit(skb);
  348. if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
  349. struct netvsc_vf_pcpu_stats *pcpu_stats
  350. = this_cpu_ptr(ndev_ctx->vf_stats);
  351. u64_stats_update_begin(&pcpu_stats->syncp);
  352. pcpu_stats->tx_packets++;
  353. pcpu_stats->tx_bytes += len;
  354. u64_stats_update_end(&pcpu_stats->syncp);
  355. } else {
  356. this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
  357. }
  358. return rc;
  359. }
  360. static int netvsc_xmit(struct sk_buff *skb, struct net_device *net, bool xdp_tx)
  361. {
  362. struct net_device_context *net_device_ctx = netdev_priv(net);
  363. struct hv_netvsc_packet *packet = NULL;
  364. int ret;
  365. unsigned int num_data_pgs;
  366. struct rndis_message *rndis_msg;
  367. struct net_device *vf_netdev;
  368. u32 rndis_msg_size;
  369. u32 hash;
  370. struct hv_page_buffer pb[MAX_DATA_RANGES];
  371. /* If VF is present and up then redirect packets to it.
  372. * Skip the VF if it is marked down or has no carrier.
  373. * If netpoll is in uses, then VF can not be used either.
  374. */
  375. vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
  376. if (vf_netdev && netif_running(vf_netdev) &&
  377. netif_carrier_ok(vf_netdev) && !netpoll_tx_running(net) &&
  378. net_device_ctx->data_path_is_vf)
  379. return netvsc_vf_xmit(net, vf_netdev, skb);
  380. /* We will atmost need two pages to describe the rndis
  381. * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
  382. * of pages in a single packet. If skb is scattered around
  383. * more pages we try linearizing it.
  384. */
  385. num_data_pgs = netvsc_get_slots(skb) + 2;
  386. if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
  387. ++net_device_ctx->eth_stats.tx_scattered;
  388. if (skb_linearize(skb))
  389. goto no_memory;
  390. num_data_pgs = netvsc_get_slots(skb) + 2;
  391. if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
  392. ++net_device_ctx->eth_stats.tx_too_big;
  393. goto drop;
  394. }
  395. }
  396. /*
  397. * Place the rndis header in the skb head room and
  398. * the skb->cb will be used for hv_netvsc_packet
  399. * structure.
  400. */
  401. ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
  402. if (ret)
  403. goto no_memory;
  404. /* Use the skb control buffer for building up the packet */
  405. BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
  406. sizeof_field(struct sk_buff, cb));
  407. packet = (struct hv_netvsc_packet *)skb->cb;
  408. packet->q_idx = skb_get_queue_mapping(skb);
  409. packet->total_data_buflen = skb->len;
  410. packet->total_bytes = skb->len;
  411. packet->total_packets = 1;
  412. rndis_msg = (struct rndis_message *)skb->head;
  413. /* Add the rndis header */
  414. rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
  415. rndis_msg->msg_len = packet->total_data_buflen;
  416. rndis_msg->msg.pkt = (struct rndis_packet) {
  417. .data_offset = sizeof(struct rndis_packet),
  418. .data_len = packet->total_data_buflen,
  419. .per_pkt_info_offset = sizeof(struct rndis_packet),
  420. };
  421. rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
  422. hash = skb_get_hash_raw(skb);
  423. if (hash != 0 && net->real_num_tx_queues > 1) {
  424. u32 *hash_info;
  425. rndis_msg_size += NDIS_HASH_PPI_SIZE;
  426. hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
  427. NBL_HASH_VALUE);
  428. *hash_info = hash;
  429. }
  430. /* When using AF_PACKET we need to drop VLAN header from
  431. * the frame and update the SKB to allow the HOST OS
  432. * to transmit the 802.1Q packet
  433. */
  434. if (skb->protocol == htons(ETH_P_8021Q)) {
  435. u16 vlan_tci;
  436. skb_reset_mac_header(skb);
  437. if (eth_type_vlan(eth_hdr(skb)->h_proto)) {
  438. if (unlikely(__skb_vlan_pop(skb, &vlan_tci) != 0)) {
  439. ++net_device_ctx->eth_stats.vlan_error;
  440. goto drop;
  441. }
  442. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
  443. /* Update the NDIS header pkt lengths */
  444. packet->total_data_buflen -= VLAN_HLEN;
  445. packet->total_bytes -= VLAN_HLEN;
  446. rndis_msg->msg_len = packet->total_data_buflen;
  447. rndis_msg->msg.pkt.data_len = packet->total_data_buflen;
  448. }
  449. }
  450. if (skb_vlan_tag_present(skb)) {
  451. struct ndis_pkt_8021q_info *vlan;
  452. rndis_msg_size += NDIS_VLAN_PPI_SIZE;
  453. vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
  454. IEEE_8021Q_INFO);
  455. vlan->value = 0;
  456. vlan->vlanid = skb_vlan_tag_get_id(skb);
  457. vlan->cfi = skb_vlan_tag_get_cfi(skb);
  458. vlan->pri = skb_vlan_tag_get_prio(skb);
  459. }
  460. if (skb_is_gso(skb)) {
  461. struct ndis_tcp_lso_info *lso_info;
  462. rndis_msg_size += NDIS_LSO_PPI_SIZE;
  463. lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
  464. TCP_LARGESEND_PKTINFO);
  465. lso_info->value = 0;
  466. lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
  467. if (skb->protocol == htons(ETH_P_IP)) {
  468. lso_info->lso_v2_transmit.ip_version =
  469. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
  470. ip_hdr(skb)->tot_len = 0;
  471. ip_hdr(skb)->check = 0;
  472. tcp_hdr(skb)->check =
  473. ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  474. ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
  475. } else {
  476. lso_info->lso_v2_transmit.ip_version =
  477. NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
  478. tcp_v6_gso_csum_prep(skb);
  479. }
  480. lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
  481. lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
  482. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  483. if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
  484. struct ndis_tcp_ip_checksum_info *csum_info;
  485. rndis_msg_size += NDIS_CSUM_PPI_SIZE;
  486. csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
  487. TCPIP_CHKSUM_PKTINFO);
  488. csum_info->value = 0;
  489. csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
  490. if (skb->protocol == htons(ETH_P_IP)) {
  491. csum_info->transmit.is_ipv4 = 1;
  492. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  493. csum_info->transmit.tcp_checksum = 1;
  494. else
  495. csum_info->transmit.udp_checksum = 1;
  496. } else {
  497. csum_info->transmit.is_ipv6 = 1;
  498. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  499. csum_info->transmit.tcp_checksum = 1;
  500. else
  501. csum_info->transmit.udp_checksum = 1;
  502. }
  503. } else {
  504. /* Can't do offload of this type of checksum */
  505. if (skb_checksum_help(skb))
  506. goto drop;
  507. }
  508. }
  509. /* Start filling in the page buffers with the rndis hdr */
  510. rndis_msg->msg_len += rndis_msg_size;
  511. packet->total_data_buflen = rndis_msg->msg_len;
  512. packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
  513. skb, packet, pb);
  514. /* timestamp packet in software */
  515. skb_tx_timestamp(skb);
  516. ret = netvsc_send(net, packet, rndis_msg, pb, skb, xdp_tx);
  517. if (likely(ret == 0))
  518. return NETDEV_TX_OK;
  519. if (ret == -EAGAIN) {
  520. ++net_device_ctx->eth_stats.tx_busy;
  521. return NETDEV_TX_BUSY;
  522. }
  523. if (ret == -ENOSPC)
  524. ++net_device_ctx->eth_stats.tx_no_space;
  525. drop:
  526. dev_kfree_skb_any(skb);
  527. net->stats.tx_dropped++;
  528. return NETDEV_TX_OK;
  529. no_memory:
  530. ++net_device_ctx->eth_stats.tx_no_memory;
  531. goto drop;
  532. }
  533. static netdev_tx_t netvsc_start_xmit(struct sk_buff *skb,
  534. struct net_device *ndev)
  535. {
  536. return netvsc_xmit(skb, ndev, false);
  537. }
  538. /*
  539. * netvsc_linkstatus_callback - Link up/down notification
  540. */
  541. void netvsc_linkstatus_callback(struct net_device *net,
  542. struct rndis_message *resp,
  543. void *data, u32 data_buflen)
  544. {
  545. struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
  546. struct net_device_context *ndev_ctx = netdev_priv(net);
  547. struct netvsc_reconfig *event;
  548. unsigned long flags;
  549. /* Ensure the packet is big enough to access its fields */
  550. if (resp->msg_len - RNDIS_HEADER_SIZE < sizeof(struct rndis_indicate_status)) {
  551. netdev_err(net, "invalid rndis_indicate_status packet, len: %u\n",
  552. resp->msg_len);
  553. return;
  554. }
  555. /* Copy the RNDIS indicate status into nvchan->recv_buf */
  556. memcpy(indicate, data + RNDIS_HEADER_SIZE, sizeof(*indicate));
  557. /* Update the physical link speed when changing to another vSwitch */
  558. if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
  559. u32 speed;
  560. /* Validate status_buf_offset and status_buflen.
  561. *
  562. * Certain (pre-Fe) implementations of Hyper-V's vSwitch didn't account
  563. * for the status buffer field in resp->msg_len; perform the validation
  564. * using data_buflen (>= resp->msg_len).
  565. */
  566. if (indicate->status_buflen < sizeof(speed) ||
  567. indicate->status_buf_offset < sizeof(*indicate) ||
  568. data_buflen - RNDIS_HEADER_SIZE < indicate->status_buf_offset ||
  569. data_buflen - RNDIS_HEADER_SIZE - indicate->status_buf_offset
  570. < indicate->status_buflen) {
  571. netdev_err(net, "invalid rndis_indicate_status packet\n");
  572. return;
  573. }
  574. speed = *(u32 *)(data + RNDIS_HEADER_SIZE + indicate->status_buf_offset) / 10000;
  575. ndev_ctx->speed = speed;
  576. return;
  577. }
  578. /* Handle these link change statuses below */
  579. if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
  580. indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
  581. indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
  582. return;
  583. if (net->reg_state != NETREG_REGISTERED)
  584. return;
  585. event = kzalloc_obj(*event, GFP_ATOMIC);
  586. if (!event)
  587. return;
  588. event->event = indicate->status;
  589. spin_lock_irqsave(&ndev_ctx->lock, flags);
  590. list_add_tail(&event->list, &ndev_ctx->reconfig_events);
  591. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  592. schedule_delayed_work(&ndev_ctx->dwork, 0);
  593. }
  594. /* This function should only be called after skb_record_rx_queue() */
  595. void netvsc_xdp_xmit(struct sk_buff *skb, struct net_device *ndev)
  596. {
  597. int rc;
  598. skb->queue_mapping = skb_get_rx_queue(skb);
  599. __skb_push(skb, ETH_HLEN);
  600. rc = netvsc_xmit(skb, ndev, true);
  601. if (dev_xmit_complete(rc))
  602. return;
  603. dev_kfree_skb_any(skb);
  604. ndev->stats.tx_dropped++;
  605. }
  606. static void netvsc_comp_ipcsum(struct sk_buff *skb)
  607. {
  608. struct iphdr *iph = (struct iphdr *)skb->data;
  609. iph->check = 0;
  610. iph->check = ip_fast_csum(iph, iph->ihl);
  611. }
  612. static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
  613. struct netvsc_channel *nvchan,
  614. struct xdp_buff *xdp)
  615. {
  616. struct napi_struct *napi = &nvchan->napi;
  617. const struct ndis_pkt_8021q_info *vlan = &nvchan->rsc.vlan;
  618. const struct ndis_tcp_ip_checksum_info *csum_info =
  619. &nvchan->rsc.csum_info;
  620. const u32 *hash_info = &nvchan->rsc.hash_info;
  621. u8 ppi_flags = nvchan->rsc.ppi_flags;
  622. struct sk_buff *skb;
  623. void *xbuf = xdp->data_hard_start;
  624. int i;
  625. if (xbuf) {
  626. unsigned int hdroom = xdp->data - xdp->data_hard_start;
  627. unsigned int xlen = xdp->data_end - xdp->data;
  628. unsigned int frag_size = xdp->frame_sz;
  629. skb = build_skb(xbuf, frag_size);
  630. if (!skb) {
  631. __free_page(virt_to_page(xbuf));
  632. return NULL;
  633. }
  634. skb_reserve(skb, hdroom);
  635. skb_put(skb, xlen);
  636. skb->dev = napi->dev;
  637. } else {
  638. skb = napi_alloc_skb(napi, nvchan->rsc.pktlen);
  639. if (!skb)
  640. return NULL;
  641. /* Copy to skb. This copy is needed here since the memory
  642. * pointed by hv_netvsc_packet cannot be deallocated.
  643. */
  644. for (i = 0; i < nvchan->rsc.cnt; i++)
  645. skb_put_data(skb, nvchan->rsc.data[i],
  646. nvchan->rsc.len[i]);
  647. }
  648. skb->protocol = eth_type_trans(skb, net);
  649. /* skb is already created with CHECKSUM_NONE */
  650. skb_checksum_none_assert(skb);
  651. /* Incoming packets may have IP header checksum verified by the host.
  652. * They may not have IP header checksum computed after coalescing.
  653. * We compute it here if the flags are set, because on Linux, the IP
  654. * checksum is always checked.
  655. */
  656. if ((ppi_flags & NVSC_RSC_CSUM_INFO) && csum_info->receive.ip_checksum_value_invalid &&
  657. csum_info->receive.ip_checksum_succeeded &&
  658. skb->protocol == htons(ETH_P_IP)) {
  659. /* Check that there is enough space to hold the IP header. */
  660. if (skb_headlen(skb) < sizeof(struct iphdr)) {
  661. kfree_skb(skb);
  662. return NULL;
  663. }
  664. netvsc_comp_ipcsum(skb);
  665. }
  666. /* Do L4 checksum offload if enabled and present. */
  667. if ((ppi_flags & NVSC_RSC_CSUM_INFO) && (net->features & NETIF_F_RXCSUM)) {
  668. if (csum_info->receive.tcp_checksum_succeeded ||
  669. csum_info->receive.udp_checksum_succeeded)
  670. skb->ip_summed = CHECKSUM_UNNECESSARY;
  671. }
  672. if ((ppi_flags & NVSC_RSC_HASH_INFO) && (net->features & NETIF_F_RXHASH))
  673. skb_set_hash(skb, *hash_info, PKT_HASH_TYPE_L4);
  674. if (ppi_flags & NVSC_RSC_VLAN) {
  675. u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT) |
  676. (vlan->cfi ? VLAN_CFI_MASK : 0);
  677. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  678. vlan_tci);
  679. }
  680. return skb;
  681. }
  682. /*
  683. * netvsc_recv_callback - Callback when we receive a packet from the
  684. * "wire" on the specified device.
  685. */
  686. int netvsc_recv_callback(struct net_device *net,
  687. struct netvsc_device *net_device,
  688. struct netvsc_channel *nvchan)
  689. {
  690. struct net_device_context *net_device_ctx = netdev_priv(net);
  691. struct vmbus_channel *channel = nvchan->channel;
  692. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  693. struct sk_buff *skb;
  694. struct netvsc_stats_rx *rx_stats = &nvchan->rx_stats;
  695. struct xdp_buff xdp;
  696. u32 act;
  697. if (net->reg_state != NETREG_REGISTERED)
  698. return NVSP_STAT_FAIL;
  699. act = netvsc_run_xdp(net, nvchan, &xdp);
  700. if (act == XDP_REDIRECT)
  701. return NVSP_STAT_SUCCESS;
  702. if (act != XDP_PASS && act != XDP_TX) {
  703. u64_stats_update_begin(&rx_stats->syncp);
  704. rx_stats->xdp_drop++;
  705. u64_stats_update_end(&rx_stats->syncp);
  706. return NVSP_STAT_SUCCESS; /* consumed by XDP */
  707. }
  708. /* Allocate a skb - TODO direct I/O to pages? */
  709. skb = netvsc_alloc_recv_skb(net, nvchan, &xdp);
  710. if (unlikely(!skb)) {
  711. ++net_device_ctx->eth_stats.rx_no_memory;
  712. return NVSP_STAT_FAIL;
  713. }
  714. skb_record_rx_queue(skb, q_idx);
  715. /*
  716. * Even if injecting the packet, record the statistics
  717. * on the synthetic device because modifying the VF device
  718. * statistics will not work correctly.
  719. */
  720. u64_stats_update_begin(&rx_stats->syncp);
  721. if (act == XDP_TX)
  722. rx_stats->xdp_tx++;
  723. rx_stats->packets++;
  724. rx_stats->bytes += nvchan->rsc.pktlen;
  725. if (skb->pkt_type == PACKET_BROADCAST)
  726. ++rx_stats->broadcast;
  727. else if (skb->pkt_type == PACKET_MULTICAST)
  728. ++rx_stats->multicast;
  729. u64_stats_update_end(&rx_stats->syncp);
  730. if (act == XDP_TX) {
  731. netvsc_xdp_xmit(skb, net);
  732. return NVSP_STAT_SUCCESS;
  733. }
  734. napi_gro_receive(&nvchan->napi, skb);
  735. return NVSP_STAT_SUCCESS;
  736. }
  737. static void netvsc_get_drvinfo(struct net_device *net,
  738. struct ethtool_drvinfo *info)
  739. {
  740. strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  741. strscpy(info->fw_version, "N/A", sizeof(info->fw_version));
  742. }
  743. static void netvsc_get_channels(struct net_device *net,
  744. struct ethtool_channels *channel)
  745. {
  746. struct net_device_context *net_device_ctx = netdev_priv(net);
  747. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  748. if (nvdev) {
  749. channel->max_combined = nvdev->max_chn;
  750. channel->combined_count = nvdev->num_chn;
  751. }
  752. }
  753. /* Alloc struct netvsc_device_info, and initialize it from either existing
  754. * struct netvsc_device, or from default values.
  755. */
  756. static
  757. struct netvsc_device_info *netvsc_devinfo_get(struct netvsc_device *nvdev)
  758. {
  759. struct netvsc_device_info *dev_info;
  760. struct bpf_prog *prog;
  761. dev_info = kzalloc_obj(*dev_info, GFP_ATOMIC);
  762. if (!dev_info)
  763. return NULL;
  764. if (nvdev) {
  765. ASSERT_RTNL();
  766. dev_info->num_chn = nvdev->num_chn;
  767. dev_info->send_sections = nvdev->send_section_cnt;
  768. dev_info->send_section_size = nvdev->send_section_size;
  769. dev_info->recv_sections = nvdev->recv_section_cnt;
  770. dev_info->recv_section_size = nvdev->recv_section_size;
  771. memcpy(dev_info->rss_key, nvdev->extension->rss_key,
  772. NETVSC_HASH_KEYLEN);
  773. prog = netvsc_xdp_get(nvdev);
  774. if (prog) {
  775. bpf_prog_inc(prog);
  776. dev_info->bprog = prog;
  777. }
  778. } else {
  779. dev_info->num_chn = max(VRSS_CHANNEL_DEFAULT,
  780. netif_get_num_default_rss_queues());
  781. dev_info->send_sections = NETVSC_DEFAULT_TX;
  782. dev_info->send_section_size = NETVSC_SEND_SECTION_SIZE;
  783. dev_info->recv_sections = NETVSC_DEFAULT_RX;
  784. dev_info->recv_section_size = NETVSC_RECV_SECTION_SIZE;
  785. }
  786. return dev_info;
  787. }
  788. /* Free struct netvsc_device_info */
  789. static void netvsc_devinfo_put(struct netvsc_device_info *dev_info)
  790. {
  791. if (dev_info->bprog) {
  792. ASSERT_RTNL();
  793. bpf_prog_put(dev_info->bprog);
  794. }
  795. kfree(dev_info);
  796. }
  797. static int netvsc_detach(struct net_device *ndev,
  798. struct netvsc_device *nvdev)
  799. {
  800. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  801. struct hv_device *hdev = ndev_ctx->device_ctx;
  802. int ret;
  803. /* Don't try continuing to try and setup sub channels */
  804. if (cancel_work_sync(&nvdev->subchan_work))
  805. nvdev->num_chn = 1;
  806. netvsc_xdp_set(ndev, NULL, NULL, nvdev);
  807. /* If device was up (receiving) then shutdown */
  808. if (netif_running(ndev)) {
  809. netvsc_tx_disable(nvdev, ndev);
  810. ret = rndis_filter_close(nvdev);
  811. if (ret) {
  812. netdev_err(ndev,
  813. "unable to close device (ret %d).\n", ret);
  814. return ret;
  815. }
  816. ret = netvsc_wait_until_empty(nvdev);
  817. if (ret) {
  818. netdev_err(ndev,
  819. "Ring buffer not empty after closing rndis\n");
  820. return ret;
  821. }
  822. }
  823. netif_device_detach(ndev);
  824. rndis_filter_device_remove(hdev, nvdev);
  825. return 0;
  826. }
  827. static int netvsc_attach(struct net_device *ndev,
  828. struct netvsc_device_info *dev_info)
  829. {
  830. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  831. struct hv_device *hdev = ndev_ctx->device_ctx;
  832. struct netvsc_device *nvdev;
  833. struct rndis_device *rdev;
  834. struct bpf_prog *prog;
  835. int ret = 0;
  836. nvdev = rndis_filter_device_add(hdev, dev_info);
  837. if (IS_ERR(nvdev))
  838. return PTR_ERR(nvdev);
  839. if (nvdev->num_chn > 1) {
  840. ret = rndis_set_subchannel(ndev, nvdev, dev_info);
  841. /* if unavailable, just proceed with one queue */
  842. if (ret) {
  843. nvdev->max_chn = 1;
  844. nvdev->num_chn = 1;
  845. }
  846. }
  847. prog = dev_info->bprog;
  848. if (prog) {
  849. bpf_prog_inc(prog);
  850. ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
  851. if (ret) {
  852. bpf_prog_put(prog);
  853. goto err1;
  854. }
  855. }
  856. /* In any case device is now ready */
  857. nvdev->tx_disable = false;
  858. netif_device_attach(ndev);
  859. /* Note: enable and attach happen when sub-channels setup */
  860. netif_carrier_off(ndev);
  861. if (netif_running(ndev)) {
  862. ret = rndis_filter_open(nvdev);
  863. if (ret)
  864. goto err2;
  865. rdev = nvdev->extension;
  866. if (!rdev->link_state)
  867. netif_carrier_on(ndev);
  868. }
  869. return 0;
  870. err2:
  871. netif_device_detach(ndev);
  872. err1:
  873. rndis_filter_device_remove(hdev, nvdev);
  874. return ret;
  875. }
  876. static int netvsc_set_channels(struct net_device *net,
  877. struct ethtool_channels *channels)
  878. {
  879. struct net_device_context *net_device_ctx = netdev_priv(net);
  880. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  881. unsigned int orig, count = channels->combined_count;
  882. struct netvsc_device_info *device_info;
  883. int ret;
  884. /* We do not support separate count for rx, tx, or other */
  885. if (count == 0 ||
  886. channels->rx_count || channels->tx_count || channels->other_count)
  887. return -EINVAL;
  888. if (!nvdev || nvdev->destroy)
  889. return -ENODEV;
  890. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  891. return -EINVAL;
  892. if (count > nvdev->max_chn)
  893. return -EINVAL;
  894. orig = nvdev->num_chn;
  895. device_info = netvsc_devinfo_get(nvdev);
  896. if (!device_info)
  897. return -ENOMEM;
  898. device_info->num_chn = count;
  899. ret = netvsc_detach(net, nvdev);
  900. if (ret)
  901. goto out;
  902. ret = netvsc_attach(net, device_info);
  903. if (ret) {
  904. device_info->num_chn = orig;
  905. if (netvsc_attach(net, device_info))
  906. netdev_err(net, "restoring channel setting failed\n");
  907. }
  908. out:
  909. netvsc_devinfo_put(device_info);
  910. return ret;
  911. }
  912. static void netvsc_init_settings(struct net_device *dev)
  913. {
  914. struct net_device_context *ndc = netdev_priv(dev);
  915. ndc->l4_hash = HV_DEFAULT_L4HASH;
  916. ndc->speed = SPEED_UNKNOWN;
  917. ndc->duplex = DUPLEX_FULL;
  918. dev->features = NETIF_F_LRO;
  919. }
  920. static int netvsc_get_link_ksettings(struct net_device *dev,
  921. struct ethtool_link_ksettings *cmd)
  922. {
  923. struct net_device_context *ndc = netdev_priv(dev);
  924. struct net_device *vf_netdev;
  925. vf_netdev = rtnl_dereference(ndc->vf_netdev);
  926. if (vf_netdev)
  927. return __ethtool_get_link_ksettings(vf_netdev, cmd);
  928. cmd->base.speed = ndc->speed;
  929. cmd->base.duplex = ndc->duplex;
  930. cmd->base.port = PORT_OTHER;
  931. return 0;
  932. }
  933. static int netvsc_set_link_ksettings(struct net_device *dev,
  934. const struct ethtool_link_ksettings *cmd)
  935. {
  936. struct net_device_context *ndc = netdev_priv(dev);
  937. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  938. if (vf_netdev) {
  939. if (!vf_netdev->ethtool_ops->set_link_ksettings)
  940. return -EOPNOTSUPP;
  941. return vf_netdev->ethtool_ops->set_link_ksettings(vf_netdev,
  942. cmd);
  943. }
  944. return ethtool_virtdev_set_link_ksettings(dev, cmd,
  945. &ndc->speed, &ndc->duplex);
  946. }
  947. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  948. {
  949. struct net_device_context *ndevctx = netdev_priv(ndev);
  950. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  951. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  952. int orig_mtu = ndev->mtu;
  953. struct netvsc_device_info *device_info;
  954. int ret = 0;
  955. if (!nvdev || nvdev->destroy)
  956. return -ENODEV;
  957. device_info = netvsc_devinfo_get(nvdev);
  958. if (!device_info)
  959. return -ENOMEM;
  960. /* Change MTU of underlying VF netdev first. */
  961. if (vf_netdev) {
  962. ret = dev_set_mtu(vf_netdev, mtu);
  963. if (ret)
  964. goto out;
  965. }
  966. ret = netvsc_detach(ndev, nvdev);
  967. if (ret)
  968. goto rollback_vf;
  969. WRITE_ONCE(ndev->mtu, mtu);
  970. ret = netvsc_attach(ndev, device_info);
  971. if (!ret)
  972. goto out;
  973. /* Attempt rollback to original MTU */
  974. WRITE_ONCE(ndev->mtu, orig_mtu);
  975. if (netvsc_attach(ndev, device_info))
  976. netdev_err(ndev, "restoring mtu failed\n");
  977. rollback_vf:
  978. if (vf_netdev)
  979. dev_set_mtu(vf_netdev, orig_mtu);
  980. out:
  981. netvsc_devinfo_put(device_info);
  982. return ret;
  983. }
  984. static void netvsc_get_vf_stats(struct net_device *net,
  985. struct netvsc_vf_pcpu_stats *tot)
  986. {
  987. struct net_device_context *ndev_ctx = netdev_priv(net);
  988. int i;
  989. memset(tot, 0, sizeof(*tot));
  990. for_each_possible_cpu(i) {
  991. const struct netvsc_vf_pcpu_stats *stats
  992. = per_cpu_ptr(ndev_ctx->vf_stats, i);
  993. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  994. unsigned int start;
  995. do {
  996. start = u64_stats_fetch_begin(&stats->syncp);
  997. rx_packets = stats->rx_packets;
  998. tx_packets = stats->tx_packets;
  999. rx_bytes = stats->rx_bytes;
  1000. tx_bytes = stats->tx_bytes;
  1001. } while (u64_stats_fetch_retry(&stats->syncp, start));
  1002. tot->rx_packets += rx_packets;
  1003. tot->tx_packets += tx_packets;
  1004. tot->rx_bytes += rx_bytes;
  1005. tot->tx_bytes += tx_bytes;
  1006. tot->tx_dropped += stats->tx_dropped;
  1007. }
  1008. }
  1009. static void netvsc_get_pcpu_stats(struct net_device *net,
  1010. struct netvsc_ethtool_pcpu_stats *pcpu_tot)
  1011. {
  1012. struct net_device_context *ndev_ctx = netdev_priv(net);
  1013. struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
  1014. int i;
  1015. /* fetch percpu stats of vf */
  1016. for_each_possible_cpu(i) {
  1017. const struct netvsc_vf_pcpu_stats *stats =
  1018. per_cpu_ptr(ndev_ctx->vf_stats, i);
  1019. struct netvsc_ethtool_pcpu_stats *this_tot = &pcpu_tot[i];
  1020. unsigned int start;
  1021. do {
  1022. start = u64_stats_fetch_begin(&stats->syncp);
  1023. this_tot->vf_rx_packets = stats->rx_packets;
  1024. this_tot->vf_tx_packets = stats->tx_packets;
  1025. this_tot->vf_rx_bytes = stats->rx_bytes;
  1026. this_tot->vf_tx_bytes = stats->tx_bytes;
  1027. } while (u64_stats_fetch_retry(&stats->syncp, start));
  1028. this_tot->rx_packets = this_tot->vf_rx_packets;
  1029. this_tot->tx_packets = this_tot->vf_tx_packets;
  1030. this_tot->rx_bytes = this_tot->vf_rx_bytes;
  1031. this_tot->tx_bytes = this_tot->vf_tx_bytes;
  1032. }
  1033. /* fetch percpu stats of netvsc */
  1034. for (i = 0; i < nvdev->num_chn; i++) {
  1035. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  1036. const struct netvsc_stats_tx *tx_stats;
  1037. const struct netvsc_stats_rx *rx_stats;
  1038. struct netvsc_ethtool_pcpu_stats *this_tot =
  1039. &pcpu_tot[nvchan->channel->target_cpu];
  1040. u64 packets, bytes;
  1041. unsigned int start;
  1042. tx_stats = &nvchan->tx_stats;
  1043. do {
  1044. start = u64_stats_fetch_begin(&tx_stats->syncp);
  1045. packets = tx_stats->packets;
  1046. bytes = tx_stats->bytes;
  1047. } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
  1048. this_tot->tx_bytes += bytes;
  1049. this_tot->tx_packets += packets;
  1050. rx_stats = &nvchan->rx_stats;
  1051. do {
  1052. start = u64_stats_fetch_begin(&rx_stats->syncp);
  1053. packets = rx_stats->packets;
  1054. bytes = rx_stats->bytes;
  1055. } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
  1056. this_tot->rx_bytes += bytes;
  1057. this_tot->rx_packets += packets;
  1058. }
  1059. }
  1060. static void netvsc_get_stats64(struct net_device *net,
  1061. struct rtnl_link_stats64 *t)
  1062. {
  1063. struct net_device_context *ndev_ctx = netdev_priv(net);
  1064. struct netvsc_device *nvdev;
  1065. struct netvsc_vf_pcpu_stats vf_tot;
  1066. int i;
  1067. rcu_read_lock();
  1068. nvdev = rcu_dereference(ndev_ctx->nvdev);
  1069. if (!nvdev)
  1070. goto out;
  1071. netdev_stats_to_stats64(t, &net->stats);
  1072. netvsc_get_vf_stats(net, &vf_tot);
  1073. t->rx_packets += vf_tot.rx_packets;
  1074. t->tx_packets += vf_tot.tx_packets;
  1075. t->rx_bytes += vf_tot.rx_bytes;
  1076. t->tx_bytes += vf_tot.tx_bytes;
  1077. t->tx_dropped += vf_tot.tx_dropped;
  1078. for (i = 0; i < nvdev->num_chn; i++) {
  1079. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  1080. const struct netvsc_stats_tx *tx_stats;
  1081. const struct netvsc_stats_rx *rx_stats;
  1082. u64 packets, bytes, multicast;
  1083. unsigned int start;
  1084. tx_stats = &nvchan->tx_stats;
  1085. do {
  1086. start = u64_stats_fetch_begin(&tx_stats->syncp);
  1087. packets = tx_stats->packets;
  1088. bytes = tx_stats->bytes;
  1089. } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
  1090. t->tx_bytes += bytes;
  1091. t->tx_packets += packets;
  1092. rx_stats = &nvchan->rx_stats;
  1093. do {
  1094. start = u64_stats_fetch_begin(&rx_stats->syncp);
  1095. packets = rx_stats->packets;
  1096. bytes = rx_stats->bytes;
  1097. multicast = rx_stats->multicast + rx_stats->broadcast;
  1098. } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
  1099. t->rx_bytes += bytes;
  1100. t->rx_packets += packets;
  1101. t->multicast += multicast;
  1102. }
  1103. out:
  1104. rcu_read_unlock();
  1105. }
  1106. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  1107. {
  1108. struct net_device_context *ndc = netdev_priv(ndev);
  1109. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  1110. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1111. struct sockaddr_storage *addr = p;
  1112. int err;
  1113. err = eth_prepare_mac_addr_change(ndev, p);
  1114. if (err)
  1115. return err;
  1116. if (!nvdev)
  1117. return -ENODEV;
  1118. if (vf_netdev) {
  1119. err = dev_set_mac_address(vf_netdev, addr, NULL);
  1120. if (err)
  1121. return err;
  1122. }
  1123. err = rndis_filter_set_device_mac(nvdev, addr->__data);
  1124. if (!err) {
  1125. eth_commit_mac_addr_change(ndev, p);
  1126. } else if (vf_netdev) {
  1127. /* rollback change on VF */
  1128. memcpy(addr->__data, ndev->dev_addr, ETH_ALEN);
  1129. dev_set_mac_address(vf_netdev, addr, NULL);
  1130. }
  1131. return err;
  1132. }
  1133. static const struct {
  1134. char name[ETH_GSTRING_LEN];
  1135. u16 offset;
  1136. } netvsc_stats[] = {
  1137. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  1138. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  1139. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  1140. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  1141. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  1142. { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
  1143. { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
  1144. { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
  1145. { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
  1146. { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
  1147. { "vlan_error", offsetof(struct netvsc_ethtool_stats, vlan_error) },
  1148. }, pcpu_stats[] = {
  1149. { "cpu%u_rx_packets",
  1150. offsetof(struct netvsc_ethtool_pcpu_stats, rx_packets) },
  1151. { "cpu%u_rx_bytes",
  1152. offsetof(struct netvsc_ethtool_pcpu_stats, rx_bytes) },
  1153. { "cpu%u_tx_packets",
  1154. offsetof(struct netvsc_ethtool_pcpu_stats, tx_packets) },
  1155. { "cpu%u_tx_bytes",
  1156. offsetof(struct netvsc_ethtool_pcpu_stats, tx_bytes) },
  1157. { "cpu%u_vf_rx_packets",
  1158. offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_packets) },
  1159. { "cpu%u_vf_rx_bytes",
  1160. offsetof(struct netvsc_ethtool_pcpu_stats, vf_rx_bytes) },
  1161. { "cpu%u_vf_tx_packets",
  1162. offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_packets) },
  1163. { "cpu%u_vf_tx_bytes",
  1164. offsetof(struct netvsc_ethtool_pcpu_stats, vf_tx_bytes) },
  1165. }, vf_stats[] = {
  1166. { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
  1167. { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
  1168. { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
  1169. { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
  1170. { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
  1171. };
  1172. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  1173. #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
  1174. /* statistics per queue (rx/tx packets/bytes) */
  1175. #define NETVSC_PCPU_STATS_LEN (num_present_cpus() * ARRAY_SIZE(pcpu_stats))
  1176. /* 8 statistics per queue (rx/tx packets/bytes, XDP actions) */
  1177. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 8)
  1178. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  1179. {
  1180. struct net_device_context *ndc = netdev_priv(dev);
  1181. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1182. if (!nvdev)
  1183. return -ENODEV;
  1184. switch (string_set) {
  1185. case ETH_SS_STATS:
  1186. return NETVSC_GLOBAL_STATS_LEN
  1187. + NETVSC_VF_STATS_LEN
  1188. + NETVSC_QUEUE_STATS_LEN(nvdev)
  1189. + NETVSC_PCPU_STATS_LEN;
  1190. default:
  1191. return -EINVAL;
  1192. }
  1193. }
  1194. static void netvsc_get_ethtool_stats(struct net_device *dev,
  1195. struct ethtool_stats *stats, u64 *data)
  1196. {
  1197. struct net_device_context *ndc = netdev_priv(dev);
  1198. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1199. const void *nds = &ndc->eth_stats;
  1200. const struct netvsc_stats_tx *tx_stats;
  1201. const struct netvsc_stats_rx *rx_stats;
  1202. struct netvsc_vf_pcpu_stats sum;
  1203. struct netvsc_ethtool_pcpu_stats *pcpu_sum;
  1204. unsigned int start;
  1205. u64 packets, bytes;
  1206. u64 xdp_drop;
  1207. u64 xdp_redirect;
  1208. u64 xdp_tx;
  1209. u64 xdp_xmit;
  1210. int i, j, cpu;
  1211. if (!nvdev)
  1212. return;
  1213. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  1214. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  1215. netvsc_get_vf_stats(dev, &sum);
  1216. for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
  1217. data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
  1218. for (j = 0; j < nvdev->num_chn; j++) {
  1219. tx_stats = &nvdev->chan_table[j].tx_stats;
  1220. do {
  1221. start = u64_stats_fetch_begin(&tx_stats->syncp);
  1222. packets = tx_stats->packets;
  1223. bytes = tx_stats->bytes;
  1224. xdp_xmit = tx_stats->xdp_xmit;
  1225. } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
  1226. data[i++] = packets;
  1227. data[i++] = bytes;
  1228. data[i++] = xdp_xmit;
  1229. rx_stats = &nvdev->chan_table[j].rx_stats;
  1230. do {
  1231. start = u64_stats_fetch_begin(&rx_stats->syncp);
  1232. packets = rx_stats->packets;
  1233. bytes = rx_stats->bytes;
  1234. xdp_drop = rx_stats->xdp_drop;
  1235. xdp_redirect = rx_stats->xdp_redirect;
  1236. xdp_tx = rx_stats->xdp_tx;
  1237. } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
  1238. data[i++] = packets;
  1239. data[i++] = bytes;
  1240. data[i++] = xdp_drop;
  1241. data[i++] = xdp_redirect;
  1242. data[i++] = xdp_tx;
  1243. }
  1244. pcpu_sum = kvmalloc_objs(struct netvsc_ethtool_pcpu_stats, nr_cpu_ids);
  1245. if (!pcpu_sum)
  1246. return;
  1247. netvsc_get_pcpu_stats(dev, pcpu_sum);
  1248. for_each_present_cpu(cpu) {
  1249. struct netvsc_ethtool_pcpu_stats *this_sum = &pcpu_sum[cpu];
  1250. for (j = 0; j < ARRAY_SIZE(pcpu_stats); j++)
  1251. data[i++] = *(u64 *)((void *)this_sum
  1252. + pcpu_stats[j].offset);
  1253. }
  1254. kvfree(pcpu_sum);
  1255. }
  1256. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1257. {
  1258. struct net_device_context *ndc = netdev_priv(dev);
  1259. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1260. u8 *p = data;
  1261. int i, cpu;
  1262. if (!nvdev)
  1263. return;
  1264. switch (stringset) {
  1265. case ETH_SS_STATS:
  1266. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++)
  1267. ethtool_puts(&p, netvsc_stats[i].name);
  1268. for (i = 0; i < ARRAY_SIZE(vf_stats); i++)
  1269. ethtool_puts(&p, vf_stats[i].name);
  1270. for (i = 0; i < nvdev->num_chn; i++) {
  1271. ethtool_sprintf(&p, "tx_queue_%u_packets", i);
  1272. ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
  1273. ethtool_sprintf(&p, "tx_queue_%u_xdp_xmit", i);
  1274. ethtool_sprintf(&p, "rx_queue_%u_packets", i);
  1275. ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
  1276. ethtool_sprintf(&p, "rx_queue_%u_xdp_drop", i);
  1277. ethtool_sprintf(&p, "rx_queue_%u_xdp_redirect", i);
  1278. ethtool_sprintf(&p, "rx_queue_%u_xdp_tx", i);
  1279. }
  1280. for_each_present_cpu(cpu) {
  1281. for (i = 0; i < ARRAY_SIZE(pcpu_stats); i++)
  1282. ethtool_sprintf(&p, pcpu_stats[i].name, cpu);
  1283. }
  1284. break;
  1285. }
  1286. }
  1287. static int
  1288. netvsc_get_rxfh_fields(struct net_device *ndev,
  1289. struct ethtool_rxfh_fields *info)
  1290. {
  1291. struct net_device_context *ndc = netdev_priv(ndev);
  1292. const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1293. info->data = RXH_IP_SRC | RXH_IP_DST;
  1294. switch (info->flow_type) {
  1295. case TCP_V4_FLOW:
  1296. if (ndc->l4_hash & HV_TCP4_L4HASH)
  1297. info->data |= l4_flag;
  1298. break;
  1299. case TCP_V6_FLOW:
  1300. if (ndc->l4_hash & HV_TCP6_L4HASH)
  1301. info->data |= l4_flag;
  1302. break;
  1303. case UDP_V4_FLOW:
  1304. if (ndc->l4_hash & HV_UDP4_L4HASH)
  1305. info->data |= l4_flag;
  1306. break;
  1307. case UDP_V6_FLOW:
  1308. if (ndc->l4_hash & HV_UDP6_L4HASH)
  1309. info->data |= l4_flag;
  1310. break;
  1311. case IPV4_FLOW:
  1312. case IPV6_FLOW:
  1313. break;
  1314. default:
  1315. info->data = 0;
  1316. break;
  1317. }
  1318. return 0;
  1319. }
  1320. static u32 netvsc_get_rx_ring_count(struct net_device *dev)
  1321. {
  1322. struct net_device_context *ndc = netdev_priv(dev);
  1323. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1324. if (!nvdev)
  1325. return 0;
  1326. return nvdev->num_chn;
  1327. }
  1328. static int
  1329. netvsc_set_rxfh_fields(struct net_device *dev,
  1330. const struct ethtool_rxfh_fields *info,
  1331. struct netlink_ext_ack *extack)
  1332. {
  1333. struct net_device_context *ndc = netdev_priv(dev);
  1334. if (info->data == (RXH_IP_SRC | RXH_IP_DST |
  1335. RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  1336. switch (info->flow_type) {
  1337. case TCP_V4_FLOW:
  1338. ndc->l4_hash |= HV_TCP4_L4HASH;
  1339. break;
  1340. case TCP_V6_FLOW:
  1341. ndc->l4_hash |= HV_TCP6_L4HASH;
  1342. break;
  1343. case UDP_V4_FLOW:
  1344. ndc->l4_hash |= HV_UDP4_L4HASH;
  1345. break;
  1346. case UDP_V6_FLOW:
  1347. ndc->l4_hash |= HV_UDP6_L4HASH;
  1348. break;
  1349. default:
  1350. return -EOPNOTSUPP;
  1351. }
  1352. return 0;
  1353. }
  1354. if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
  1355. switch (info->flow_type) {
  1356. case TCP_V4_FLOW:
  1357. ndc->l4_hash &= ~HV_TCP4_L4HASH;
  1358. break;
  1359. case TCP_V6_FLOW:
  1360. ndc->l4_hash &= ~HV_TCP6_L4HASH;
  1361. break;
  1362. case UDP_V4_FLOW:
  1363. ndc->l4_hash &= ~HV_UDP4_L4HASH;
  1364. break;
  1365. case UDP_V6_FLOW:
  1366. ndc->l4_hash &= ~HV_UDP6_L4HASH;
  1367. break;
  1368. default:
  1369. return -EOPNOTSUPP;
  1370. }
  1371. return 0;
  1372. }
  1373. return -EOPNOTSUPP;
  1374. }
  1375. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  1376. {
  1377. return NETVSC_HASH_KEYLEN;
  1378. }
  1379. static u32 netvsc_rss_indir_size(struct net_device *dev)
  1380. {
  1381. struct net_device_context *ndc = netdev_priv(dev);
  1382. return ndc->rx_table_sz;
  1383. }
  1384. static int netvsc_get_rxfh(struct net_device *dev,
  1385. struct ethtool_rxfh_param *rxfh)
  1386. {
  1387. struct net_device_context *ndc = netdev_priv(dev);
  1388. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1389. struct rndis_device *rndis_dev;
  1390. int i;
  1391. if (!ndev)
  1392. return -ENODEV;
  1393. rxfh->hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  1394. rndis_dev = ndev->extension;
  1395. if (rxfh->indir) {
  1396. for (i = 0; i < ndc->rx_table_sz; i++)
  1397. rxfh->indir[i] = ndc->rx_table[i];
  1398. }
  1399. if (rxfh->key)
  1400. memcpy(rxfh->key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  1401. return 0;
  1402. }
  1403. static int netvsc_set_rxfh(struct net_device *dev,
  1404. struct ethtool_rxfh_param *rxfh,
  1405. struct netlink_ext_ack *extack)
  1406. {
  1407. struct net_device_context *ndc = netdev_priv(dev);
  1408. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1409. struct rndis_device *rndis_dev;
  1410. u8 *key = rxfh->key;
  1411. int i;
  1412. if (!ndev)
  1413. return -ENODEV;
  1414. if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
  1415. rxfh->hfunc != ETH_RSS_HASH_TOP)
  1416. return -EOPNOTSUPP;
  1417. if (!ndc->rx_table_sz)
  1418. return -EOPNOTSUPP;
  1419. rndis_dev = ndev->extension;
  1420. if (rxfh->indir) {
  1421. for (i = 0; i < ndc->rx_table_sz; i++)
  1422. if (rxfh->indir[i] >= ndev->num_chn)
  1423. return -EINVAL;
  1424. for (i = 0; i < ndc->rx_table_sz; i++)
  1425. ndc->rx_table[i] = rxfh->indir[i];
  1426. }
  1427. if (!key) {
  1428. if (!rxfh->indir)
  1429. return 0;
  1430. key = rndis_dev->rss_key;
  1431. }
  1432. return rndis_filter_set_rss_param(rndis_dev, key);
  1433. }
  1434. /* Hyper-V RNDIS protocol does not have ring in the HW sense.
  1435. * It does have pre-allocated receive area which is divided into sections.
  1436. */
  1437. static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
  1438. struct ethtool_ringparam *ring)
  1439. {
  1440. u32 max_buf_size;
  1441. ring->rx_pending = nvdev->recv_section_cnt;
  1442. ring->tx_pending = nvdev->send_section_cnt;
  1443. if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  1444. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  1445. else
  1446. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  1447. ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
  1448. ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
  1449. / nvdev->send_section_size;
  1450. }
  1451. static void netvsc_get_ringparam(struct net_device *ndev,
  1452. struct ethtool_ringparam *ring,
  1453. struct kernel_ethtool_ringparam *kernel_ring,
  1454. struct netlink_ext_ack *extack)
  1455. {
  1456. struct net_device_context *ndevctx = netdev_priv(ndev);
  1457. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1458. if (!nvdev)
  1459. return;
  1460. __netvsc_get_ringparam(nvdev, ring);
  1461. }
  1462. static int netvsc_set_ringparam(struct net_device *ndev,
  1463. struct ethtool_ringparam *ring,
  1464. struct kernel_ethtool_ringparam *kernel_ring,
  1465. struct netlink_ext_ack *extack)
  1466. {
  1467. struct net_device_context *ndevctx = netdev_priv(ndev);
  1468. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1469. struct netvsc_device_info *device_info;
  1470. struct ethtool_ringparam orig;
  1471. u32 new_tx, new_rx;
  1472. int ret = 0;
  1473. if (!nvdev || nvdev->destroy)
  1474. return -ENODEV;
  1475. memset(&orig, 0, sizeof(orig));
  1476. __netvsc_get_ringparam(nvdev, &orig);
  1477. new_tx = clamp_t(u32, ring->tx_pending,
  1478. NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
  1479. new_rx = clamp_t(u32, ring->rx_pending,
  1480. NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
  1481. if (new_tx == orig.tx_pending &&
  1482. new_rx == orig.rx_pending)
  1483. return 0; /* no change */
  1484. device_info = netvsc_devinfo_get(nvdev);
  1485. if (!device_info)
  1486. return -ENOMEM;
  1487. device_info->send_sections = new_tx;
  1488. device_info->recv_sections = new_rx;
  1489. ret = netvsc_detach(ndev, nvdev);
  1490. if (ret)
  1491. goto out;
  1492. ret = netvsc_attach(ndev, device_info);
  1493. if (ret) {
  1494. device_info->send_sections = orig.tx_pending;
  1495. device_info->recv_sections = orig.rx_pending;
  1496. if (netvsc_attach(ndev, device_info))
  1497. netdev_err(ndev, "restoring ringparam failed");
  1498. }
  1499. out:
  1500. netvsc_devinfo_put(device_info);
  1501. return ret;
  1502. }
  1503. static netdev_features_t netvsc_fix_features(struct net_device *ndev,
  1504. netdev_features_t features)
  1505. {
  1506. struct net_device_context *ndevctx = netdev_priv(ndev);
  1507. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1508. if (!nvdev || nvdev->destroy)
  1509. return features;
  1510. if ((features & NETIF_F_LRO) && netvsc_xdp_get(nvdev)) {
  1511. features ^= NETIF_F_LRO;
  1512. netdev_info(ndev, "Skip LRO - unsupported with XDP\n");
  1513. }
  1514. return features;
  1515. }
  1516. static int netvsc_set_features(struct net_device *ndev,
  1517. netdev_features_t features)
  1518. {
  1519. netdev_features_t change = features ^ ndev->features;
  1520. struct net_device_context *ndevctx = netdev_priv(ndev);
  1521. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1522. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  1523. struct ndis_offload_params offloads;
  1524. int ret = 0;
  1525. if (!nvdev || nvdev->destroy)
  1526. return -ENODEV;
  1527. if (!(change & NETIF_F_LRO))
  1528. goto syncvf;
  1529. memset(&offloads, 0, sizeof(struct ndis_offload_params));
  1530. if (features & NETIF_F_LRO) {
  1531. offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
  1532. offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
  1533. } else {
  1534. offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
  1535. offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
  1536. }
  1537. ret = rndis_filter_set_offload_params(ndev, nvdev, &offloads);
  1538. if (ret) {
  1539. features ^= NETIF_F_LRO;
  1540. ndev->features = features;
  1541. }
  1542. syncvf:
  1543. if (!vf_netdev)
  1544. return ret;
  1545. vf_netdev->wanted_features = features;
  1546. netdev_update_features(vf_netdev);
  1547. return ret;
  1548. }
  1549. static int netvsc_get_regs_len(struct net_device *netdev)
  1550. {
  1551. return VRSS_SEND_TAB_SIZE * sizeof(u32);
  1552. }
  1553. static void netvsc_get_regs(struct net_device *netdev,
  1554. struct ethtool_regs *regs, void *p)
  1555. {
  1556. struct net_device_context *ndc = netdev_priv(netdev);
  1557. u32 *regs_buff = p;
  1558. /* increase the version, if buffer format is changed. */
  1559. regs->version = 1;
  1560. memcpy(regs_buff, ndc->tx_table, VRSS_SEND_TAB_SIZE * sizeof(u32));
  1561. }
  1562. static u32 netvsc_get_msglevel(struct net_device *ndev)
  1563. {
  1564. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1565. return ndev_ctx->msg_enable;
  1566. }
  1567. static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
  1568. {
  1569. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1570. ndev_ctx->msg_enable = val;
  1571. }
  1572. static const struct ethtool_ops ethtool_ops = {
  1573. .get_drvinfo = netvsc_get_drvinfo,
  1574. .get_regs_len = netvsc_get_regs_len,
  1575. .get_regs = netvsc_get_regs,
  1576. .get_msglevel = netvsc_get_msglevel,
  1577. .set_msglevel = netvsc_set_msglevel,
  1578. .get_link = ethtool_op_get_link,
  1579. .get_ethtool_stats = netvsc_get_ethtool_stats,
  1580. .get_sset_count = netvsc_get_sset_count,
  1581. .get_strings = netvsc_get_strings,
  1582. .get_channels = netvsc_get_channels,
  1583. .set_channels = netvsc_set_channels,
  1584. .get_ts_info = ethtool_op_get_ts_info,
  1585. .get_rx_ring_count = netvsc_get_rx_ring_count,
  1586. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  1587. .get_rxfh_indir_size = netvsc_rss_indir_size,
  1588. .get_rxfh = netvsc_get_rxfh,
  1589. .set_rxfh = netvsc_set_rxfh,
  1590. .get_rxfh_fields = netvsc_get_rxfh_fields,
  1591. .set_rxfh_fields = netvsc_set_rxfh_fields,
  1592. .get_link_ksettings = netvsc_get_link_ksettings,
  1593. .set_link_ksettings = netvsc_set_link_ksettings,
  1594. .get_ringparam = netvsc_get_ringparam,
  1595. .set_ringparam = netvsc_set_ringparam,
  1596. };
  1597. static const struct net_device_ops device_ops = {
  1598. .ndo_open = netvsc_open,
  1599. .ndo_stop = netvsc_close,
  1600. .ndo_start_xmit = netvsc_start_xmit,
  1601. .ndo_change_rx_flags = netvsc_change_rx_flags,
  1602. .ndo_set_rx_mode = netvsc_set_rx_mode,
  1603. .ndo_fix_features = netvsc_fix_features,
  1604. .ndo_set_features = netvsc_set_features,
  1605. .ndo_change_mtu = netvsc_change_mtu,
  1606. .ndo_validate_addr = eth_validate_addr,
  1607. .ndo_set_mac_address = netvsc_set_mac_addr,
  1608. .ndo_select_queue = netvsc_select_queue,
  1609. .ndo_get_stats64 = netvsc_get_stats64,
  1610. .ndo_bpf = netvsc_bpf,
  1611. .ndo_xdp_xmit = netvsc_ndoxdp_xmit,
  1612. };
  1613. /*
  1614. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  1615. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  1616. * present send GARP packet to network peers with netif_notify_peers().
  1617. */
  1618. static void netvsc_link_change(struct work_struct *w)
  1619. {
  1620. struct net_device_context *ndev_ctx =
  1621. container_of(w, struct net_device_context, dwork.work);
  1622. struct hv_device *device_obj = ndev_ctx->device_ctx;
  1623. struct net_device *net = hv_get_drvdata(device_obj);
  1624. unsigned long flags, next_reconfig, delay;
  1625. struct netvsc_reconfig *event = NULL;
  1626. struct netvsc_device *net_device;
  1627. struct rndis_device *rdev;
  1628. bool reschedule = false;
  1629. /* if changes are happening, comeback later */
  1630. if (!rtnl_trylock()) {
  1631. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1632. return;
  1633. }
  1634. net_device = rtnl_dereference(ndev_ctx->nvdev);
  1635. if (!net_device)
  1636. goto out_unlock;
  1637. rdev = net_device->extension;
  1638. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  1639. if (time_is_after_jiffies(next_reconfig)) {
  1640. /* link_watch only sends one notification with current state
  1641. * per second, avoid doing reconfig more frequently. Handle
  1642. * wrap around.
  1643. */
  1644. delay = next_reconfig - jiffies;
  1645. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1646. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1647. goto out_unlock;
  1648. }
  1649. ndev_ctx->last_reconfig = jiffies;
  1650. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1651. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1652. event = list_first_entry(&ndev_ctx->reconfig_events,
  1653. struct netvsc_reconfig, list);
  1654. list_del(&event->list);
  1655. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1656. }
  1657. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1658. if (!event)
  1659. goto out_unlock;
  1660. switch (event->event) {
  1661. /* Only the following events are possible due to the check in
  1662. * netvsc_linkstatus_callback()
  1663. */
  1664. case RNDIS_STATUS_MEDIA_CONNECT:
  1665. if (rdev->link_state) {
  1666. rdev->link_state = false;
  1667. netif_carrier_on(net);
  1668. netvsc_tx_enable(net_device, net);
  1669. } else {
  1670. __netdev_notify_peers(net);
  1671. }
  1672. kfree(event);
  1673. break;
  1674. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1675. if (!rdev->link_state) {
  1676. rdev->link_state = true;
  1677. netif_carrier_off(net);
  1678. netvsc_tx_disable(net_device, net);
  1679. }
  1680. kfree(event);
  1681. break;
  1682. case RNDIS_STATUS_NETWORK_CHANGE:
  1683. /* Only makes sense if carrier is present */
  1684. if (!rdev->link_state) {
  1685. rdev->link_state = true;
  1686. netif_carrier_off(net);
  1687. netvsc_tx_disable(net_device, net);
  1688. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1689. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1690. list_add(&event->list, &ndev_ctx->reconfig_events);
  1691. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1692. reschedule = true;
  1693. }
  1694. break;
  1695. }
  1696. rtnl_unlock();
  1697. /* link_watch only sends one notification with current state per
  1698. * second, handle next reconfig event in 2 seconds.
  1699. */
  1700. if (reschedule)
  1701. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1702. return;
  1703. out_unlock:
  1704. rtnl_unlock();
  1705. }
  1706. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  1707. {
  1708. struct net_device_context *net_device_ctx;
  1709. struct net_device *dev;
  1710. dev = netdev_master_upper_dev_get(vf_netdev);
  1711. if (!dev || dev->netdev_ops != &device_ops)
  1712. return NULL; /* not a netvsc device */
  1713. net_device_ctx = netdev_priv(dev);
  1714. if (!rtnl_dereference(net_device_ctx->nvdev))
  1715. return NULL; /* device is removed */
  1716. return dev;
  1717. }
  1718. /* Called when VF is injecting data into network stack.
  1719. * Change the associated network device from VF to netvsc.
  1720. * note: already called with rcu_read_lock
  1721. */
  1722. static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
  1723. {
  1724. struct sk_buff *skb = *pskb;
  1725. struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
  1726. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1727. struct netvsc_vf_pcpu_stats *pcpu_stats
  1728. = this_cpu_ptr(ndev_ctx->vf_stats);
  1729. skb = skb_share_check(skb, GFP_ATOMIC);
  1730. if (unlikely(!skb))
  1731. return RX_HANDLER_CONSUMED;
  1732. *pskb = skb;
  1733. skb->dev = ndev;
  1734. u64_stats_update_begin(&pcpu_stats->syncp);
  1735. pcpu_stats->rx_packets++;
  1736. pcpu_stats->rx_bytes += skb->len;
  1737. u64_stats_update_end(&pcpu_stats->syncp);
  1738. return RX_HANDLER_ANOTHER;
  1739. }
  1740. static int netvsc_vf_join(struct net_device *vf_netdev,
  1741. struct net_device *ndev, int context)
  1742. {
  1743. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1744. int ret;
  1745. ret = netdev_rx_handler_register(vf_netdev,
  1746. netvsc_vf_handle_frame, ndev);
  1747. if (ret != 0) {
  1748. netdev_err(vf_netdev,
  1749. "can not register netvsc VF receive handler (err = %d)\n",
  1750. ret);
  1751. goto rx_handler_failed;
  1752. }
  1753. ret = netdev_master_upper_dev_link(vf_netdev, ndev,
  1754. NULL, NULL, NULL);
  1755. if (ret != 0) {
  1756. netdev_err(vf_netdev,
  1757. "can not set master device %s (err = %d)\n",
  1758. ndev->name, ret);
  1759. goto upper_link_failed;
  1760. }
  1761. /* If this registration is called from probe context vf_takeover
  1762. * is taken care of later in probe itself.
  1763. */
  1764. if (context == VF_REG_IN_NOTIFIER)
  1765. schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
  1766. call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
  1767. netdev_info(vf_netdev, "joined to %s\n", ndev->name);
  1768. return 0;
  1769. upper_link_failed:
  1770. netdev_rx_handler_unregister(vf_netdev);
  1771. rx_handler_failed:
  1772. return ret;
  1773. }
  1774. static void __netvsc_vf_setup(struct net_device *ndev,
  1775. struct net_device *vf_netdev)
  1776. {
  1777. int ret;
  1778. /* Align MTU of VF with master */
  1779. ret = dev_set_mtu(vf_netdev, ndev->mtu);
  1780. if (ret)
  1781. netdev_warn(vf_netdev,
  1782. "unable to change mtu to %u\n", ndev->mtu);
  1783. /* set multicast etc flags on VF */
  1784. dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE, NULL);
  1785. /* sync address list from ndev to VF */
  1786. netif_addr_lock_bh(ndev);
  1787. dev_uc_sync(vf_netdev, ndev);
  1788. dev_mc_sync(vf_netdev, ndev);
  1789. netif_addr_unlock_bh(ndev);
  1790. if (netif_running(ndev)) {
  1791. ret = dev_open(vf_netdev, NULL);
  1792. if (ret)
  1793. netdev_warn(vf_netdev,
  1794. "unable to open: %d\n", ret);
  1795. }
  1796. }
  1797. /* Setup VF as slave of the synthetic device.
  1798. * Runs in workqueue to avoid recursion in netlink callbacks.
  1799. */
  1800. static void netvsc_vf_setup(struct work_struct *w)
  1801. {
  1802. struct net_device_context *ndev_ctx
  1803. = container_of(w, struct net_device_context, vf_takeover.work);
  1804. struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1805. struct net_device *vf_netdev;
  1806. if (!rtnl_trylock()) {
  1807. schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
  1808. return;
  1809. }
  1810. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1811. if (vf_netdev)
  1812. __netvsc_vf_setup(ndev, vf_netdev);
  1813. rtnl_unlock();
  1814. }
  1815. /* Find netvsc by VF serial number.
  1816. * The PCI hyperv controller records the serial number as the slot kobj name.
  1817. */
  1818. static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
  1819. {
  1820. struct device *parent = vf_netdev->dev.parent;
  1821. struct net_device_context *ndev_ctx;
  1822. struct net_device *ndev;
  1823. struct pci_dev *pdev;
  1824. u32 serial;
  1825. if (!parent || !dev_is_pci(parent))
  1826. return NULL; /* not a PCI device */
  1827. pdev = to_pci_dev(parent);
  1828. if (!pdev->slot) {
  1829. netdev_notice(vf_netdev, "no PCI slot information\n");
  1830. return NULL;
  1831. }
  1832. if (kstrtou32(pci_slot_name(pdev->slot), 10, &serial)) {
  1833. netdev_notice(vf_netdev, "Invalid vf serial:%s\n",
  1834. pci_slot_name(pdev->slot));
  1835. return NULL;
  1836. }
  1837. list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
  1838. if (!ndev_ctx->vf_alloc)
  1839. continue;
  1840. if (ndev_ctx->vf_serial != serial)
  1841. continue;
  1842. ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1843. if (ndev->addr_len != vf_netdev->addr_len ||
  1844. memcmp(ndev->perm_addr, vf_netdev->perm_addr,
  1845. ndev->addr_len) != 0)
  1846. continue;
  1847. return ndev;
  1848. }
  1849. /* Fallback path to check synthetic vf with help of mac addr.
  1850. * Because this function can be called before vf_netdev is
  1851. * initialized (NETDEV_POST_INIT) when its perm_addr has not been copied
  1852. * from dev_addr, also try to match to its dev_addr.
  1853. * Note: On Hyper-V and Azure, it's not possible to set a MAC address
  1854. * on a VF that matches to the MAC of a unrelated NETVSC device.
  1855. */
  1856. list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
  1857. ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1858. if (ether_addr_equal(vf_netdev->perm_addr, ndev->perm_addr) ||
  1859. ether_addr_equal(vf_netdev->dev_addr, ndev->perm_addr))
  1860. return ndev;
  1861. }
  1862. netdev_notice(vf_netdev,
  1863. "no netdev found for vf serial:%u\n", serial);
  1864. return NULL;
  1865. }
  1866. static int netvsc_prepare_bonding(struct net_device *vf_netdev)
  1867. {
  1868. struct net_device *ndev;
  1869. ndev = get_netvsc_byslot(vf_netdev);
  1870. if (!ndev)
  1871. return NOTIFY_DONE;
  1872. /* Set slave flag and no addrconf flag before open
  1873. * to prevent IPv6 addrconf.
  1874. */
  1875. vf_netdev->flags |= IFF_SLAVE;
  1876. vf_netdev->priv_flags |= IFF_NO_ADDRCONF;
  1877. return NOTIFY_DONE;
  1878. }
  1879. static int netvsc_register_vf(struct net_device *vf_netdev, int context)
  1880. {
  1881. struct net_device_context *net_device_ctx;
  1882. struct netvsc_device *netvsc_dev;
  1883. struct bpf_prog *prog;
  1884. struct net_device *ndev;
  1885. int ret;
  1886. if (vf_netdev->addr_len != ETH_ALEN)
  1887. return NOTIFY_DONE;
  1888. ndev = get_netvsc_byslot(vf_netdev);
  1889. if (!ndev)
  1890. return NOTIFY_DONE;
  1891. net_device_ctx = netdev_priv(ndev);
  1892. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1893. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1894. return NOTIFY_DONE;
  1895. /* if synthetic interface is a different namespace,
  1896. * then move the VF to that namespace; join will be
  1897. * done again in that context.
  1898. */
  1899. if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
  1900. ret = dev_change_net_namespace(vf_netdev,
  1901. dev_net(ndev), "eth%d");
  1902. if (ret)
  1903. netdev_err(vf_netdev,
  1904. "could not move to same namespace as %s: %d\n",
  1905. ndev->name, ret);
  1906. else
  1907. netdev_info(vf_netdev,
  1908. "VF moved to namespace with: %s\n",
  1909. ndev->name);
  1910. return NOTIFY_DONE;
  1911. }
  1912. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1913. if (netvsc_vf_join(vf_netdev, ndev, context) != 0)
  1914. return NOTIFY_DONE;
  1915. dev_hold(vf_netdev);
  1916. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1917. if (ndev->needed_headroom < vf_netdev->needed_headroom)
  1918. ndev->needed_headroom = vf_netdev->needed_headroom;
  1919. vf_netdev->wanted_features = ndev->features;
  1920. netdev_update_features(vf_netdev);
  1921. prog = netvsc_xdp_get(netvsc_dev);
  1922. netvsc_vf_setxdp(vf_netdev, prog);
  1923. return NOTIFY_OK;
  1924. }
  1925. /* Change the data path when VF UP/DOWN/CHANGE are detected.
  1926. *
  1927. * Typically a UP or DOWN event is followed by a CHANGE event, so
  1928. * net_device_ctx->data_path_is_vf is used to cache the current data path
  1929. * to avoid the duplicate call of netvsc_switch_datapath() and the duplicate
  1930. * message.
  1931. *
  1932. * During hibernation, if a VF NIC driver (e.g. mlx5) preserves the network
  1933. * interface, there is only the CHANGE event and no UP or DOWN event.
  1934. */
  1935. static int netvsc_vf_changed(struct net_device *vf_netdev, unsigned long event)
  1936. {
  1937. struct net_device_context *net_device_ctx;
  1938. struct netvsc_device *netvsc_dev;
  1939. struct net_device *ndev;
  1940. bool vf_is_up = false;
  1941. int ret;
  1942. if (event != NETDEV_GOING_DOWN)
  1943. vf_is_up = netif_running(vf_netdev);
  1944. ndev = get_netvsc_byref(vf_netdev);
  1945. if (!ndev)
  1946. return NOTIFY_DONE;
  1947. net_device_ctx = netdev_priv(ndev);
  1948. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1949. if (!netvsc_dev)
  1950. return NOTIFY_DONE;
  1951. if (net_device_ctx->data_path_is_vf == vf_is_up)
  1952. return NOTIFY_OK;
  1953. if (vf_is_up && !net_device_ctx->vf_alloc) {
  1954. netdev_info(ndev, "Waiting for the VF association from host\n");
  1955. wait_for_completion(&net_device_ctx->vf_add);
  1956. }
  1957. ret = netvsc_switch_datapath(ndev, vf_is_up);
  1958. if (ret) {
  1959. netdev_err(ndev,
  1960. "Data path failed to switch %s VF: %s, err: %d\n",
  1961. vf_is_up ? "to" : "from", vf_netdev->name, ret);
  1962. return NOTIFY_DONE;
  1963. } else {
  1964. netdev_info(ndev, "Data path switched %s VF: %s\n",
  1965. vf_is_up ? "to" : "from", vf_netdev->name);
  1966. /* In Azure, when accelerated networking in enabled, other NICs
  1967. * like MANA, MLX, are configured as a bonded nic with
  1968. * Netvsc(failover) NIC. For bonded NICs, the min of the max
  1969. * pkt aggregate size of the members is propagated in the stack.
  1970. * In order to allow these NICs (MANA/MLX) to use up to
  1971. * GSO_MAX_SIZE gso packet size, we need to allow Netvsc NIC to
  1972. * also support this in the guest.
  1973. * This value is only increased for netvsc NIC when datapath is
  1974. * switched over to the VF
  1975. */
  1976. if (vf_is_up)
  1977. netif_set_tso_max_size(ndev, vf_netdev->tso_max_size);
  1978. else
  1979. netif_set_tso_max_size(ndev, netvsc_dev->netvsc_gso_max_size);
  1980. }
  1981. return NOTIFY_OK;
  1982. }
  1983. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1984. {
  1985. struct net_device *ndev;
  1986. struct net_device_context *net_device_ctx;
  1987. ndev = get_netvsc_byref(vf_netdev);
  1988. if (!ndev)
  1989. return NOTIFY_DONE;
  1990. net_device_ctx = netdev_priv(ndev);
  1991. cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
  1992. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1993. reinit_completion(&net_device_ctx->vf_add);
  1994. netdev_rx_handler_unregister(vf_netdev);
  1995. netdev_upper_dev_unlink(vf_netdev, ndev);
  1996. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1997. dev_put(vf_netdev);
  1998. ndev->needed_headroom = RNDIS_AND_PPI_SIZE;
  1999. return NOTIFY_OK;
  2000. }
  2001. static int check_dev_is_matching_vf(struct net_device *event_ndev)
  2002. {
  2003. /* Skip NetVSC interfaces */
  2004. if (event_ndev->netdev_ops == &device_ops)
  2005. return -ENODEV;
  2006. /* Avoid non-Ethernet type devices */
  2007. if (event_ndev->type != ARPHRD_ETHER)
  2008. return -ENODEV;
  2009. /* Avoid Vlan dev with same MAC registering as VF */
  2010. if (is_vlan_dev(event_ndev))
  2011. return -ENODEV;
  2012. /* Avoid Bonding master dev with same MAC registering as VF */
  2013. if (netif_is_bond_master(event_ndev))
  2014. return -ENODEV;
  2015. return 0;
  2016. }
  2017. static int netvsc_probe(struct hv_device *dev,
  2018. const struct hv_vmbus_device_id *dev_id)
  2019. {
  2020. struct net_device *net = NULL, *vf_netdev;
  2021. struct net_device_context *net_device_ctx;
  2022. struct netvsc_device_info *device_info = NULL;
  2023. struct netvsc_device *nvdev;
  2024. int ret = -ENOMEM;
  2025. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  2026. VRSS_CHANNEL_MAX);
  2027. if (!net)
  2028. goto no_net;
  2029. netif_carrier_off(net);
  2030. netvsc_init_settings(net);
  2031. net_device_ctx = netdev_priv(net);
  2032. net_device_ctx->device_ctx = dev;
  2033. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  2034. if (netif_msg_probe(net_device_ctx))
  2035. netdev_dbg(net, "netvsc msg_enable: %d\n",
  2036. net_device_ctx->msg_enable);
  2037. hv_set_drvdata(dev, net);
  2038. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  2039. init_completion(&net_device_ctx->vf_add);
  2040. spin_lock_init(&net_device_ctx->lock);
  2041. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  2042. INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
  2043. INIT_DELAYED_WORK(&net_device_ctx->vfns_work, netvsc_vfns_work);
  2044. net_device_ctx->vf_stats
  2045. = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
  2046. if (!net_device_ctx->vf_stats)
  2047. goto no_stats;
  2048. net->netdev_ops = &device_ops;
  2049. net->ethtool_ops = &ethtool_ops;
  2050. SET_NETDEV_DEV(net, &dev->device);
  2051. dma_set_min_align_mask(&dev->device, HV_HYP_PAGE_SIZE - 1);
  2052. /* We always need headroom for rndis header */
  2053. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  2054. /* Initialize the number of queues to be 1, we may change it if more
  2055. * channels are offered later.
  2056. */
  2057. netif_set_real_num_tx_queues(net, 1);
  2058. netif_set_real_num_rx_queues(net, 1);
  2059. /* Notify the netvsc driver of the new device */
  2060. device_info = netvsc_devinfo_get(NULL);
  2061. if (!device_info) {
  2062. ret = -ENOMEM;
  2063. goto devinfo_failed;
  2064. }
  2065. /* We must get rtnl lock before scheduling nvdev->subchan_work,
  2066. * otherwise netvsc_subchan_work() can get rtnl lock first and wait
  2067. * all subchannels to show up, but that may not happen because
  2068. * netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
  2069. * -> ... -> device_add() -> ... -> __device_attach() can't get
  2070. * the device lock, so all the subchannels can't be processed --
  2071. * finally netvsc_subchan_work() hangs forever.
  2072. *
  2073. * The rtnl lock also needs to be held before rndis_filter_device_add()
  2074. * which advertises nvsp_2_vsc_capability / sriov bit, and triggers
  2075. * VF NIC offering and registering. If VF NIC finished register_netdev()
  2076. * earlier it may cause name based config failure.
  2077. */
  2078. rtnl_lock();
  2079. nvdev = rndis_filter_device_add(dev, device_info);
  2080. if (IS_ERR(nvdev)) {
  2081. ret = PTR_ERR(nvdev);
  2082. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  2083. goto rndis_failed;
  2084. }
  2085. eth_hw_addr_set(net, device_info->mac_adr);
  2086. if (nvdev->num_chn > 1)
  2087. schedule_work(&nvdev->subchan_work);
  2088. /* hw_features computed in rndis_netdev_set_hwcaps() */
  2089. net->features = net->hw_features |
  2090. NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX |
  2091. NETIF_F_HW_VLAN_CTAG_RX;
  2092. net->vlan_features = net->features;
  2093. netdev_lockdep_set_classes(net);
  2094. net->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
  2095. NETDEV_XDP_ACT_NDO_XMIT;
  2096. /* MTU range: 68 - 1500 or 65521 */
  2097. net->min_mtu = NETVSC_MTU_MIN;
  2098. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  2099. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  2100. else
  2101. net->max_mtu = ETH_DATA_LEN;
  2102. nvdev->tx_disable = false;
  2103. ret = register_netdevice(net);
  2104. if (ret != 0) {
  2105. pr_err("Unable to register netdev.\n");
  2106. goto register_failed;
  2107. }
  2108. list_add(&net_device_ctx->list, &netvsc_dev_list);
  2109. /* When the hv_netvsc driver is unloaded and reloaded, the
  2110. * NET_DEVICE_REGISTER for the vf device is replayed before probe
  2111. * is complete. This is because register_netdevice_notifier() gets
  2112. * registered before vmbus_driver_register() so that callback func
  2113. * is set before probe and we don't miss events like NETDEV_POST_INIT
  2114. * So, in this section we try to register the matching vf device that
  2115. * is present as a netdevice, knowing that its register call is not
  2116. * processed in the netvsc_netdev_notifier(as probing is progress and
  2117. * get_netvsc_byslot fails).
  2118. */
  2119. for_each_netdev(dev_net(net), vf_netdev) {
  2120. ret = check_dev_is_matching_vf(vf_netdev);
  2121. if (ret != 0)
  2122. continue;
  2123. if (net != get_netvsc_byslot(vf_netdev))
  2124. continue;
  2125. netvsc_prepare_bonding(vf_netdev);
  2126. netdev_lock_ops(vf_netdev);
  2127. netvsc_register_vf(vf_netdev, VF_REG_IN_PROBE);
  2128. netdev_unlock_ops(vf_netdev);
  2129. __netvsc_vf_setup(net, vf_netdev);
  2130. break;
  2131. }
  2132. rtnl_unlock();
  2133. netvsc_devinfo_put(device_info);
  2134. return 0;
  2135. register_failed:
  2136. rndis_filter_device_remove(dev, nvdev);
  2137. rndis_failed:
  2138. rtnl_unlock();
  2139. netvsc_devinfo_put(device_info);
  2140. devinfo_failed:
  2141. free_percpu(net_device_ctx->vf_stats);
  2142. no_stats:
  2143. hv_set_drvdata(dev, NULL);
  2144. free_netdev(net);
  2145. no_net:
  2146. return ret;
  2147. }
  2148. static void netvsc_remove(struct hv_device *dev)
  2149. {
  2150. struct net_device_context *ndev_ctx;
  2151. struct net_device *vf_netdev, *net;
  2152. struct netvsc_device *nvdev;
  2153. net = hv_get_drvdata(dev);
  2154. if (net == NULL) {
  2155. dev_err(&dev->device, "No net device to remove\n");
  2156. return;
  2157. }
  2158. ndev_ctx = netdev_priv(net);
  2159. cancel_delayed_work_sync(&ndev_ctx->dwork);
  2160. rtnl_lock();
  2161. cancel_delayed_work_sync(&ndev_ctx->vfns_work);
  2162. nvdev = rtnl_dereference(ndev_ctx->nvdev);
  2163. if (nvdev) {
  2164. cancel_work_sync(&nvdev->subchan_work);
  2165. netvsc_xdp_set(net, NULL, NULL, nvdev);
  2166. }
  2167. /*
  2168. * Call to the vsc driver to let it know that the device is being
  2169. * removed. Also blocks mtu and channel changes.
  2170. */
  2171. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  2172. if (vf_netdev)
  2173. netvsc_unregister_vf(vf_netdev);
  2174. if (nvdev)
  2175. rndis_filter_device_remove(dev, nvdev);
  2176. unregister_netdevice(net);
  2177. list_del(&ndev_ctx->list);
  2178. rtnl_unlock();
  2179. hv_set_drvdata(dev, NULL);
  2180. free_percpu(ndev_ctx->vf_stats);
  2181. free_netdev(net);
  2182. }
  2183. static int netvsc_suspend(struct hv_device *dev)
  2184. {
  2185. struct net_device_context *ndev_ctx;
  2186. struct netvsc_device *nvdev;
  2187. struct net_device *net;
  2188. int ret;
  2189. net = hv_get_drvdata(dev);
  2190. ndev_ctx = netdev_priv(net);
  2191. cancel_delayed_work_sync(&ndev_ctx->dwork);
  2192. rtnl_lock();
  2193. cancel_delayed_work_sync(&ndev_ctx->vfns_work);
  2194. nvdev = rtnl_dereference(ndev_ctx->nvdev);
  2195. if (nvdev == NULL) {
  2196. ret = -ENODEV;
  2197. goto out;
  2198. }
  2199. /* Save the current config info */
  2200. ndev_ctx->saved_netvsc_dev_info = netvsc_devinfo_get(nvdev);
  2201. if (!ndev_ctx->saved_netvsc_dev_info) {
  2202. ret = -ENOMEM;
  2203. goto out;
  2204. }
  2205. ret = netvsc_detach(net, nvdev);
  2206. out:
  2207. rtnl_unlock();
  2208. return ret;
  2209. }
  2210. static int netvsc_resume(struct hv_device *dev)
  2211. {
  2212. struct net_device *net = hv_get_drvdata(dev);
  2213. struct net_device_context *net_device_ctx;
  2214. struct netvsc_device_info *device_info;
  2215. int ret;
  2216. rtnl_lock();
  2217. net_device_ctx = netdev_priv(net);
  2218. /* Reset the data path to the netvsc NIC before re-opening the vmbus
  2219. * channel. Later netvsc_netdev_event() will switch the data path to
  2220. * the VF upon the UP or CHANGE event.
  2221. */
  2222. net_device_ctx->data_path_is_vf = false;
  2223. device_info = net_device_ctx->saved_netvsc_dev_info;
  2224. ret = netvsc_attach(net, device_info);
  2225. netvsc_devinfo_put(device_info);
  2226. net_device_ctx->saved_netvsc_dev_info = NULL;
  2227. rtnl_unlock();
  2228. return ret;
  2229. }
  2230. static const struct hv_vmbus_device_id id_table[] = {
  2231. /* Network guid */
  2232. { HV_NIC_GUID, },
  2233. { },
  2234. };
  2235. MODULE_DEVICE_TABLE(vmbus, id_table);
  2236. /* The one and only one */
  2237. static struct hv_driver netvsc_drv = {
  2238. .name = KBUILD_MODNAME,
  2239. .id_table = id_table,
  2240. .probe = netvsc_probe,
  2241. .remove = netvsc_remove,
  2242. .suspend = netvsc_suspend,
  2243. .resume = netvsc_resume,
  2244. .driver = {
  2245. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  2246. },
  2247. };
  2248. /* Set VF's namespace same as the synthetic NIC */
  2249. static void netvsc_event_set_vf_ns(struct net_device *ndev)
  2250. {
  2251. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  2252. struct net_device *vf_netdev;
  2253. int ret;
  2254. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  2255. if (!vf_netdev)
  2256. return;
  2257. if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
  2258. ret = dev_change_net_namespace(vf_netdev, dev_net(ndev),
  2259. "eth%d");
  2260. if (ret)
  2261. netdev_err(vf_netdev,
  2262. "Cannot move to same namespace as %s: %d\n",
  2263. ndev->name, ret);
  2264. else
  2265. netdev_info(vf_netdev,
  2266. "Moved VF to namespace with: %s\n",
  2267. ndev->name);
  2268. }
  2269. }
  2270. void netvsc_vfns_work(struct work_struct *w)
  2271. {
  2272. struct net_device_context *ndev_ctx =
  2273. container_of(w, struct net_device_context, vfns_work.work);
  2274. struct net_device *ndev;
  2275. if (!rtnl_trylock()) {
  2276. schedule_delayed_work(&ndev_ctx->vfns_work, 1);
  2277. return;
  2278. }
  2279. ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  2280. if (!ndev)
  2281. goto out;
  2282. netvsc_event_set_vf_ns(ndev);
  2283. out:
  2284. rtnl_unlock();
  2285. }
  2286. /*
  2287. * On Hyper-V, every VF interface is matched with a corresponding
  2288. * synthetic interface. The synthetic interface is presented first
  2289. * to the guest. When the corresponding VF instance is registered,
  2290. * we will take care of switching the data path.
  2291. */
  2292. static int netvsc_netdev_event(struct notifier_block *this,
  2293. unsigned long event, void *ptr)
  2294. {
  2295. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  2296. struct net_device_context *ndev_ctx;
  2297. int ret = 0;
  2298. if (event_dev->netdev_ops == &device_ops && event == NETDEV_REGISTER) {
  2299. ndev_ctx = netdev_priv(event_dev);
  2300. schedule_delayed_work(&ndev_ctx->vfns_work, 0);
  2301. return NOTIFY_DONE;
  2302. }
  2303. ret = check_dev_is_matching_vf(event_dev);
  2304. if (ret != 0)
  2305. return NOTIFY_DONE;
  2306. switch (event) {
  2307. case NETDEV_POST_INIT:
  2308. return netvsc_prepare_bonding(event_dev);
  2309. case NETDEV_REGISTER:
  2310. return netvsc_register_vf(event_dev, VF_REG_IN_NOTIFIER);
  2311. case NETDEV_UNREGISTER:
  2312. return netvsc_unregister_vf(event_dev);
  2313. case NETDEV_UP:
  2314. case NETDEV_DOWN:
  2315. case NETDEV_CHANGE:
  2316. case NETDEV_GOING_DOWN:
  2317. return netvsc_vf_changed(event_dev, event);
  2318. default:
  2319. return NOTIFY_DONE;
  2320. }
  2321. }
  2322. static struct notifier_block netvsc_netdev_notifier = {
  2323. .notifier_call = netvsc_netdev_event,
  2324. };
  2325. static void __exit netvsc_drv_exit(void)
  2326. {
  2327. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  2328. vmbus_driver_unregister(&netvsc_drv);
  2329. }
  2330. static int __init netvsc_drv_init(void)
  2331. {
  2332. int ret;
  2333. if (ring_size < RING_SIZE_MIN) {
  2334. ring_size = RING_SIZE_MIN;
  2335. pr_info("Increased ring_size to %u (min allowed)\n",
  2336. ring_size);
  2337. }
  2338. netvsc_ring_bytes = VMBUS_RING_SIZE(ring_size * 4096);
  2339. register_netdevice_notifier(&netvsc_netdev_notifier);
  2340. ret = vmbus_driver_register(&netvsc_drv);
  2341. if (ret)
  2342. goto err_vmbus_reg;
  2343. return 0;
  2344. err_vmbus_reg:
  2345. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  2346. return ret;
  2347. }
  2348. MODULE_LICENSE("GPL");
  2349. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  2350. module_init(netvsc_drv_init);
  2351. module_exit(netvsc_drv_exit);