netvsc_drv.c 56 KB

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