netvsc_drv.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  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. void *accel_priv,
  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. accel_priv, fallback);
  284. else
  285. txq = fallback(vf_netdev, skb);
  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. /* Note: enable and attach happen when sub-channels setup */
  742. netif_carrier_off(ndev);
  743. if (netif_running(ndev)) {
  744. ret = rndis_filter_open(nvdev);
  745. if (ret)
  746. return ret;
  747. rdev = nvdev->extension;
  748. if (!rdev->link_state)
  749. netif_carrier_on(ndev);
  750. }
  751. return 0;
  752. }
  753. static int netvsc_set_channels(struct net_device *net,
  754. struct ethtool_channels *channels)
  755. {
  756. struct net_device_context *net_device_ctx = netdev_priv(net);
  757. struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
  758. unsigned int orig, count = channels->combined_count;
  759. struct netvsc_device_info device_info;
  760. int ret;
  761. /* We do not support separate count for rx, tx, or other */
  762. if (count == 0 ||
  763. channels->rx_count || channels->tx_count || channels->other_count)
  764. return -EINVAL;
  765. if (!nvdev || nvdev->destroy)
  766. return -ENODEV;
  767. if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
  768. return -EINVAL;
  769. if (count > nvdev->max_chn)
  770. return -EINVAL;
  771. orig = nvdev->num_chn;
  772. memset(&device_info, 0, sizeof(device_info));
  773. device_info.num_chn = count;
  774. device_info.send_sections = nvdev->send_section_cnt;
  775. device_info.send_section_size = nvdev->send_section_size;
  776. device_info.recv_sections = nvdev->recv_section_cnt;
  777. device_info.recv_section_size = nvdev->recv_section_size;
  778. ret = netvsc_detach(net, nvdev);
  779. if (ret)
  780. return ret;
  781. ret = netvsc_attach(net, &device_info);
  782. if (ret) {
  783. device_info.num_chn = orig;
  784. if (netvsc_attach(net, &device_info))
  785. netdev_err(net, "restoring channel setting failed\n");
  786. }
  787. return ret;
  788. }
  789. static bool
  790. netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
  791. {
  792. struct ethtool_link_ksettings diff1 = *cmd;
  793. struct ethtool_link_ksettings diff2 = {};
  794. diff1.base.speed = 0;
  795. diff1.base.duplex = 0;
  796. /* advertising and cmd are usually set */
  797. ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
  798. diff1.base.cmd = 0;
  799. /* We set port to PORT_OTHER */
  800. diff2.base.port = PORT_OTHER;
  801. return !memcmp(&diff1, &diff2, sizeof(diff1));
  802. }
  803. static void netvsc_init_settings(struct net_device *dev)
  804. {
  805. struct net_device_context *ndc = netdev_priv(dev);
  806. ndc->l4_hash = HV_DEFAULT_L4HASH;
  807. ndc->speed = SPEED_UNKNOWN;
  808. ndc->duplex = DUPLEX_FULL;
  809. }
  810. static int netvsc_get_link_ksettings(struct net_device *dev,
  811. struct ethtool_link_ksettings *cmd)
  812. {
  813. struct net_device_context *ndc = netdev_priv(dev);
  814. cmd->base.speed = ndc->speed;
  815. cmd->base.duplex = ndc->duplex;
  816. cmd->base.port = PORT_OTHER;
  817. return 0;
  818. }
  819. static int netvsc_set_link_ksettings(struct net_device *dev,
  820. const struct ethtool_link_ksettings *cmd)
  821. {
  822. struct net_device_context *ndc = netdev_priv(dev);
  823. u32 speed;
  824. speed = cmd->base.speed;
  825. if (!ethtool_validate_speed(speed) ||
  826. !ethtool_validate_duplex(cmd->base.duplex) ||
  827. !netvsc_validate_ethtool_ss_cmd(cmd))
  828. return -EINVAL;
  829. ndc->speed = speed;
  830. ndc->duplex = cmd->base.duplex;
  831. return 0;
  832. }
  833. static int netvsc_change_mtu(struct net_device *ndev, int mtu)
  834. {
  835. struct net_device_context *ndevctx = netdev_priv(ndev);
  836. struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
  837. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  838. int orig_mtu = ndev->mtu;
  839. struct netvsc_device_info device_info;
  840. int ret = 0;
  841. if (!nvdev || nvdev->destroy)
  842. return -ENODEV;
  843. /* Change MTU of underlying VF netdev first. */
  844. if (vf_netdev) {
  845. ret = dev_set_mtu(vf_netdev, mtu);
  846. if (ret)
  847. return ret;
  848. }
  849. memset(&device_info, 0, sizeof(device_info));
  850. device_info.num_chn = nvdev->num_chn;
  851. device_info.send_sections = nvdev->send_section_cnt;
  852. device_info.send_section_size = nvdev->send_section_size;
  853. device_info.recv_sections = nvdev->recv_section_cnt;
  854. device_info.recv_section_size = nvdev->recv_section_size;
  855. ret = netvsc_detach(ndev, nvdev);
  856. if (ret)
  857. goto rollback_vf;
  858. ndev->mtu = mtu;
  859. ret = netvsc_attach(ndev, &device_info);
  860. if (ret)
  861. goto rollback;
  862. return 0;
  863. rollback:
  864. /* Attempt rollback to original MTU */
  865. ndev->mtu = orig_mtu;
  866. if (netvsc_attach(ndev, &device_info))
  867. netdev_err(ndev, "restoring mtu failed\n");
  868. rollback_vf:
  869. if (vf_netdev)
  870. dev_set_mtu(vf_netdev, orig_mtu);
  871. return ret;
  872. }
  873. static void netvsc_get_vf_stats(struct net_device *net,
  874. struct netvsc_vf_pcpu_stats *tot)
  875. {
  876. struct net_device_context *ndev_ctx = netdev_priv(net);
  877. int i;
  878. memset(tot, 0, sizeof(*tot));
  879. for_each_possible_cpu(i) {
  880. const struct netvsc_vf_pcpu_stats *stats
  881. = per_cpu_ptr(ndev_ctx->vf_stats, i);
  882. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  883. unsigned int start;
  884. do {
  885. start = u64_stats_fetch_begin_irq(&stats->syncp);
  886. rx_packets = stats->rx_packets;
  887. tx_packets = stats->tx_packets;
  888. rx_bytes = stats->rx_bytes;
  889. tx_bytes = stats->tx_bytes;
  890. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  891. tot->rx_packets += rx_packets;
  892. tot->tx_packets += tx_packets;
  893. tot->rx_bytes += rx_bytes;
  894. tot->tx_bytes += tx_bytes;
  895. tot->tx_dropped += stats->tx_dropped;
  896. }
  897. }
  898. static void netvsc_get_stats64(struct net_device *net,
  899. struct rtnl_link_stats64 *t)
  900. {
  901. struct net_device_context *ndev_ctx = netdev_priv(net);
  902. struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
  903. struct netvsc_vf_pcpu_stats vf_tot;
  904. int i;
  905. if (!nvdev)
  906. return;
  907. netdev_stats_to_stats64(t, &net->stats);
  908. netvsc_get_vf_stats(net, &vf_tot);
  909. t->rx_packets += vf_tot.rx_packets;
  910. t->tx_packets += vf_tot.tx_packets;
  911. t->rx_bytes += vf_tot.rx_bytes;
  912. t->tx_bytes += vf_tot.tx_bytes;
  913. t->tx_dropped += vf_tot.tx_dropped;
  914. for (i = 0; i < nvdev->num_chn; i++) {
  915. const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
  916. const struct netvsc_stats *stats;
  917. u64 packets, bytes, multicast;
  918. unsigned int start;
  919. stats = &nvchan->tx_stats;
  920. do {
  921. start = u64_stats_fetch_begin_irq(&stats->syncp);
  922. packets = stats->packets;
  923. bytes = stats->bytes;
  924. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  925. t->tx_bytes += bytes;
  926. t->tx_packets += packets;
  927. stats = &nvchan->rx_stats;
  928. do {
  929. start = u64_stats_fetch_begin_irq(&stats->syncp);
  930. packets = stats->packets;
  931. bytes = stats->bytes;
  932. multicast = stats->multicast + stats->broadcast;
  933. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  934. t->rx_bytes += bytes;
  935. t->rx_packets += packets;
  936. t->multicast += multicast;
  937. }
  938. }
  939. static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
  940. {
  941. struct net_device_context *ndc = netdev_priv(ndev);
  942. struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
  943. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  944. struct sockaddr *addr = p;
  945. int err;
  946. err = eth_prepare_mac_addr_change(ndev, p);
  947. if (err)
  948. return err;
  949. if (!nvdev)
  950. return -ENODEV;
  951. if (vf_netdev) {
  952. err = dev_set_mac_address(vf_netdev, addr);
  953. if (err)
  954. return err;
  955. }
  956. err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
  957. if (!err) {
  958. eth_commit_mac_addr_change(ndev, p);
  959. } else if (vf_netdev) {
  960. /* rollback change on VF */
  961. memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
  962. dev_set_mac_address(vf_netdev, addr);
  963. }
  964. return err;
  965. }
  966. static const struct {
  967. char name[ETH_GSTRING_LEN];
  968. u16 offset;
  969. } netvsc_stats[] = {
  970. { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
  971. { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
  972. { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
  973. { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
  974. { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
  975. { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
  976. { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
  977. { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
  978. { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
  979. { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
  980. }, vf_stats[] = {
  981. { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
  982. { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
  983. { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
  984. { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
  985. { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
  986. };
  987. #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
  988. #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
  989. /* 4 statistics per queue (rx/tx packets/bytes) */
  990. #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
  991. static int netvsc_get_sset_count(struct net_device *dev, int string_set)
  992. {
  993. struct net_device_context *ndc = netdev_priv(dev);
  994. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  995. if (!nvdev)
  996. return -ENODEV;
  997. switch (string_set) {
  998. case ETH_SS_STATS:
  999. return NETVSC_GLOBAL_STATS_LEN
  1000. + NETVSC_VF_STATS_LEN
  1001. + NETVSC_QUEUE_STATS_LEN(nvdev);
  1002. default:
  1003. return -EINVAL;
  1004. }
  1005. }
  1006. static void netvsc_get_ethtool_stats(struct net_device *dev,
  1007. struct ethtool_stats *stats, u64 *data)
  1008. {
  1009. struct net_device_context *ndc = netdev_priv(dev);
  1010. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1011. const void *nds = &ndc->eth_stats;
  1012. const struct netvsc_stats *qstats;
  1013. struct netvsc_vf_pcpu_stats sum;
  1014. unsigned int start;
  1015. u64 packets, bytes;
  1016. int i, j;
  1017. if (!nvdev)
  1018. return;
  1019. for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
  1020. data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
  1021. netvsc_get_vf_stats(dev, &sum);
  1022. for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
  1023. data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
  1024. for (j = 0; j < nvdev->num_chn; j++) {
  1025. qstats = &nvdev->chan_table[j].tx_stats;
  1026. do {
  1027. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1028. packets = qstats->packets;
  1029. bytes = qstats->bytes;
  1030. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1031. data[i++] = packets;
  1032. data[i++] = bytes;
  1033. qstats = &nvdev->chan_table[j].rx_stats;
  1034. do {
  1035. start = u64_stats_fetch_begin_irq(&qstats->syncp);
  1036. packets = qstats->packets;
  1037. bytes = qstats->bytes;
  1038. } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
  1039. data[i++] = packets;
  1040. data[i++] = bytes;
  1041. }
  1042. }
  1043. static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1044. {
  1045. struct net_device_context *ndc = netdev_priv(dev);
  1046. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1047. u8 *p = data;
  1048. int i;
  1049. if (!nvdev)
  1050. return;
  1051. switch (stringset) {
  1052. case ETH_SS_STATS:
  1053. for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
  1054. memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
  1055. p += ETH_GSTRING_LEN;
  1056. }
  1057. for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
  1058. memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
  1059. p += ETH_GSTRING_LEN;
  1060. }
  1061. for (i = 0; i < nvdev->num_chn; i++) {
  1062. sprintf(p, "tx_queue_%u_packets", i);
  1063. p += ETH_GSTRING_LEN;
  1064. sprintf(p, "tx_queue_%u_bytes", i);
  1065. p += ETH_GSTRING_LEN;
  1066. sprintf(p, "rx_queue_%u_packets", i);
  1067. p += ETH_GSTRING_LEN;
  1068. sprintf(p, "rx_queue_%u_bytes", i);
  1069. p += ETH_GSTRING_LEN;
  1070. }
  1071. break;
  1072. }
  1073. }
  1074. static int
  1075. netvsc_get_rss_hash_opts(struct net_device_context *ndc,
  1076. struct ethtool_rxnfc *info)
  1077. {
  1078. const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
  1079. info->data = RXH_IP_SRC | RXH_IP_DST;
  1080. switch (info->flow_type) {
  1081. case TCP_V4_FLOW:
  1082. if (ndc->l4_hash & HV_TCP4_L4HASH)
  1083. info->data |= l4_flag;
  1084. break;
  1085. case TCP_V6_FLOW:
  1086. if (ndc->l4_hash & HV_TCP6_L4HASH)
  1087. info->data |= l4_flag;
  1088. break;
  1089. case UDP_V4_FLOW:
  1090. if (ndc->l4_hash & HV_UDP4_L4HASH)
  1091. info->data |= l4_flag;
  1092. break;
  1093. case UDP_V6_FLOW:
  1094. if (ndc->l4_hash & HV_UDP6_L4HASH)
  1095. info->data |= l4_flag;
  1096. break;
  1097. case IPV4_FLOW:
  1098. case IPV6_FLOW:
  1099. break;
  1100. default:
  1101. info->data = 0;
  1102. break;
  1103. }
  1104. return 0;
  1105. }
  1106. static int
  1107. netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  1108. u32 *rules)
  1109. {
  1110. struct net_device_context *ndc = netdev_priv(dev);
  1111. struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
  1112. if (!nvdev)
  1113. return -ENODEV;
  1114. switch (info->cmd) {
  1115. case ETHTOOL_GRXRINGS:
  1116. info->data = nvdev->num_chn;
  1117. return 0;
  1118. case ETHTOOL_GRXFH:
  1119. return netvsc_get_rss_hash_opts(ndc, info);
  1120. }
  1121. return -EOPNOTSUPP;
  1122. }
  1123. static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
  1124. struct ethtool_rxnfc *info)
  1125. {
  1126. if (info->data == (RXH_IP_SRC | RXH_IP_DST |
  1127. RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
  1128. switch (info->flow_type) {
  1129. case TCP_V4_FLOW:
  1130. ndc->l4_hash |= HV_TCP4_L4HASH;
  1131. break;
  1132. case TCP_V6_FLOW:
  1133. ndc->l4_hash |= HV_TCP6_L4HASH;
  1134. break;
  1135. case UDP_V4_FLOW:
  1136. ndc->l4_hash |= HV_UDP4_L4HASH;
  1137. break;
  1138. case UDP_V6_FLOW:
  1139. ndc->l4_hash |= HV_UDP6_L4HASH;
  1140. break;
  1141. default:
  1142. return -EOPNOTSUPP;
  1143. }
  1144. return 0;
  1145. }
  1146. if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
  1147. switch (info->flow_type) {
  1148. case TCP_V4_FLOW:
  1149. ndc->l4_hash &= ~HV_TCP4_L4HASH;
  1150. break;
  1151. case TCP_V6_FLOW:
  1152. ndc->l4_hash &= ~HV_TCP6_L4HASH;
  1153. break;
  1154. case UDP_V4_FLOW:
  1155. ndc->l4_hash &= ~HV_UDP4_L4HASH;
  1156. break;
  1157. case UDP_V6_FLOW:
  1158. ndc->l4_hash &= ~HV_UDP6_L4HASH;
  1159. break;
  1160. default:
  1161. return -EOPNOTSUPP;
  1162. }
  1163. return 0;
  1164. }
  1165. return -EOPNOTSUPP;
  1166. }
  1167. static int
  1168. netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
  1169. {
  1170. struct net_device_context *ndc = netdev_priv(ndev);
  1171. if (info->cmd == ETHTOOL_SRXFH)
  1172. return netvsc_set_rss_hash_opts(ndc, info);
  1173. return -EOPNOTSUPP;
  1174. }
  1175. #ifdef CONFIG_NET_POLL_CONTROLLER
  1176. static void netvsc_poll_controller(struct net_device *dev)
  1177. {
  1178. struct net_device_context *ndc = netdev_priv(dev);
  1179. struct netvsc_device *ndev;
  1180. int i;
  1181. rcu_read_lock();
  1182. ndev = rcu_dereference(ndc->nvdev);
  1183. if (ndev) {
  1184. for (i = 0; i < ndev->num_chn; i++) {
  1185. struct netvsc_channel *nvchan = &ndev->chan_table[i];
  1186. napi_schedule(&nvchan->napi);
  1187. }
  1188. }
  1189. rcu_read_unlock();
  1190. }
  1191. #endif
  1192. static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
  1193. {
  1194. return NETVSC_HASH_KEYLEN;
  1195. }
  1196. static u32 netvsc_rss_indir_size(struct net_device *dev)
  1197. {
  1198. return ITAB_NUM;
  1199. }
  1200. static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  1201. u8 *hfunc)
  1202. {
  1203. struct net_device_context *ndc = netdev_priv(dev);
  1204. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1205. struct rndis_device *rndis_dev;
  1206. int i;
  1207. if (!ndev)
  1208. return -ENODEV;
  1209. if (hfunc)
  1210. *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
  1211. rndis_dev = ndev->extension;
  1212. if (indir) {
  1213. for (i = 0; i < ITAB_NUM; i++)
  1214. indir[i] = rndis_dev->rx_table[i];
  1215. }
  1216. if (key)
  1217. memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
  1218. return 0;
  1219. }
  1220. static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
  1221. const u8 *key, const u8 hfunc)
  1222. {
  1223. struct net_device_context *ndc = netdev_priv(dev);
  1224. struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
  1225. struct rndis_device *rndis_dev;
  1226. int i;
  1227. if (!ndev)
  1228. return -ENODEV;
  1229. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  1230. return -EOPNOTSUPP;
  1231. rndis_dev = ndev->extension;
  1232. if (indir) {
  1233. for (i = 0; i < ITAB_NUM; i++)
  1234. if (indir[i] >= ndev->num_chn)
  1235. return -EINVAL;
  1236. for (i = 0; i < ITAB_NUM; i++)
  1237. rndis_dev->rx_table[i] = indir[i];
  1238. }
  1239. if (!key) {
  1240. if (!indir)
  1241. return 0;
  1242. key = rndis_dev->rss_key;
  1243. }
  1244. return rndis_filter_set_rss_param(rndis_dev, key);
  1245. }
  1246. /* Hyper-V RNDIS protocol does not have ring in the HW sense.
  1247. * It does have pre-allocated receive area which is divided into sections.
  1248. */
  1249. static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
  1250. struct ethtool_ringparam *ring)
  1251. {
  1252. u32 max_buf_size;
  1253. ring->rx_pending = nvdev->recv_section_cnt;
  1254. ring->tx_pending = nvdev->send_section_cnt;
  1255. if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
  1256. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
  1257. else
  1258. max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
  1259. ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
  1260. ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
  1261. / nvdev->send_section_size;
  1262. }
  1263. static void netvsc_get_ringparam(struct net_device *ndev,
  1264. struct ethtool_ringparam *ring)
  1265. {
  1266. struct net_device_context *ndevctx = netdev_priv(ndev);
  1267. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1268. if (!nvdev)
  1269. return;
  1270. __netvsc_get_ringparam(nvdev, ring);
  1271. }
  1272. static int netvsc_set_ringparam(struct net_device *ndev,
  1273. struct ethtool_ringparam *ring)
  1274. {
  1275. struct net_device_context *ndevctx = netdev_priv(ndev);
  1276. struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
  1277. struct netvsc_device_info device_info;
  1278. struct ethtool_ringparam orig;
  1279. u32 new_tx, new_rx;
  1280. int ret = 0;
  1281. if (!nvdev || nvdev->destroy)
  1282. return -ENODEV;
  1283. memset(&orig, 0, sizeof(orig));
  1284. __netvsc_get_ringparam(nvdev, &orig);
  1285. new_tx = clamp_t(u32, ring->tx_pending,
  1286. NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
  1287. new_rx = clamp_t(u32, ring->rx_pending,
  1288. NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
  1289. if (new_tx == orig.tx_pending &&
  1290. new_rx == orig.rx_pending)
  1291. return 0; /* no change */
  1292. memset(&device_info, 0, sizeof(device_info));
  1293. device_info.num_chn = nvdev->num_chn;
  1294. device_info.send_sections = new_tx;
  1295. device_info.send_section_size = nvdev->send_section_size;
  1296. device_info.recv_sections = new_rx;
  1297. device_info.recv_section_size = nvdev->recv_section_size;
  1298. ret = netvsc_detach(ndev, nvdev);
  1299. if (ret)
  1300. return ret;
  1301. ret = netvsc_attach(ndev, &device_info);
  1302. if (ret) {
  1303. device_info.send_sections = orig.tx_pending;
  1304. device_info.recv_sections = orig.rx_pending;
  1305. if (netvsc_attach(ndev, &device_info))
  1306. netdev_err(ndev, "restoring ringparam failed");
  1307. }
  1308. return ret;
  1309. }
  1310. static u32 netvsc_get_msglevel(struct net_device *ndev)
  1311. {
  1312. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1313. return ndev_ctx->msg_enable;
  1314. }
  1315. static void netvsc_set_msglevel(struct net_device *ndev, u32 val)
  1316. {
  1317. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1318. ndev_ctx->msg_enable = val;
  1319. }
  1320. static const struct ethtool_ops ethtool_ops = {
  1321. .get_drvinfo = netvsc_get_drvinfo,
  1322. .get_msglevel = netvsc_get_msglevel,
  1323. .set_msglevel = netvsc_set_msglevel,
  1324. .get_link = ethtool_op_get_link,
  1325. .get_ethtool_stats = netvsc_get_ethtool_stats,
  1326. .get_sset_count = netvsc_get_sset_count,
  1327. .get_strings = netvsc_get_strings,
  1328. .get_channels = netvsc_get_channels,
  1329. .set_channels = netvsc_set_channels,
  1330. .get_ts_info = ethtool_op_get_ts_info,
  1331. .get_rxnfc = netvsc_get_rxnfc,
  1332. .set_rxnfc = netvsc_set_rxnfc,
  1333. .get_rxfh_key_size = netvsc_get_rxfh_key_size,
  1334. .get_rxfh_indir_size = netvsc_rss_indir_size,
  1335. .get_rxfh = netvsc_get_rxfh,
  1336. .set_rxfh = netvsc_set_rxfh,
  1337. .get_link_ksettings = netvsc_get_link_ksettings,
  1338. .set_link_ksettings = netvsc_set_link_ksettings,
  1339. .get_ringparam = netvsc_get_ringparam,
  1340. .set_ringparam = netvsc_set_ringparam,
  1341. };
  1342. static const struct net_device_ops device_ops = {
  1343. .ndo_open = netvsc_open,
  1344. .ndo_stop = netvsc_close,
  1345. .ndo_start_xmit = netvsc_start_xmit,
  1346. .ndo_change_rx_flags = netvsc_change_rx_flags,
  1347. .ndo_set_rx_mode = netvsc_set_rx_mode,
  1348. .ndo_change_mtu = netvsc_change_mtu,
  1349. .ndo_validate_addr = eth_validate_addr,
  1350. .ndo_set_mac_address = netvsc_set_mac_addr,
  1351. .ndo_select_queue = netvsc_select_queue,
  1352. .ndo_get_stats64 = netvsc_get_stats64,
  1353. #ifdef CONFIG_NET_POLL_CONTROLLER
  1354. .ndo_poll_controller = netvsc_poll_controller,
  1355. #endif
  1356. };
  1357. /*
  1358. * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
  1359. * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
  1360. * present send GARP packet to network peers with netif_notify_peers().
  1361. */
  1362. static void netvsc_link_change(struct work_struct *w)
  1363. {
  1364. struct net_device_context *ndev_ctx =
  1365. container_of(w, struct net_device_context, dwork.work);
  1366. struct hv_device *device_obj = ndev_ctx->device_ctx;
  1367. struct net_device *net = hv_get_drvdata(device_obj);
  1368. struct netvsc_device *net_device;
  1369. struct rndis_device *rdev;
  1370. struct netvsc_reconfig *event = NULL;
  1371. bool notify = false, reschedule = false;
  1372. unsigned long flags, next_reconfig, delay;
  1373. /* if changes are happening, comeback later */
  1374. if (!rtnl_trylock()) {
  1375. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1376. return;
  1377. }
  1378. net_device = rtnl_dereference(ndev_ctx->nvdev);
  1379. if (!net_device)
  1380. goto out_unlock;
  1381. rdev = net_device->extension;
  1382. next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
  1383. if (time_is_after_jiffies(next_reconfig)) {
  1384. /* link_watch only sends one notification with current state
  1385. * per second, avoid doing reconfig more frequently. Handle
  1386. * wrap around.
  1387. */
  1388. delay = next_reconfig - jiffies;
  1389. delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
  1390. schedule_delayed_work(&ndev_ctx->dwork, delay);
  1391. goto out_unlock;
  1392. }
  1393. ndev_ctx->last_reconfig = jiffies;
  1394. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1395. if (!list_empty(&ndev_ctx->reconfig_events)) {
  1396. event = list_first_entry(&ndev_ctx->reconfig_events,
  1397. struct netvsc_reconfig, list);
  1398. list_del(&event->list);
  1399. reschedule = !list_empty(&ndev_ctx->reconfig_events);
  1400. }
  1401. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1402. if (!event)
  1403. goto out_unlock;
  1404. switch (event->event) {
  1405. /* Only the following events are possible due to the check in
  1406. * netvsc_linkstatus_callback()
  1407. */
  1408. case RNDIS_STATUS_MEDIA_CONNECT:
  1409. if (rdev->link_state) {
  1410. rdev->link_state = false;
  1411. netif_carrier_on(net);
  1412. netif_tx_wake_all_queues(net);
  1413. } else {
  1414. notify = true;
  1415. }
  1416. kfree(event);
  1417. break;
  1418. case RNDIS_STATUS_MEDIA_DISCONNECT:
  1419. if (!rdev->link_state) {
  1420. rdev->link_state = true;
  1421. netif_carrier_off(net);
  1422. netif_tx_stop_all_queues(net);
  1423. }
  1424. kfree(event);
  1425. break;
  1426. case RNDIS_STATUS_NETWORK_CHANGE:
  1427. /* Only makes sense if carrier is present */
  1428. if (!rdev->link_state) {
  1429. rdev->link_state = true;
  1430. netif_carrier_off(net);
  1431. netif_tx_stop_all_queues(net);
  1432. event->event = RNDIS_STATUS_MEDIA_CONNECT;
  1433. spin_lock_irqsave(&ndev_ctx->lock, flags);
  1434. list_add(&event->list, &ndev_ctx->reconfig_events);
  1435. spin_unlock_irqrestore(&ndev_ctx->lock, flags);
  1436. reschedule = true;
  1437. }
  1438. break;
  1439. }
  1440. rtnl_unlock();
  1441. if (notify)
  1442. netdev_notify_peers(net);
  1443. /* link_watch only sends one notification with current state per
  1444. * second, handle next reconfig event in 2 seconds.
  1445. */
  1446. if (reschedule)
  1447. schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
  1448. return;
  1449. out_unlock:
  1450. rtnl_unlock();
  1451. }
  1452. static struct net_device *get_netvsc_bymac(const u8 *mac)
  1453. {
  1454. struct net_device_context *ndev_ctx;
  1455. list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
  1456. struct net_device *dev = hv_get_drvdata(ndev_ctx->device_ctx);
  1457. if (ether_addr_equal(mac, dev->perm_addr))
  1458. return dev;
  1459. }
  1460. return NULL;
  1461. }
  1462. static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
  1463. {
  1464. struct net_device_context *net_device_ctx;
  1465. struct net_device *dev;
  1466. dev = netdev_master_upper_dev_get(vf_netdev);
  1467. if (!dev || dev->netdev_ops != &device_ops)
  1468. return NULL; /* not a netvsc device */
  1469. net_device_ctx = netdev_priv(dev);
  1470. if (!rtnl_dereference(net_device_ctx->nvdev))
  1471. return NULL; /* device is removed */
  1472. return dev;
  1473. }
  1474. /* Called when VF is injecting data into network stack.
  1475. * Change the associated network device from VF to netvsc.
  1476. * note: already called with rcu_read_lock
  1477. */
  1478. static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
  1479. {
  1480. struct sk_buff *skb = *pskb;
  1481. struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
  1482. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1483. struct netvsc_vf_pcpu_stats *pcpu_stats
  1484. = this_cpu_ptr(ndev_ctx->vf_stats);
  1485. skb->dev = ndev;
  1486. u64_stats_update_begin(&pcpu_stats->syncp);
  1487. pcpu_stats->rx_packets++;
  1488. pcpu_stats->rx_bytes += skb->len;
  1489. u64_stats_update_end(&pcpu_stats->syncp);
  1490. return RX_HANDLER_ANOTHER;
  1491. }
  1492. static int netvsc_vf_join(struct net_device *vf_netdev,
  1493. struct net_device *ndev)
  1494. {
  1495. struct net_device_context *ndev_ctx = netdev_priv(ndev);
  1496. int ret;
  1497. ret = netdev_rx_handler_register(vf_netdev,
  1498. netvsc_vf_handle_frame, ndev);
  1499. if (ret != 0) {
  1500. netdev_err(vf_netdev,
  1501. "can not register netvsc VF receive handler (err = %d)\n",
  1502. ret);
  1503. goto rx_handler_failed;
  1504. }
  1505. ret = netdev_master_upper_dev_link(vf_netdev, ndev,
  1506. NULL, NULL, NULL);
  1507. if (ret != 0) {
  1508. netdev_err(vf_netdev,
  1509. "can not set master device %s (err = %d)\n",
  1510. ndev->name, ret);
  1511. goto upper_link_failed;
  1512. }
  1513. /* set slave flag before open to prevent IPv6 addrconf */
  1514. vf_netdev->flags |= IFF_SLAVE;
  1515. schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
  1516. call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
  1517. netdev_info(vf_netdev, "joined to %s\n", ndev->name);
  1518. return 0;
  1519. upper_link_failed:
  1520. netdev_rx_handler_unregister(vf_netdev);
  1521. rx_handler_failed:
  1522. return ret;
  1523. }
  1524. static void __netvsc_vf_setup(struct net_device *ndev,
  1525. struct net_device *vf_netdev)
  1526. {
  1527. int ret;
  1528. /* Align MTU of VF with master */
  1529. ret = dev_set_mtu(vf_netdev, ndev->mtu);
  1530. if (ret)
  1531. netdev_warn(vf_netdev,
  1532. "unable to change mtu to %u\n", ndev->mtu);
  1533. /* set multicast etc flags on VF */
  1534. dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE);
  1535. /* sync address list from ndev to VF */
  1536. netif_addr_lock_bh(ndev);
  1537. dev_uc_sync(vf_netdev, ndev);
  1538. dev_mc_sync(vf_netdev, ndev);
  1539. netif_addr_unlock_bh(ndev);
  1540. if (netif_running(ndev)) {
  1541. ret = dev_open(vf_netdev);
  1542. if (ret)
  1543. netdev_warn(vf_netdev,
  1544. "unable to open: %d\n", ret);
  1545. }
  1546. }
  1547. /* Setup VF as slave of the synthetic device.
  1548. * Runs in workqueue to avoid recursion in netlink callbacks.
  1549. */
  1550. static void netvsc_vf_setup(struct work_struct *w)
  1551. {
  1552. struct net_device_context *ndev_ctx
  1553. = container_of(w, struct net_device_context, vf_takeover.work);
  1554. struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
  1555. struct net_device *vf_netdev;
  1556. if (!rtnl_trylock()) {
  1557. schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
  1558. return;
  1559. }
  1560. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1561. if (vf_netdev)
  1562. __netvsc_vf_setup(ndev, vf_netdev);
  1563. rtnl_unlock();
  1564. }
  1565. static int netvsc_register_vf(struct net_device *vf_netdev)
  1566. {
  1567. struct net_device *ndev;
  1568. struct net_device_context *net_device_ctx;
  1569. struct netvsc_device *netvsc_dev;
  1570. int ret;
  1571. if (vf_netdev->addr_len != ETH_ALEN)
  1572. return NOTIFY_DONE;
  1573. /*
  1574. * We will use the MAC address to locate the synthetic interface to
  1575. * associate with the VF interface. If we don't find a matching
  1576. * synthetic interface, move on.
  1577. */
  1578. ndev = get_netvsc_bymac(vf_netdev->perm_addr);
  1579. if (!ndev)
  1580. return NOTIFY_DONE;
  1581. net_device_ctx = netdev_priv(ndev);
  1582. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1583. if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
  1584. return NOTIFY_DONE;
  1585. /* if syntihetic interface is a different namespace,
  1586. * then move the VF to that namespace; join will be
  1587. * done again in that context.
  1588. */
  1589. if (!net_eq(dev_net(ndev), dev_net(vf_netdev))) {
  1590. ret = dev_change_net_namespace(vf_netdev,
  1591. dev_net(ndev), "eth%d");
  1592. if (ret)
  1593. netdev_err(vf_netdev,
  1594. "could not move to same namespace as %s: %d\n",
  1595. ndev->name, ret);
  1596. else
  1597. netdev_info(vf_netdev,
  1598. "VF moved to namespace with: %s\n",
  1599. ndev->name);
  1600. return NOTIFY_DONE;
  1601. }
  1602. netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
  1603. if (netvsc_vf_join(vf_netdev, ndev) != 0)
  1604. return NOTIFY_DONE;
  1605. dev_hold(vf_netdev);
  1606. rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
  1607. return NOTIFY_OK;
  1608. }
  1609. /* VF up/down change detected, schedule to change data path */
  1610. static int netvsc_vf_changed(struct net_device *vf_netdev)
  1611. {
  1612. struct net_device_context *net_device_ctx;
  1613. struct netvsc_device *netvsc_dev;
  1614. struct net_device *ndev;
  1615. bool vf_is_up = netif_running(vf_netdev);
  1616. ndev = get_netvsc_byref(vf_netdev);
  1617. if (!ndev)
  1618. return NOTIFY_DONE;
  1619. net_device_ctx = netdev_priv(ndev);
  1620. netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
  1621. if (!netvsc_dev)
  1622. return NOTIFY_DONE;
  1623. netvsc_switch_datapath(ndev, vf_is_up);
  1624. netdev_info(ndev, "Data path switched %s VF: %s\n",
  1625. vf_is_up ? "to" : "from", vf_netdev->name);
  1626. return NOTIFY_OK;
  1627. }
  1628. static int netvsc_unregister_vf(struct net_device *vf_netdev)
  1629. {
  1630. struct net_device *ndev;
  1631. struct net_device_context *net_device_ctx;
  1632. ndev = get_netvsc_byref(vf_netdev);
  1633. if (!ndev)
  1634. return NOTIFY_DONE;
  1635. net_device_ctx = netdev_priv(ndev);
  1636. cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
  1637. netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
  1638. netdev_rx_handler_unregister(vf_netdev);
  1639. netdev_upper_dev_unlink(vf_netdev, ndev);
  1640. RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
  1641. dev_put(vf_netdev);
  1642. return NOTIFY_OK;
  1643. }
  1644. static int netvsc_probe(struct hv_device *dev,
  1645. const struct hv_vmbus_device_id *dev_id)
  1646. {
  1647. struct net_device *net = NULL;
  1648. struct net_device_context *net_device_ctx;
  1649. struct netvsc_device_info device_info;
  1650. struct netvsc_device *nvdev;
  1651. int ret = -ENOMEM;
  1652. net = alloc_etherdev_mq(sizeof(struct net_device_context),
  1653. VRSS_CHANNEL_MAX);
  1654. if (!net)
  1655. goto no_net;
  1656. netif_carrier_off(net);
  1657. netvsc_init_settings(net);
  1658. net_device_ctx = netdev_priv(net);
  1659. net_device_ctx->device_ctx = dev;
  1660. net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
  1661. if (netif_msg_probe(net_device_ctx))
  1662. netdev_dbg(net, "netvsc msg_enable: %d\n",
  1663. net_device_ctx->msg_enable);
  1664. hv_set_drvdata(dev, net);
  1665. INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
  1666. spin_lock_init(&net_device_ctx->lock);
  1667. INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
  1668. INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
  1669. net_device_ctx->vf_stats
  1670. = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
  1671. if (!net_device_ctx->vf_stats)
  1672. goto no_stats;
  1673. net->netdev_ops = &device_ops;
  1674. net->ethtool_ops = &ethtool_ops;
  1675. SET_NETDEV_DEV(net, &dev->device);
  1676. /* We always need headroom for rndis header */
  1677. net->needed_headroom = RNDIS_AND_PPI_SIZE;
  1678. /* Initialize the number of queues to be 1, we may change it if more
  1679. * channels are offered later.
  1680. */
  1681. netif_set_real_num_tx_queues(net, 1);
  1682. netif_set_real_num_rx_queues(net, 1);
  1683. /* Notify the netvsc driver of the new device */
  1684. memset(&device_info, 0, sizeof(device_info));
  1685. device_info.num_chn = VRSS_CHANNEL_DEFAULT;
  1686. device_info.send_sections = NETVSC_DEFAULT_TX;
  1687. device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
  1688. device_info.recv_sections = NETVSC_DEFAULT_RX;
  1689. device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
  1690. nvdev = rndis_filter_device_add(dev, &device_info);
  1691. if (IS_ERR(nvdev)) {
  1692. ret = PTR_ERR(nvdev);
  1693. netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
  1694. goto rndis_failed;
  1695. }
  1696. memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
  1697. /* hw_features computed in rndis_netdev_set_hwcaps() */
  1698. net->features = net->hw_features |
  1699. NETIF_F_HIGHDMA | NETIF_F_SG |
  1700. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  1701. net->vlan_features = net->features;
  1702. netdev_lockdep_set_classes(net);
  1703. /* MTU range: 68 - 1500 or 65521 */
  1704. net->min_mtu = NETVSC_MTU_MIN;
  1705. if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
  1706. net->max_mtu = NETVSC_MTU - ETH_HLEN;
  1707. else
  1708. net->max_mtu = ETH_DATA_LEN;
  1709. rtnl_lock();
  1710. ret = register_netdevice(net);
  1711. if (ret != 0) {
  1712. pr_err("Unable to register netdev.\n");
  1713. goto register_failed;
  1714. }
  1715. list_add(&net_device_ctx->list, &netvsc_dev_list);
  1716. rtnl_unlock();
  1717. return 0;
  1718. register_failed:
  1719. rtnl_unlock();
  1720. rndis_filter_device_remove(dev, nvdev);
  1721. rndis_failed:
  1722. free_percpu(net_device_ctx->vf_stats);
  1723. no_stats:
  1724. hv_set_drvdata(dev, NULL);
  1725. free_netdev(net);
  1726. no_net:
  1727. return ret;
  1728. }
  1729. static int netvsc_remove(struct hv_device *dev)
  1730. {
  1731. struct net_device_context *ndev_ctx;
  1732. struct net_device *vf_netdev, *net;
  1733. struct netvsc_device *nvdev;
  1734. net = hv_get_drvdata(dev);
  1735. if (net == NULL) {
  1736. dev_err(&dev->device, "No net device to remove\n");
  1737. return 0;
  1738. }
  1739. ndev_ctx = netdev_priv(net);
  1740. cancel_delayed_work_sync(&ndev_ctx->dwork);
  1741. rcu_read_lock();
  1742. nvdev = rcu_dereference(ndev_ctx->nvdev);
  1743. if (nvdev)
  1744. cancel_work_sync(&nvdev->subchan_work);
  1745. /*
  1746. * Call to the vsc driver to let it know that the device is being
  1747. * removed. Also blocks mtu and channel changes.
  1748. */
  1749. rtnl_lock();
  1750. vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
  1751. if (vf_netdev)
  1752. netvsc_unregister_vf(vf_netdev);
  1753. if (nvdev)
  1754. rndis_filter_device_remove(dev, nvdev);
  1755. unregister_netdevice(net);
  1756. list_del(&ndev_ctx->list);
  1757. rtnl_unlock();
  1758. rcu_read_unlock();
  1759. hv_set_drvdata(dev, NULL);
  1760. free_percpu(ndev_ctx->vf_stats);
  1761. free_netdev(net);
  1762. return 0;
  1763. }
  1764. static const struct hv_vmbus_device_id id_table[] = {
  1765. /* Network guid */
  1766. { HV_NIC_GUID, },
  1767. { },
  1768. };
  1769. MODULE_DEVICE_TABLE(vmbus, id_table);
  1770. /* The one and only one */
  1771. static struct hv_driver netvsc_drv = {
  1772. .name = KBUILD_MODNAME,
  1773. .id_table = id_table,
  1774. .probe = netvsc_probe,
  1775. .remove = netvsc_remove,
  1776. };
  1777. /*
  1778. * On Hyper-V, every VF interface is matched with a corresponding
  1779. * synthetic interface. The synthetic interface is presented first
  1780. * to the guest. When the corresponding VF instance is registered,
  1781. * we will take care of switching the data path.
  1782. */
  1783. static int netvsc_netdev_event(struct notifier_block *this,
  1784. unsigned long event, void *ptr)
  1785. {
  1786. struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
  1787. /* Skip our own events */
  1788. if (event_dev->netdev_ops == &device_ops)
  1789. return NOTIFY_DONE;
  1790. /* Avoid non-Ethernet type devices */
  1791. if (event_dev->type != ARPHRD_ETHER)
  1792. return NOTIFY_DONE;
  1793. /* Avoid Vlan dev with same MAC registering as VF */
  1794. if (is_vlan_dev(event_dev))
  1795. return NOTIFY_DONE;
  1796. /* Avoid Bonding master dev with same MAC registering as VF */
  1797. if ((event_dev->priv_flags & IFF_BONDING) &&
  1798. (event_dev->flags & IFF_MASTER))
  1799. return NOTIFY_DONE;
  1800. switch (event) {
  1801. case NETDEV_REGISTER:
  1802. return netvsc_register_vf(event_dev);
  1803. case NETDEV_UNREGISTER:
  1804. return netvsc_unregister_vf(event_dev);
  1805. case NETDEV_UP:
  1806. case NETDEV_DOWN:
  1807. return netvsc_vf_changed(event_dev);
  1808. default:
  1809. return NOTIFY_DONE;
  1810. }
  1811. }
  1812. static struct notifier_block netvsc_netdev_notifier = {
  1813. .notifier_call = netvsc_netdev_event,
  1814. };
  1815. static void __exit netvsc_drv_exit(void)
  1816. {
  1817. unregister_netdevice_notifier(&netvsc_netdev_notifier);
  1818. vmbus_driver_unregister(&netvsc_drv);
  1819. }
  1820. static int __init netvsc_drv_init(void)
  1821. {
  1822. int ret;
  1823. if (ring_size < RING_SIZE_MIN) {
  1824. ring_size = RING_SIZE_MIN;
  1825. pr_info("Increased ring_size to %u (min allowed)\n",
  1826. ring_size);
  1827. }
  1828. netvsc_ring_bytes = ring_size * PAGE_SIZE;
  1829. ret = vmbus_driver_register(&netvsc_drv);
  1830. if (ret)
  1831. return ret;
  1832. register_netdevice_notifier(&netvsc_netdev_notifier);
  1833. return 0;
  1834. }
  1835. MODULE_LICENSE("GPL");
  1836. MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
  1837. module_init(netvsc_drv_init);
  1838. module_exit(netvsc_drv_exit);