netvsc_drv.c 69 KB

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