netvsc_drv.c 71 KB

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