netvsc_drv.c 57 KB

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