veth.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/net/veth.c
  4. *
  5. * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
  6. *
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
  9. *
  10. */
  11. #include <linux/netdevice.h>
  12. #include <linux/slab.h>
  13. #include <linux/ethtool.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/u64_stats_sync.h>
  16. #include <net/rtnetlink.h>
  17. #include <net/dst.h>
  18. #include <net/netdev_lock.h>
  19. #include <net/xfrm.h>
  20. #include <net/xdp.h>
  21. #include <linux/veth.h>
  22. #include <linux/module.h>
  23. #include <linux/bpf.h>
  24. #include <linux/filter.h>
  25. #include <linux/ptr_ring.h>
  26. #include <linux/bpf_trace.h>
  27. #include <linux/net_tstamp.h>
  28. #include <linux/skbuff_ref.h>
  29. #include <net/page_pool/helpers.h>
  30. #define DRV_NAME "veth"
  31. #define DRV_VERSION "1.0"
  32. #define VETH_XDP_FLAG BIT(0)
  33. #define VETH_RING_SIZE 256
  34. #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
  35. #define VETH_XDP_TX_BULK_SIZE 16
  36. #define VETH_XDP_BATCH 16
  37. struct veth_stats {
  38. u64 rx_drops;
  39. /* xdp */
  40. u64 xdp_packets;
  41. u64 xdp_bytes;
  42. u64 xdp_redirect;
  43. u64 xdp_drops;
  44. u64 xdp_tx;
  45. u64 xdp_tx_err;
  46. u64 peer_tq_xdp_xmit;
  47. u64 peer_tq_xdp_xmit_err;
  48. };
  49. struct veth_rq_stats {
  50. struct veth_stats vs;
  51. struct u64_stats_sync syncp;
  52. };
  53. struct veth_rq {
  54. struct napi_struct xdp_napi;
  55. struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
  56. struct net_device *dev;
  57. struct bpf_prog __rcu *xdp_prog;
  58. struct xdp_mem_info xdp_mem;
  59. struct veth_rq_stats stats;
  60. bool rx_notify_masked;
  61. struct ptr_ring xdp_ring;
  62. struct xdp_rxq_info xdp_rxq;
  63. struct page_pool *page_pool;
  64. };
  65. struct veth_priv {
  66. struct net_device __rcu *peer;
  67. atomic64_t dropped;
  68. struct bpf_prog *_xdp_prog;
  69. struct veth_rq *rq;
  70. unsigned int requested_headroom;
  71. };
  72. struct veth_xdp_tx_bq {
  73. struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
  74. unsigned int count;
  75. };
  76. /*
  77. * ethtool interface
  78. */
  79. struct veth_q_stat_desc {
  80. char desc[ETH_GSTRING_LEN];
  81. size_t offset;
  82. };
  83. #define VETH_RQ_STAT(m) offsetof(struct veth_stats, m)
  84. static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
  85. { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
  86. { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
  87. { "drops", VETH_RQ_STAT(rx_drops) },
  88. { "xdp_redirect", VETH_RQ_STAT(xdp_redirect) },
  89. { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
  90. { "xdp_tx", VETH_RQ_STAT(xdp_tx) },
  91. { "xdp_tx_errors", VETH_RQ_STAT(xdp_tx_err) },
  92. };
  93. #define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
  94. static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
  95. { "xdp_xmit", VETH_RQ_STAT(peer_tq_xdp_xmit) },
  96. { "xdp_xmit_errors", VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
  97. };
  98. #define VETH_TQ_STATS_LEN ARRAY_SIZE(veth_tq_stats_desc)
  99. static struct {
  100. const char string[ETH_GSTRING_LEN];
  101. } ethtool_stats_keys[] = {
  102. { "peer_ifindex" },
  103. };
  104. struct veth_xdp_buff {
  105. struct xdp_buff xdp;
  106. struct sk_buff *skb;
  107. };
  108. static int veth_get_link_ksettings(struct net_device *dev,
  109. struct ethtool_link_ksettings *cmd)
  110. {
  111. cmd->base.speed = SPEED_10000;
  112. cmd->base.duplex = DUPLEX_FULL;
  113. cmd->base.port = PORT_TP;
  114. cmd->base.autoneg = AUTONEG_DISABLE;
  115. return 0;
  116. }
  117. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  118. {
  119. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  120. strscpy(info->version, DRV_VERSION, sizeof(info->version));
  121. }
  122. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  123. {
  124. u8 *p = buf;
  125. int i, j;
  126. switch(stringset) {
  127. case ETH_SS_STATS:
  128. memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  129. p += sizeof(ethtool_stats_keys);
  130. for (i = 0; i < dev->real_num_rx_queues; i++)
  131. for (j = 0; j < VETH_RQ_STATS_LEN; j++)
  132. ethtool_sprintf(&p, "rx_queue_%u_%.18s",
  133. i, veth_rq_stats_desc[j].desc);
  134. for (i = 0; i < dev->real_num_tx_queues; i++)
  135. for (j = 0; j < VETH_TQ_STATS_LEN; j++)
  136. ethtool_sprintf(&p, "tx_queue_%u_%.18s",
  137. i, veth_tq_stats_desc[j].desc);
  138. page_pool_ethtool_stats_get_strings(p);
  139. break;
  140. }
  141. }
  142. static int veth_get_sset_count(struct net_device *dev, int sset)
  143. {
  144. switch (sset) {
  145. case ETH_SS_STATS:
  146. return ARRAY_SIZE(ethtool_stats_keys) +
  147. VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
  148. VETH_TQ_STATS_LEN * dev->real_num_tx_queues +
  149. page_pool_ethtool_stats_get_count();
  150. default:
  151. return -EOPNOTSUPP;
  152. }
  153. }
  154. static void veth_get_page_pool_stats(struct net_device *dev, u64 *data)
  155. {
  156. #ifdef CONFIG_PAGE_POOL_STATS
  157. struct veth_priv *priv = netdev_priv(dev);
  158. struct page_pool_stats pp_stats = {};
  159. int i;
  160. for (i = 0; i < dev->real_num_rx_queues; i++) {
  161. if (!priv->rq[i].page_pool)
  162. continue;
  163. page_pool_get_stats(priv->rq[i].page_pool, &pp_stats);
  164. }
  165. page_pool_ethtool_stats_get(data, &pp_stats);
  166. #endif /* CONFIG_PAGE_POOL_STATS */
  167. }
  168. static void veth_get_ethtool_stats(struct net_device *dev,
  169. struct ethtool_stats *stats, u64 *data)
  170. {
  171. struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
  172. struct net_device *peer = rtnl_dereference(priv->peer);
  173. int i, j, idx, pp_idx;
  174. data[0] = peer ? peer->ifindex : 0;
  175. idx = 1;
  176. for (i = 0; i < dev->real_num_rx_queues; i++) {
  177. const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
  178. const void *stats_base = (void *)&rq_stats->vs;
  179. unsigned int start;
  180. size_t offset;
  181. do {
  182. start = u64_stats_fetch_begin(&rq_stats->syncp);
  183. for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
  184. offset = veth_rq_stats_desc[j].offset;
  185. data[idx + j] = *(u64 *)(stats_base + offset);
  186. }
  187. } while (u64_stats_fetch_retry(&rq_stats->syncp, start));
  188. idx += VETH_RQ_STATS_LEN;
  189. }
  190. pp_idx = idx;
  191. if (!peer)
  192. goto page_pool_stats;
  193. rcv_priv = netdev_priv(peer);
  194. for (i = 0; i < peer->real_num_rx_queues; i++) {
  195. const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
  196. const void *base = (void *)&rq_stats->vs;
  197. unsigned int start, tx_idx = idx;
  198. u64 buf[VETH_TQ_STATS_LEN];
  199. size_t offset;
  200. do {
  201. start = u64_stats_fetch_begin(&rq_stats->syncp);
  202. for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
  203. offset = veth_tq_stats_desc[j].offset;
  204. buf[j] = *(u64 *)(base + offset);
  205. }
  206. } while (u64_stats_fetch_retry(&rq_stats->syncp, start));
  207. tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
  208. for (j = 0; j < VETH_TQ_STATS_LEN; j++)
  209. data[tx_idx + j] += buf[j];
  210. }
  211. pp_idx = idx + dev->real_num_tx_queues * VETH_TQ_STATS_LEN;
  212. page_pool_stats:
  213. veth_get_page_pool_stats(dev, &data[pp_idx]);
  214. }
  215. static void veth_get_channels(struct net_device *dev,
  216. struct ethtool_channels *channels)
  217. {
  218. channels->tx_count = dev->real_num_tx_queues;
  219. channels->rx_count = dev->real_num_rx_queues;
  220. channels->max_tx = dev->num_tx_queues;
  221. channels->max_rx = dev->num_rx_queues;
  222. }
  223. static int veth_set_channels(struct net_device *dev,
  224. struct ethtool_channels *ch);
  225. static const struct ethtool_ops veth_ethtool_ops = {
  226. .get_drvinfo = veth_get_drvinfo,
  227. .get_link = ethtool_op_get_link,
  228. .get_strings = veth_get_strings,
  229. .get_sset_count = veth_get_sset_count,
  230. .get_ethtool_stats = veth_get_ethtool_stats,
  231. .get_link_ksettings = veth_get_link_ksettings,
  232. .get_ts_info = ethtool_op_get_ts_info,
  233. .get_channels = veth_get_channels,
  234. .set_channels = veth_set_channels,
  235. };
  236. /* general routines */
  237. static bool veth_is_xdp_frame(void *ptr)
  238. {
  239. return (unsigned long)ptr & VETH_XDP_FLAG;
  240. }
  241. static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
  242. {
  243. return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
  244. }
  245. static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
  246. {
  247. return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
  248. }
  249. static void veth_ptr_free(void *ptr)
  250. {
  251. if (veth_is_xdp_frame(ptr))
  252. xdp_return_frame(veth_ptr_to_xdp(ptr));
  253. else
  254. kfree_skb(ptr);
  255. }
  256. static void __veth_xdp_flush(struct veth_rq *rq)
  257. {
  258. /* Write ptr_ring before reading rx_notify_masked */
  259. smp_mb();
  260. if (!READ_ONCE(rq->rx_notify_masked) &&
  261. napi_schedule_prep(&rq->xdp_napi)) {
  262. WRITE_ONCE(rq->rx_notify_masked, true);
  263. __napi_schedule(&rq->xdp_napi);
  264. }
  265. }
  266. static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
  267. {
  268. if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb)))
  269. return NETDEV_TX_BUSY; /* signal qdisc layer */
  270. return NET_RX_SUCCESS; /* same as NETDEV_TX_OK */
  271. }
  272. static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
  273. struct veth_rq *rq, bool xdp)
  274. {
  275. return __dev_forward_skb(dev, skb) ?: xdp ?
  276. veth_xdp_rx(rq, skb) :
  277. __netif_rx(skb);
  278. }
  279. /* return true if the specified skb has chances of GRO aggregation
  280. * Don't strive for accuracy, but try to avoid GRO overhead in the most
  281. * common scenarios.
  282. * When XDP is enabled, all traffic is considered eligible, as the xmit
  283. * device has TSO off.
  284. * When TSO is enabled on the xmit device, we are likely interested only
  285. * in UDP aggregation, explicitly check for that if the skb is suspected
  286. * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
  287. * to belong to locally generated UDP traffic.
  288. */
  289. static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
  290. const struct net_device *rcv,
  291. const struct sk_buff *skb)
  292. {
  293. return !(dev->features & NETIF_F_ALL_TSO) ||
  294. (skb->destructor == sock_wfree &&
  295. rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
  296. }
  297. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  298. {
  299. struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
  300. struct veth_rq *rq = NULL;
  301. struct netdev_queue *txq;
  302. struct net_device *rcv;
  303. int length = skb->len;
  304. bool use_napi = false;
  305. int ret, rxq;
  306. rcu_read_lock();
  307. rcv = rcu_dereference(priv->peer);
  308. if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
  309. kfree_skb(skb);
  310. goto drop;
  311. }
  312. rcv_priv = netdev_priv(rcv);
  313. rxq = skb_get_queue_mapping(skb);
  314. if (rxq < rcv->real_num_rx_queues) {
  315. rq = &rcv_priv->rq[rxq];
  316. /* The napi pointer is available when an XDP program is
  317. * attached or when GRO is enabled
  318. * Don't bother with napi/GRO if the skb can't be aggregated
  319. */
  320. use_napi = rcu_access_pointer(rq->napi) &&
  321. veth_skb_is_eligible_for_gro(dev, rcv, skb);
  322. }
  323. skb_tx_timestamp(skb);
  324. ret = veth_forward_skb(rcv, skb, rq, use_napi);
  325. switch (ret) {
  326. case NET_RX_SUCCESS: /* same as NETDEV_TX_OK */
  327. if (!use_napi)
  328. dev_sw_netstats_tx_add(dev, 1, length);
  329. else
  330. __veth_xdp_flush(rq);
  331. break;
  332. case NETDEV_TX_BUSY:
  333. /* If a qdisc is attached to our virtual device, returning
  334. * NETDEV_TX_BUSY is allowed.
  335. */
  336. txq = netdev_get_tx_queue(dev, rxq);
  337. if (qdisc_txq_has_no_queue(txq)) {
  338. dev_kfree_skb_any(skb);
  339. goto drop;
  340. }
  341. /* Restore Eth hdr pulled by dev_forward_skb/eth_type_trans */
  342. __skb_push(skb, ETH_HLEN);
  343. netif_tx_stop_queue(txq);
  344. /* Makes sure NAPI peer consumer runs. Consumer is responsible
  345. * for starting txq again, until then ndo_start_xmit (this
  346. * function) will not be invoked by the netstack again.
  347. */
  348. __veth_xdp_flush(rq);
  349. break;
  350. case NET_RX_DROP: /* same as NET_XMIT_DROP */
  351. drop:
  352. atomic64_inc(&priv->dropped);
  353. ret = NET_XMIT_DROP;
  354. break;
  355. default:
  356. net_crit_ratelimited("%s(%s): Invalid return code(%d)",
  357. __func__, dev->name, ret);
  358. }
  359. rcu_read_unlock();
  360. return ret;
  361. }
  362. static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
  363. {
  364. struct veth_priv *priv = netdev_priv(dev);
  365. int i;
  366. result->peer_tq_xdp_xmit_err = 0;
  367. result->xdp_packets = 0;
  368. result->xdp_tx_err = 0;
  369. result->xdp_bytes = 0;
  370. result->rx_drops = 0;
  371. for (i = 0; i < dev->num_rx_queues; i++) {
  372. u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
  373. struct veth_rq_stats *stats = &priv->rq[i].stats;
  374. unsigned int start;
  375. do {
  376. start = u64_stats_fetch_begin(&stats->syncp);
  377. peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
  378. xdp_tx_err = stats->vs.xdp_tx_err;
  379. packets = stats->vs.xdp_packets;
  380. bytes = stats->vs.xdp_bytes;
  381. drops = stats->vs.rx_drops;
  382. } while (u64_stats_fetch_retry(&stats->syncp, start));
  383. result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
  384. result->xdp_tx_err += xdp_tx_err;
  385. result->xdp_packets += packets;
  386. result->xdp_bytes += bytes;
  387. result->rx_drops += drops;
  388. }
  389. }
  390. static void veth_get_stats64(struct net_device *dev,
  391. struct rtnl_link_stats64 *tot)
  392. {
  393. struct veth_priv *priv = netdev_priv(dev);
  394. struct net_device *peer;
  395. struct veth_stats rx;
  396. tot->tx_dropped = atomic64_read(&priv->dropped);
  397. dev_fetch_sw_netstats(tot, dev->tstats);
  398. veth_stats_rx(&rx, dev);
  399. tot->tx_dropped += rx.xdp_tx_err;
  400. tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
  401. tot->rx_bytes += rx.xdp_bytes;
  402. tot->rx_packets += rx.xdp_packets;
  403. rcu_read_lock();
  404. peer = rcu_dereference(priv->peer);
  405. if (peer) {
  406. struct rtnl_link_stats64 tot_peer = {};
  407. dev_fetch_sw_netstats(&tot_peer, peer->tstats);
  408. tot->rx_bytes += tot_peer.tx_bytes;
  409. tot->rx_packets += tot_peer.tx_packets;
  410. veth_stats_rx(&rx, peer);
  411. tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
  412. tot->rx_dropped += rx.xdp_tx_err;
  413. tot->tx_bytes += rx.xdp_bytes;
  414. tot->tx_packets += rx.xdp_packets;
  415. }
  416. rcu_read_unlock();
  417. }
  418. /* fake multicast ability */
  419. static void veth_set_multicast_list(struct net_device *dev)
  420. {
  421. }
  422. static int veth_select_rxq(struct net_device *dev)
  423. {
  424. return smp_processor_id() % dev->real_num_rx_queues;
  425. }
  426. static struct net_device *veth_peer_dev(struct net_device *dev)
  427. {
  428. struct veth_priv *priv = netdev_priv(dev);
  429. /* Callers must be under RCU read side. */
  430. return rcu_dereference(priv->peer);
  431. }
  432. static int veth_xdp_xmit(struct net_device *dev, int n,
  433. struct xdp_frame **frames,
  434. u32 flags, bool ndo_xmit)
  435. {
  436. struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
  437. int i, ret = -ENXIO, nxmit = 0;
  438. struct net_device *rcv;
  439. unsigned int max_len;
  440. struct veth_rq *rq;
  441. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  442. return -EINVAL;
  443. rcu_read_lock();
  444. rcv = rcu_dereference(priv->peer);
  445. if (unlikely(!rcv))
  446. goto out;
  447. rcv_priv = netdev_priv(rcv);
  448. rq = &rcv_priv->rq[veth_select_rxq(rcv)];
  449. /* The napi pointer is set if NAPI is enabled, which ensures that
  450. * xdp_ring is initialized on receive side and the peer device is up.
  451. */
  452. if (!rcu_access_pointer(rq->napi))
  453. goto out;
  454. max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
  455. spin_lock(&rq->xdp_ring.producer_lock);
  456. for (i = 0; i < n; i++) {
  457. struct xdp_frame *frame = frames[i];
  458. void *ptr = veth_xdp_to_ptr(frame);
  459. if (unlikely(xdp_get_frame_len(frame) > max_len ||
  460. __ptr_ring_produce(&rq->xdp_ring, ptr)))
  461. break;
  462. nxmit++;
  463. }
  464. spin_unlock(&rq->xdp_ring.producer_lock);
  465. if (flags & XDP_XMIT_FLUSH)
  466. __veth_xdp_flush(rq);
  467. ret = nxmit;
  468. if (ndo_xmit) {
  469. u64_stats_update_begin(&rq->stats.syncp);
  470. rq->stats.vs.peer_tq_xdp_xmit += nxmit;
  471. rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
  472. u64_stats_update_end(&rq->stats.syncp);
  473. }
  474. out:
  475. rcu_read_unlock();
  476. return ret;
  477. }
  478. static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
  479. struct xdp_frame **frames, u32 flags)
  480. {
  481. int err;
  482. err = veth_xdp_xmit(dev, n, frames, flags, true);
  483. if (err < 0) {
  484. struct veth_priv *priv = netdev_priv(dev);
  485. atomic64_add(n, &priv->dropped);
  486. }
  487. return err;
  488. }
  489. static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
  490. {
  491. int sent, i, err = 0, drops;
  492. sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
  493. if (sent < 0) {
  494. err = sent;
  495. sent = 0;
  496. }
  497. for (i = sent; unlikely(i < bq->count); i++)
  498. xdp_return_frame(bq->q[i]);
  499. drops = bq->count - sent;
  500. trace_xdp_bulk_tx(rq->dev, sent, drops, err);
  501. u64_stats_update_begin(&rq->stats.syncp);
  502. rq->stats.vs.xdp_tx += sent;
  503. rq->stats.vs.xdp_tx_err += drops;
  504. u64_stats_update_end(&rq->stats.syncp);
  505. bq->count = 0;
  506. }
  507. static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
  508. {
  509. struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
  510. struct net_device *rcv;
  511. struct veth_rq *rcv_rq;
  512. rcu_read_lock();
  513. veth_xdp_flush_bq(rq, bq);
  514. rcv = rcu_dereference(priv->peer);
  515. if (unlikely(!rcv))
  516. goto out;
  517. rcv_priv = netdev_priv(rcv);
  518. rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
  519. /* xdp_ring is initialized on receive side? */
  520. if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
  521. goto out;
  522. __veth_xdp_flush(rcv_rq);
  523. out:
  524. rcu_read_unlock();
  525. }
  526. static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
  527. struct veth_xdp_tx_bq *bq)
  528. {
  529. struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
  530. if (unlikely(!frame))
  531. return -EOVERFLOW;
  532. if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
  533. veth_xdp_flush_bq(rq, bq);
  534. bq->q[bq->count++] = frame;
  535. return 0;
  536. }
  537. static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
  538. struct xdp_frame *frame,
  539. struct veth_xdp_tx_bq *bq,
  540. struct veth_stats *stats)
  541. {
  542. struct xdp_frame orig_frame;
  543. struct bpf_prog *xdp_prog;
  544. rcu_read_lock();
  545. xdp_prog = rcu_dereference(rq->xdp_prog);
  546. if (likely(xdp_prog)) {
  547. struct veth_xdp_buff vxbuf;
  548. struct xdp_buff *xdp = &vxbuf.xdp;
  549. u32 act;
  550. xdp_convert_frame_to_buff(frame, xdp);
  551. xdp->rxq = &rq->xdp_rxq;
  552. vxbuf.skb = NULL;
  553. act = bpf_prog_run_xdp(xdp_prog, xdp);
  554. switch (act) {
  555. case XDP_PASS:
  556. if (xdp_update_frame_from_buff(xdp, frame))
  557. goto err_xdp;
  558. break;
  559. case XDP_TX:
  560. orig_frame = *frame;
  561. xdp->rxq->mem.type = frame->mem_type;
  562. if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
  563. trace_xdp_exception(rq->dev, xdp_prog, act);
  564. frame = &orig_frame;
  565. stats->rx_drops++;
  566. goto err_xdp;
  567. }
  568. stats->xdp_tx++;
  569. rcu_read_unlock();
  570. goto xdp_xmit;
  571. case XDP_REDIRECT:
  572. orig_frame = *frame;
  573. xdp->rxq->mem.type = frame->mem_type;
  574. if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
  575. frame = &orig_frame;
  576. stats->rx_drops++;
  577. goto err_xdp;
  578. }
  579. stats->xdp_redirect++;
  580. rcu_read_unlock();
  581. goto xdp_xmit;
  582. default:
  583. bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
  584. fallthrough;
  585. case XDP_ABORTED:
  586. trace_xdp_exception(rq->dev, xdp_prog, act);
  587. fallthrough;
  588. case XDP_DROP:
  589. stats->xdp_drops++;
  590. goto err_xdp;
  591. }
  592. }
  593. rcu_read_unlock();
  594. return frame;
  595. err_xdp:
  596. rcu_read_unlock();
  597. xdp_return_frame(frame);
  598. xdp_xmit:
  599. return NULL;
  600. }
  601. /* frames array contains VETH_XDP_BATCH at most */
  602. static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
  603. int n_xdpf, struct veth_xdp_tx_bq *bq,
  604. struct veth_stats *stats)
  605. {
  606. void *skbs[VETH_XDP_BATCH];
  607. int i;
  608. if (unlikely(!napi_skb_cache_get_bulk(skbs, n_xdpf))) {
  609. for (i = 0; i < n_xdpf; i++)
  610. xdp_return_frame(frames[i]);
  611. stats->rx_drops += n_xdpf;
  612. return;
  613. }
  614. for (i = 0; i < n_xdpf; i++) {
  615. struct sk_buff *skb = skbs[i];
  616. skb = __xdp_build_skb_from_frame(frames[i], skb,
  617. rq->dev);
  618. if (!skb) {
  619. xdp_return_frame(frames[i]);
  620. stats->rx_drops++;
  621. continue;
  622. }
  623. napi_gro_receive(&rq->xdp_napi, skb);
  624. }
  625. }
  626. static void veth_xdp_get(struct xdp_buff *xdp)
  627. {
  628. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  629. int i;
  630. get_page(virt_to_page(xdp->data));
  631. if (likely(!xdp_buff_has_frags(xdp)))
  632. return;
  633. for (i = 0; i < sinfo->nr_frags; i++)
  634. __skb_frag_ref(&sinfo->frags[i]);
  635. }
  636. static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
  637. struct xdp_buff *xdp,
  638. struct sk_buff **pskb)
  639. {
  640. struct sk_buff *skb = *pskb;
  641. u32 frame_sz;
  642. if (skb_shared(skb) || skb_head_is_locked(skb) ||
  643. skb_shinfo(skb)->nr_frags ||
  644. skb_headroom(skb) < XDP_PACKET_HEADROOM) {
  645. if (skb_pp_cow_data(rq->page_pool, pskb, XDP_PACKET_HEADROOM))
  646. goto drop;
  647. skb = *pskb;
  648. }
  649. /* SKB "head" area always have tailroom for skb_shared_info */
  650. frame_sz = skb_end_pointer(skb) - skb->head;
  651. frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  652. xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
  653. xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
  654. skb_headlen(skb), true);
  655. if (skb_is_nonlinear(skb)) {
  656. skb_shinfo(skb)->xdp_frags_size = skb->data_len;
  657. xdp_buff_set_frags_flag(xdp);
  658. } else {
  659. xdp_buff_clear_frags_flag(xdp);
  660. }
  661. *pskb = skb;
  662. return 0;
  663. drop:
  664. consume_skb(skb);
  665. *pskb = NULL;
  666. return -ENOMEM;
  667. }
  668. static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
  669. struct sk_buff *skb,
  670. struct veth_xdp_tx_bq *bq,
  671. struct veth_stats *stats)
  672. {
  673. void *orig_data, *orig_data_end;
  674. struct bpf_prog *xdp_prog;
  675. struct veth_xdp_buff vxbuf;
  676. struct xdp_buff *xdp = &vxbuf.xdp;
  677. u32 act, metalen;
  678. int off;
  679. skb_prepare_for_gro(skb);
  680. rcu_read_lock();
  681. xdp_prog = rcu_dereference(rq->xdp_prog);
  682. if (unlikely(!xdp_prog)) {
  683. rcu_read_unlock();
  684. goto out;
  685. }
  686. __skb_push(skb, skb->data - skb_mac_header(skb));
  687. if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
  688. goto drop;
  689. vxbuf.skb = skb;
  690. orig_data = xdp->data;
  691. orig_data_end = xdp->data_end;
  692. act = bpf_prog_run_xdp(xdp_prog, xdp);
  693. switch (act) {
  694. case XDP_PASS:
  695. break;
  696. case XDP_TX:
  697. veth_xdp_get(xdp);
  698. consume_skb(skb);
  699. xdp->rxq->mem = rq->xdp_mem;
  700. if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
  701. trace_xdp_exception(rq->dev, xdp_prog, act);
  702. stats->rx_drops++;
  703. goto err_xdp;
  704. }
  705. stats->xdp_tx++;
  706. rcu_read_unlock();
  707. goto xdp_xmit;
  708. case XDP_REDIRECT:
  709. veth_xdp_get(xdp);
  710. consume_skb(skb);
  711. xdp->rxq->mem = rq->xdp_mem;
  712. if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
  713. stats->rx_drops++;
  714. goto err_xdp;
  715. }
  716. stats->xdp_redirect++;
  717. rcu_read_unlock();
  718. goto xdp_xmit;
  719. default:
  720. bpf_warn_invalid_xdp_action(rq->dev, xdp_prog, act);
  721. fallthrough;
  722. case XDP_ABORTED:
  723. trace_xdp_exception(rq->dev, xdp_prog, act);
  724. fallthrough;
  725. case XDP_DROP:
  726. stats->xdp_drops++;
  727. goto xdp_drop;
  728. }
  729. rcu_read_unlock();
  730. /* check if bpf_xdp_adjust_head was used */
  731. off = orig_data - xdp->data;
  732. if (off > 0)
  733. __skb_push(skb, off);
  734. else if (off < 0)
  735. __skb_pull(skb, -off);
  736. skb_reset_mac_header(skb);
  737. /* check if bpf_xdp_adjust_tail was used */
  738. off = xdp->data_end - orig_data_end;
  739. if (off != 0)
  740. __skb_put(skb, off); /* positive on grow, negative on shrink */
  741. /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
  742. * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
  743. */
  744. if (xdp_buff_has_frags(xdp))
  745. skb->data_len = skb_shinfo(skb)->xdp_frags_size;
  746. else
  747. skb->data_len = 0;
  748. skb->protocol = eth_type_trans(skb, rq->dev);
  749. metalen = xdp->data - xdp->data_meta;
  750. if (metalen)
  751. skb_metadata_set(skb, metalen);
  752. out:
  753. return skb;
  754. drop:
  755. stats->rx_drops++;
  756. xdp_drop:
  757. rcu_read_unlock();
  758. kfree_skb(skb);
  759. return NULL;
  760. err_xdp:
  761. rcu_read_unlock();
  762. xdp_return_buff(xdp);
  763. xdp_xmit:
  764. return NULL;
  765. }
  766. static int veth_xdp_rcv(struct veth_rq *rq, int budget,
  767. struct veth_xdp_tx_bq *bq,
  768. struct veth_stats *stats)
  769. {
  770. int i, done = 0, n_xdpf = 0;
  771. void *xdpf[VETH_XDP_BATCH];
  772. for (i = 0; i < budget; i++) {
  773. void *ptr = __ptr_ring_consume(&rq->xdp_ring);
  774. if (!ptr)
  775. break;
  776. if (veth_is_xdp_frame(ptr)) {
  777. /* ndo_xdp_xmit */
  778. struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
  779. stats->xdp_bytes += xdp_get_frame_len(frame);
  780. frame = veth_xdp_rcv_one(rq, frame, bq, stats);
  781. if (frame) {
  782. /* XDP_PASS */
  783. xdpf[n_xdpf++] = frame;
  784. if (n_xdpf == VETH_XDP_BATCH) {
  785. veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
  786. bq, stats);
  787. n_xdpf = 0;
  788. }
  789. }
  790. } else {
  791. /* ndo_start_xmit */
  792. struct sk_buff *skb = ptr;
  793. stats->xdp_bytes += skb->len;
  794. skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
  795. if (skb) {
  796. if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
  797. netif_receive_skb(skb);
  798. else
  799. napi_gro_receive(&rq->xdp_napi, skb);
  800. }
  801. }
  802. done++;
  803. }
  804. if (n_xdpf)
  805. veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
  806. u64_stats_update_begin(&rq->stats.syncp);
  807. rq->stats.vs.xdp_redirect += stats->xdp_redirect;
  808. rq->stats.vs.xdp_bytes += stats->xdp_bytes;
  809. rq->stats.vs.xdp_drops += stats->xdp_drops;
  810. rq->stats.vs.rx_drops += stats->rx_drops;
  811. rq->stats.vs.xdp_packets += done;
  812. u64_stats_update_end(&rq->stats.syncp);
  813. return done;
  814. }
  815. static int veth_poll(struct napi_struct *napi, int budget)
  816. {
  817. struct veth_rq *rq =
  818. container_of(napi, struct veth_rq, xdp_napi);
  819. struct veth_priv *priv = netdev_priv(rq->dev);
  820. int queue_idx = rq->xdp_rxq.queue_index;
  821. struct netdev_queue *peer_txq;
  822. struct veth_stats stats = {};
  823. struct net_device *peer_dev;
  824. struct veth_xdp_tx_bq bq;
  825. int done;
  826. bq.count = 0;
  827. /* NAPI functions as RCU section */
  828. peer_dev = rcu_dereference_check(priv->peer, rcu_read_lock_bh_held());
  829. peer_txq = peer_dev ? netdev_get_tx_queue(peer_dev, queue_idx) : NULL;
  830. xdp_set_return_frame_no_direct();
  831. done = veth_xdp_rcv(rq, budget, &bq, &stats);
  832. if (stats.xdp_redirect > 0)
  833. xdp_do_flush();
  834. if (stats.xdp_tx > 0)
  835. veth_xdp_flush(rq, &bq);
  836. xdp_clear_return_frame_no_direct();
  837. if (done < budget && napi_complete_done(napi, done)) {
  838. /* Write rx_notify_masked before reading ptr_ring */
  839. smp_store_mb(rq->rx_notify_masked, false);
  840. if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
  841. if (napi_schedule_prep(&rq->xdp_napi)) {
  842. WRITE_ONCE(rq->rx_notify_masked, true);
  843. __napi_schedule(&rq->xdp_napi);
  844. }
  845. }
  846. }
  847. /* Release backpressure per NAPI poll */
  848. smp_rmb(); /* Paired with netif_tx_stop_queue set_bit */
  849. if (peer_txq && netif_tx_queue_stopped(peer_txq)) {
  850. txq_trans_cond_update(peer_txq);
  851. netif_tx_wake_queue(peer_txq);
  852. }
  853. return done;
  854. }
  855. static int veth_create_page_pool(struct veth_rq *rq)
  856. {
  857. struct page_pool_params pp_params = {
  858. .order = 0,
  859. .pool_size = VETH_RING_SIZE,
  860. .nid = NUMA_NO_NODE,
  861. .dev = &rq->dev->dev,
  862. };
  863. rq->page_pool = page_pool_create(&pp_params);
  864. if (IS_ERR(rq->page_pool)) {
  865. int err = PTR_ERR(rq->page_pool);
  866. rq->page_pool = NULL;
  867. return err;
  868. }
  869. return 0;
  870. }
  871. static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
  872. {
  873. struct veth_priv *priv = netdev_priv(dev);
  874. int err, i;
  875. for (i = start; i < end; i++) {
  876. err = veth_create_page_pool(&priv->rq[i]);
  877. if (err)
  878. goto err_page_pool;
  879. }
  880. for (i = start; i < end; i++) {
  881. struct veth_rq *rq = &priv->rq[i];
  882. err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
  883. if (err)
  884. goto err_xdp_ring;
  885. }
  886. for (i = start; i < end; i++) {
  887. struct veth_rq *rq = &priv->rq[i];
  888. napi_enable(&rq->xdp_napi);
  889. rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
  890. }
  891. return 0;
  892. err_xdp_ring:
  893. for (i--; i >= start; i--)
  894. ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
  895. i = end;
  896. err_page_pool:
  897. for (i--; i >= start; i--) {
  898. page_pool_destroy(priv->rq[i].page_pool);
  899. priv->rq[i].page_pool = NULL;
  900. }
  901. return err;
  902. }
  903. static int __veth_napi_enable(struct net_device *dev)
  904. {
  905. return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
  906. }
  907. static void veth_napi_del_range(struct net_device *dev, int start, int end)
  908. {
  909. struct veth_priv *priv = netdev_priv(dev);
  910. int i;
  911. for (i = start; i < end; i++) {
  912. struct veth_rq *rq = &priv->rq[i];
  913. rcu_assign_pointer(priv->rq[i].napi, NULL);
  914. napi_disable(&rq->xdp_napi);
  915. __netif_napi_del(&rq->xdp_napi);
  916. }
  917. synchronize_net();
  918. for (i = start; i < end; i++) {
  919. struct veth_rq *rq = &priv->rq[i];
  920. rq->rx_notify_masked = false;
  921. ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
  922. }
  923. for (i = start; i < end; i++) {
  924. page_pool_destroy(priv->rq[i].page_pool);
  925. priv->rq[i].page_pool = NULL;
  926. }
  927. }
  928. static void veth_napi_del(struct net_device *dev)
  929. {
  930. veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
  931. }
  932. static bool veth_gro_requested(const struct net_device *dev)
  933. {
  934. return !!(dev->wanted_features & NETIF_F_GRO);
  935. }
  936. static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
  937. bool napi_already_on)
  938. {
  939. struct veth_priv *priv = netdev_priv(dev);
  940. int err, i;
  941. for (i = start; i < end; i++) {
  942. struct veth_rq *rq = &priv->rq[i];
  943. if (!napi_already_on)
  944. netif_napi_add(dev, &rq->xdp_napi, veth_poll);
  945. err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
  946. if (err < 0)
  947. goto err_rxq_reg;
  948. err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
  949. MEM_TYPE_PAGE_SHARED,
  950. NULL);
  951. if (err < 0)
  952. goto err_reg_mem;
  953. /* Save original mem info as it can be overwritten */
  954. rq->xdp_mem = rq->xdp_rxq.mem;
  955. }
  956. return 0;
  957. err_reg_mem:
  958. xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
  959. err_rxq_reg:
  960. for (i--; i >= start; i--) {
  961. struct veth_rq *rq = &priv->rq[i];
  962. xdp_rxq_info_unreg(&rq->xdp_rxq);
  963. if (!napi_already_on)
  964. netif_napi_del(&rq->xdp_napi);
  965. }
  966. return err;
  967. }
  968. static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
  969. bool delete_napi)
  970. {
  971. struct veth_priv *priv = netdev_priv(dev);
  972. int i;
  973. for (i = start; i < end; i++) {
  974. struct veth_rq *rq = &priv->rq[i];
  975. rq->xdp_rxq.mem = rq->xdp_mem;
  976. xdp_rxq_info_unreg(&rq->xdp_rxq);
  977. if (delete_napi)
  978. netif_napi_del(&rq->xdp_napi);
  979. }
  980. }
  981. static int veth_enable_xdp(struct net_device *dev)
  982. {
  983. bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
  984. struct veth_priv *priv = netdev_priv(dev);
  985. int err, i;
  986. if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
  987. err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
  988. if (err)
  989. return err;
  990. if (!napi_already_on) {
  991. err = __veth_napi_enable(dev);
  992. if (err) {
  993. veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
  994. return err;
  995. }
  996. }
  997. }
  998. for (i = 0; i < dev->real_num_rx_queues; i++) {
  999. rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
  1000. rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
  1001. }
  1002. return 0;
  1003. }
  1004. static void veth_disable_xdp(struct net_device *dev)
  1005. {
  1006. struct veth_priv *priv = netdev_priv(dev);
  1007. int i;
  1008. for (i = 0; i < dev->real_num_rx_queues; i++)
  1009. rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
  1010. if (!netif_running(dev) || !veth_gro_requested(dev))
  1011. veth_napi_del(dev);
  1012. veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
  1013. }
  1014. static int veth_napi_enable_range(struct net_device *dev, int start, int end)
  1015. {
  1016. struct veth_priv *priv = netdev_priv(dev);
  1017. int err, i;
  1018. for (i = start; i < end; i++) {
  1019. struct veth_rq *rq = &priv->rq[i];
  1020. netif_napi_add(dev, &rq->xdp_napi, veth_poll);
  1021. }
  1022. err = __veth_napi_enable_range(dev, start, end);
  1023. if (err) {
  1024. for (i = start; i < end; i++) {
  1025. struct veth_rq *rq = &priv->rq[i];
  1026. netif_napi_del(&rq->xdp_napi);
  1027. }
  1028. return err;
  1029. }
  1030. return err;
  1031. }
  1032. static int veth_napi_enable(struct net_device *dev)
  1033. {
  1034. return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
  1035. }
  1036. static void veth_disable_range_safe(struct net_device *dev, int start, int end)
  1037. {
  1038. struct veth_priv *priv = netdev_priv(dev);
  1039. if (start >= end)
  1040. return;
  1041. if (priv->_xdp_prog) {
  1042. veth_napi_del_range(dev, start, end);
  1043. veth_disable_xdp_range(dev, start, end, false);
  1044. } else if (veth_gro_requested(dev)) {
  1045. veth_napi_del_range(dev, start, end);
  1046. }
  1047. }
  1048. static int veth_enable_range_safe(struct net_device *dev, int start, int end)
  1049. {
  1050. struct veth_priv *priv = netdev_priv(dev);
  1051. int err;
  1052. if (start >= end)
  1053. return 0;
  1054. if (priv->_xdp_prog) {
  1055. /* these channels are freshly initialized, napi is not on there even
  1056. * when GRO is requeste
  1057. */
  1058. err = veth_enable_xdp_range(dev, start, end, false);
  1059. if (err)
  1060. return err;
  1061. err = __veth_napi_enable_range(dev, start, end);
  1062. if (err) {
  1063. /* on error always delete the newly added napis */
  1064. veth_disable_xdp_range(dev, start, end, true);
  1065. return err;
  1066. }
  1067. } else if (veth_gro_requested(dev)) {
  1068. return veth_napi_enable_range(dev, start, end);
  1069. }
  1070. return 0;
  1071. }
  1072. static void veth_set_xdp_features(struct net_device *dev)
  1073. {
  1074. struct veth_priv *priv = netdev_priv(dev);
  1075. struct net_device *peer;
  1076. peer = rtnl_dereference(priv->peer);
  1077. if (peer && peer->real_num_tx_queues <= dev->real_num_rx_queues) {
  1078. struct veth_priv *priv_peer = netdev_priv(peer);
  1079. xdp_features_t val = NETDEV_XDP_ACT_BASIC |
  1080. NETDEV_XDP_ACT_REDIRECT |
  1081. NETDEV_XDP_ACT_RX_SG;
  1082. if (priv_peer->_xdp_prog || veth_gro_requested(peer))
  1083. val |= NETDEV_XDP_ACT_NDO_XMIT |
  1084. NETDEV_XDP_ACT_NDO_XMIT_SG;
  1085. xdp_set_features_flag(dev, val);
  1086. } else {
  1087. xdp_clear_features_flag(dev);
  1088. }
  1089. }
  1090. static int veth_set_channels(struct net_device *dev,
  1091. struct ethtool_channels *ch)
  1092. {
  1093. struct veth_priv *priv = netdev_priv(dev);
  1094. unsigned int old_rx_count, new_rx_count;
  1095. struct veth_priv *peer_priv;
  1096. struct net_device *peer;
  1097. int err;
  1098. /* sanity check. Upper bounds are already enforced by the caller */
  1099. if (!ch->rx_count || !ch->tx_count)
  1100. return -EINVAL;
  1101. /* avoid braking XDP, if that is enabled */
  1102. peer = rtnl_dereference(priv->peer);
  1103. peer_priv = peer ? netdev_priv(peer) : NULL;
  1104. if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
  1105. return -EINVAL;
  1106. if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
  1107. return -EINVAL;
  1108. old_rx_count = dev->real_num_rx_queues;
  1109. new_rx_count = ch->rx_count;
  1110. if (netif_running(dev)) {
  1111. /* turn device off */
  1112. netif_carrier_off(dev);
  1113. if (peer)
  1114. netif_carrier_off(peer);
  1115. /* try to allocate new resources, as needed*/
  1116. err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
  1117. if (err)
  1118. goto out;
  1119. }
  1120. err = netif_set_real_num_rx_queues(dev, ch->rx_count);
  1121. if (err)
  1122. goto revert;
  1123. err = netif_set_real_num_tx_queues(dev, ch->tx_count);
  1124. if (err) {
  1125. int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
  1126. /* this error condition could happen only if rx and tx change
  1127. * in opposite directions (e.g. tx nr raises, rx nr decreases)
  1128. * and we can't do anything to fully restore the original
  1129. * status
  1130. */
  1131. if (err2)
  1132. pr_warn("Can't restore rx queues config %d -> %d %d",
  1133. new_rx_count, old_rx_count, err2);
  1134. else
  1135. goto revert;
  1136. }
  1137. out:
  1138. if (netif_running(dev)) {
  1139. /* note that we need to swap the arguments WRT the enable part
  1140. * to identify the range we have to disable
  1141. */
  1142. veth_disable_range_safe(dev, new_rx_count, old_rx_count);
  1143. netif_carrier_on(dev);
  1144. if (peer)
  1145. netif_carrier_on(peer);
  1146. }
  1147. /* update XDP supported features */
  1148. veth_set_xdp_features(dev);
  1149. if (peer)
  1150. veth_set_xdp_features(peer);
  1151. return err;
  1152. revert:
  1153. new_rx_count = old_rx_count;
  1154. old_rx_count = ch->rx_count;
  1155. goto out;
  1156. }
  1157. static int veth_open(struct net_device *dev)
  1158. {
  1159. struct veth_priv *priv = netdev_priv(dev);
  1160. struct net_device *peer = rtnl_dereference(priv->peer);
  1161. int err;
  1162. if (!peer)
  1163. return -ENOTCONN;
  1164. if (priv->_xdp_prog) {
  1165. err = veth_enable_xdp(dev);
  1166. if (err)
  1167. return err;
  1168. } else if (veth_gro_requested(dev)) {
  1169. err = veth_napi_enable(dev);
  1170. if (err)
  1171. return err;
  1172. }
  1173. if (peer->flags & IFF_UP) {
  1174. netif_carrier_on(dev);
  1175. netif_carrier_on(peer);
  1176. }
  1177. veth_set_xdp_features(dev);
  1178. return 0;
  1179. }
  1180. static int veth_close(struct net_device *dev)
  1181. {
  1182. struct veth_priv *priv = netdev_priv(dev);
  1183. struct net_device *peer = rtnl_dereference(priv->peer);
  1184. netif_carrier_off(dev);
  1185. if (peer)
  1186. netif_carrier_off(peer);
  1187. if (priv->_xdp_prog)
  1188. veth_disable_xdp(dev);
  1189. else if (veth_gro_requested(dev))
  1190. veth_napi_del(dev);
  1191. return 0;
  1192. }
  1193. static int is_valid_veth_mtu(int mtu)
  1194. {
  1195. return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
  1196. }
  1197. static int veth_alloc_queues(struct net_device *dev)
  1198. {
  1199. struct veth_priv *priv = netdev_priv(dev);
  1200. int i;
  1201. priv->rq = kvzalloc_objs(*priv->rq, dev->num_rx_queues,
  1202. GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
  1203. if (!priv->rq)
  1204. return -ENOMEM;
  1205. for (i = 0; i < dev->num_rx_queues; i++) {
  1206. priv->rq[i].dev = dev;
  1207. u64_stats_init(&priv->rq[i].stats.syncp);
  1208. }
  1209. return 0;
  1210. }
  1211. static void veth_free_queues(struct net_device *dev)
  1212. {
  1213. struct veth_priv *priv = netdev_priv(dev);
  1214. kvfree(priv->rq);
  1215. }
  1216. static int veth_dev_init(struct net_device *dev)
  1217. {
  1218. netdev_lockdep_set_classes(dev);
  1219. return veth_alloc_queues(dev);
  1220. }
  1221. static void veth_dev_free(struct net_device *dev)
  1222. {
  1223. veth_free_queues(dev);
  1224. }
  1225. #ifdef CONFIG_NET_POLL_CONTROLLER
  1226. static void veth_poll_controller(struct net_device *dev)
  1227. {
  1228. /* veth only receives frames when its peer sends one
  1229. * Since it has nothing to do with disabling irqs, we are guaranteed
  1230. * never to have pending data when we poll for it so
  1231. * there is nothing to do here.
  1232. *
  1233. * We need this though so netpoll recognizes us as an interface that
  1234. * supports polling, which enables bridge devices in virt setups to
  1235. * still use netconsole
  1236. */
  1237. }
  1238. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1239. static int veth_get_iflink(const struct net_device *dev)
  1240. {
  1241. struct veth_priv *priv = netdev_priv(dev);
  1242. struct net_device *peer;
  1243. int iflink;
  1244. rcu_read_lock();
  1245. peer = rcu_dereference(priv->peer);
  1246. iflink = peer ? READ_ONCE(peer->ifindex) : 0;
  1247. rcu_read_unlock();
  1248. return iflink;
  1249. }
  1250. static netdev_features_t veth_fix_features(struct net_device *dev,
  1251. netdev_features_t features)
  1252. {
  1253. struct veth_priv *priv = netdev_priv(dev);
  1254. struct net_device *peer;
  1255. peer = rtnl_dereference(priv->peer);
  1256. if (peer) {
  1257. struct veth_priv *peer_priv = netdev_priv(peer);
  1258. if (peer_priv->_xdp_prog)
  1259. features &= ~NETIF_F_GSO_SOFTWARE;
  1260. }
  1261. return features;
  1262. }
  1263. static int veth_set_features(struct net_device *dev,
  1264. netdev_features_t features)
  1265. {
  1266. netdev_features_t changed = features ^ dev->features;
  1267. struct veth_priv *priv = netdev_priv(dev);
  1268. struct net_device *peer;
  1269. int err;
  1270. if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
  1271. return 0;
  1272. peer = rtnl_dereference(priv->peer);
  1273. if (features & NETIF_F_GRO) {
  1274. err = veth_napi_enable(dev);
  1275. if (err)
  1276. return err;
  1277. if (peer)
  1278. xdp_features_set_redirect_target(peer, true);
  1279. } else {
  1280. if (peer)
  1281. xdp_features_clear_redirect_target(peer);
  1282. veth_napi_del(dev);
  1283. }
  1284. return 0;
  1285. }
  1286. static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
  1287. {
  1288. struct veth_priv *peer_priv, *priv = netdev_priv(dev);
  1289. struct net_device *peer;
  1290. if (new_hr < 0)
  1291. new_hr = 0;
  1292. rcu_read_lock();
  1293. peer = rcu_dereference(priv->peer);
  1294. if (unlikely(!peer))
  1295. goto out;
  1296. peer_priv = netdev_priv(peer);
  1297. priv->requested_headroom = new_hr;
  1298. new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
  1299. dev->needed_headroom = new_hr;
  1300. peer->needed_headroom = new_hr;
  1301. out:
  1302. rcu_read_unlock();
  1303. }
  1304. static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  1305. struct netlink_ext_ack *extack)
  1306. {
  1307. struct veth_priv *priv = netdev_priv(dev);
  1308. struct bpf_prog *old_prog;
  1309. struct net_device *peer;
  1310. unsigned int max_mtu;
  1311. int err;
  1312. old_prog = priv->_xdp_prog;
  1313. priv->_xdp_prog = prog;
  1314. peer = rtnl_dereference(priv->peer);
  1315. if (prog) {
  1316. if (!peer) {
  1317. NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
  1318. err = -ENOTCONN;
  1319. goto err;
  1320. }
  1321. max_mtu = SKB_WITH_OVERHEAD(PAGE_SIZE - VETH_XDP_HEADROOM) -
  1322. peer->hard_header_len;
  1323. /* Allow increasing the max_mtu if the program supports
  1324. * XDP fragments.
  1325. */
  1326. if (prog->aux->xdp_has_frags)
  1327. max_mtu += PAGE_SIZE * MAX_SKB_FRAGS;
  1328. if (peer->mtu > max_mtu) {
  1329. NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
  1330. err = -ERANGE;
  1331. goto err;
  1332. }
  1333. if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
  1334. NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
  1335. err = -ENOSPC;
  1336. goto err;
  1337. }
  1338. if (dev->flags & IFF_UP) {
  1339. err = veth_enable_xdp(dev);
  1340. if (err) {
  1341. NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
  1342. goto err;
  1343. }
  1344. }
  1345. if (!old_prog) {
  1346. peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
  1347. peer->max_mtu = max_mtu;
  1348. }
  1349. xdp_features_set_redirect_target(peer, true);
  1350. }
  1351. if (old_prog) {
  1352. if (!prog) {
  1353. if (peer && !veth_gro_requested(dev))
  1354. xdp_features_clear_redirect_target(peer);
  1355. if (dev->flags & IFF_UP)
  1356. veth_disable_xdp(dev);
  1357. if (peer) {
  1358. peer->hw_features |= NETIF_F_GSO_SOFTWARE;
  1359. peer->max_mtu = ETH_MAX_MTU;
  1360. }
  1361. }
  1362. bpf_prog_put(old_prog);
  1363. }
  1364. if ((!!old_prog ^ !!prog) && peer)
  1365. netdev_update_features(peer);
  1366. return 0;
  1367. err:
  1368. priv->_xdp_prog = old_prog;
  1369. return err;
  1370. }
  1371. static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  1372. {
  1373. switch (xdp->command) {
  1374. case XDP_SETUP_PROG:
  1375. return veth_xdp_set(dev, xdp->prog, xdp->extack);
  1376. default:
  1377. return -EINVAL;
  1378. }
  1379. }
  1380. static int veth_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
  1381. {
  1382. struct veth_xdp_buff *_ctx = (void *)ctx;
  1383. if (!_ctx->skb)
  1384. return -ENODATA;
  1385. *timestamp = skb_hwtstamps(_ctx->skb)->hwtstamp;
  1386. return 0;
  1387. }
  1388. static int veth_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash,
  1389. enum xdp_rss_hash_type *rss_type)
  1390. {
  1391. struct veth_xdp_buff *_ctx = (void *)ctx;
  1392. struct sk_buff *skb = _ctx->skb;
  1393. if (!skb)
  1394. return -ENODATA;
  1395. *hash = skb_get_hash(skb);
  1396. *rss_type = skb->l4_hash ? XDP_RSS_TYPE_L4_ANY : XDP_RSS_TYPE_NONE;
  1397. return 0;
  1398. }
  1399. static int veth_xdp_rx_vlan_tag(const struct xdp_md *ctx, __be16 *vlan_proto,
  1400. u16 *vlan_tci)
  1401. {
  1402. const struct veth_xdp_buff *_ctx = (void *)ctx;
  1403. const struct sk_buff *skb = _ctx->skb;
  1404. int err;
  1405. if (!skb)
  1406. return -ENODATA;
  1407. err = __vlan_hwaccel_get_tag(skb, vlan_tci);
  1408. if (err)
  1409. return err;
  1410. *vlan_proto = skb->vlan_proto;
  1411. return err;
  1412. }
  1413. static const struct net_device_ops veth_netdev_ops = {
  1414. .ndo_init = veth_dev_init,
  1415. .ndo_open = veth_open,
  1416. .ndo_stop = veth_close,
  1417. .ndo_start_xmit = veth_xmit,
  1418. .ndo_get_stats64 = veth_get_stats64,
  1419. .ndo_set_rx_mode = veth_set_multicast_list,
  1420. .ndo_set_mac_address = eth_mac_addr,
  1421. #ifdef CONFIG_NET_POLL_CONTROLLER
  1422. .ndo_poll_controller = veth_poll_controller,
  1423. #endif
  1424. .ndo_get_iflink = veth_get_iflink,
  1425. .ndo_fix_features = veth_fix_features,
  1426. .ndo_set_features = veth_set_features,
  1427. .ndo_features_check = passthru_features_check,
  1428. .ndo_set_rx_headroom = veth_set_rx_headroom,
  1429. .ndo_bpf = veth_xdp,
  1430. .ndo_xdp_xmit = veth_ndo_xdp_xmit,
  1431. .ndo_get_peer_dev = veth_peer_dev,
  1432. };
  1433. static const struct xdp_metadata_ops veth_xdp_metadata_ops = {
  1434. .xmo_rx_timestamp = veth_xdp_rx_timestamp,
  1435. .xmo_rx_hash = veth_xdp_rx_hash,
  1436. .xmo_rx_vlan_tag = veth_xdp_rx_vlan_tag,
  1437. };
  1438. #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
  1439. NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
  1440. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
  1441. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
  1442. NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
  1443. static void veth_setup(struct net_device *dev)
  1444. {
  1445. ether_setup(dev);
  1446. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1447. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1448. dev->priv_flags |= IFF_NO_QUEUE;
  1449. dev->priv_flags |= IFF_PHONY_HEADROOM;
  1450. dev->priv_flags |= IFF_DISABLE_NETPOLL;
  1451. dev->lltx = true;
  1452. dev->netdev_ops = &veth_netdev_ops;
  1453. dev->xdp_metadata_ops = &veth_xdp_metadata_ops;
  1454. dev->ethtool_ops = &veth_ethtool_ops;
  1455. dev->features |= VETH_FEATURES;
  1456. dev->vlan_features = dev->features &
  1457. ~(NETIF_F_HW_VLAN_CTAG_TX |
  1458. NETIF_F_HW_VLAN_STAG_TX |
  1459. NETIF_F_HW_VLAN_CTAG_RX |
  1460. NETIF_F_HW_VLAN_STAG_RX);
  1461. dev->needs_free_netdev = true;
  1462. dev->priv_destructor = veth_dev_free;
  1463. dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
  1464. dev->max_mtu = ETH_MAX_MTU;
  1465. dev->hw_features = VETH_FEATURES;
  1466. dev->hw_enc_features = VETH_FEATURES;
  1467. dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
  1468. netif_set_tso_max_size(dev, GSO_MAX_SIZE);
  1469. }
  1470. /*
  1471. * netlink interface
  1472. */
  1473. static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
  1474. struct netlink_ext_ack *extack)
  1475. {
  1476. if (tb[IFLA_ADDRESS]) {
  1477. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1478. return -EINVAL;
  1479. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1480. return -EADDRNOTAVAIL;
  1481. }
  1482. if (tb[IFLA_MTU]) {
  1483. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  1484. return -EINVAL;
  1485. }
  1486. return 0;
  1487. }
  1488. static struct rtnl_link_ops veth_link_ops;
  1489. static void veth_disable_gro(struct net_device *dev)
  1490. {
  1491. dev->features &= ~NETIF_F_GRO;
  1492. dev->wanted_features &= ~NETIF_F_GRO;
  1493. netdev_update_features(dev);
  1494. }
  1495. static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
  1496. {
  1497. int err;
  1498. if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
  1499. err = netif_set_real_num_tx_queues(dev, 1);
  1500. if (err)
  1501. return err;
  1502. }
  1503. if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
  1504. err = netif_set_real_num_rx_queues(dev, 1);
  1505. if (err)
  1506. return err;
  1507. }
  1508. return 0;
  1509. }
  1510. static int veth_newlink(struct net_device *dev,
  1511. struct rtnl_newlink_params *params,
  1512. struct netlink_ext_ack *extack)
  1513. {
  1514. struct net *peer_net = rtnl_newlink_peer_net(params);
  1515. struct nlattr **data = params->data;
  1516. struct nlattr **tb = params->tb;
  1517. int err;
  1518. struct net_device *peer;
  1519. struct veth_priv *priv;
  1520. char ifname[IFNAMSIZ];
  1521. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  1522. unsigned char name_assign_type;
  1523. struct ifinfomsg *ifmp;
  1524. /*
  1525. * create and register peer first
  1526. */
  1527. if (data && data[VETH_INFO_PEER]) {
  1528. struct nlattr *nla_peer = data[VETH_INFO_PEER];
  1529. ifmp = nla_data(nla_peer);
  1530. rtnl_nla_parse_ifinfomsg(peer_tb, nla_peer, extack);
  1531. tbp = peer_tb;
  1532. } else {
  1533. ifmp = NULL;
  1534. tbp = tb;
  1535. }
  1536. if (ifmp && tbp[IFLA_IFNAME]) {
  1537. nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  1538. name_assign_type = NET_NAME_USER;
  1539. } else {
  1540. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  1541. name_assign_type = NET_NAME_ENUM;
  1542. }
  1543. peer = rtnl_create_link(peer_net, ifname, name_assign_type,
  1544. &veth_link_ops, tbp, extack);
  1545. if (IS_ERR(peer))
  1546. return PTR_ERR(peer);
  1547. if (!ifmp || !tbp[IFLA_ADDRESS])
  1548. eth_hw_addr_random(peer);
  1549. if (ifmp && (dev->ifindex != 0))
  1550. peer->ifindex = ifmp->ifi_index;
  1551. netif_inherit_tso_max(peer, dev);
  1552. err = register_netdevice(peer);
  1553. if (err < 0)
  1554. goto err_register_peer;
  1555. /* keep GRO disabled by default to be consistent with the established
  1556. * veth behavior
  1557. */
  1558. veth_disable_gro(peer);
  1559. netif_carrier_off(peer);
  1560. err = rtnl_configure_link(peer, ifmp, 0, NULL);
  1561. if (err < 0)
  1562. goto err_configure_peer;
  1563. /*
  1564. * register dev last
  1565. *
  1566. * note, that since we've registered new device the dev's name
  1567. * should be re-allocated
  1568. */
  1569. if (tb[IFLA_ADDRESS] == NULL)
  1570. eth_hw_addr_random(dev);
  1571. if (tb[IFLA_IFNAME])
  1572. nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  1573. else
  1574. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  1575. err = register_netdevice(dev);
  1576. if (err < 0)
  1577. goto err_register_dev;
  1578. netif_carrier_off(dev);
  1579. /*
  1580. * tie the deviced together
  1581. */
  1582. priv = netdev_priv(dev);
  1583. rcu_assign_pointer(priv->peer, peer);
  1584. err = veth_init_queues(dev, tb);
  1585. if (err)
  1586. goto err_queues;
  1587. priv = netdev_priv(peer);
  1588. rcu_assign_pointer(priv->peer, dev);
  1589. err = veth_init_queues(peer, tb);
  1590. if (err)
  1591. goto err_queues;
  1592. veth_disable_gro(dev);
  1593. /* update XDP supported features */
  1594. veth_set_xdp_features(dev);
  1595. veth_set_xdp_features(peer);
  1596. return 0;
  1597. err_queues:
  1598. unregister_netdevice(dev);
  1599. err_register_dev:
  1600. /* nothing to do */
  1601. err_configure_peer:
  1602. unregister_netdevice(peer);
  1603. return err;
  1604. err_register_peer:
  1605. free_netdev(peer);
  1606. return err;
  1607. }
  1608. static void veth_dellink(struct net_device *dev, struct list_head *head)
  1609. {
  1610. struct veth_priv *priv;
  1611. struct net_device *peer;
  1612. priv = netdev_priv(dev);
  1613. peer = rtnl_dereference(priv->peer);
  1614. /* Note : dellink() is called from default_device_exit_batch(),
  1615. * before a rcu_synchronize() point. The devices are guaranteed
  1616. * not being freed before one RCU grace period.
  1617. */
  1618. RCU_INIT_POINTER(priv->peer, NULL);
  1619. unregister_netdevice_queue(dev, head);
  1620. if (peer) {
  1621. priv = netdev_priv(peer);
  1622. RCU_INIT_POINTER(priv->peer, NULL);
  1623. unregister_netdevice_queue(peer, head);
  1624. }
  1625. }
  1626. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  1627. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  1628. };
  1629. static struct net *veth_get_link_net(const struct net_device *dev)
  1630. {
  1631. struct veth_priv *priv = netdev_priv(dev);
  1632. struct net_device *peer = rtnl_dereference(priv->peer);
  1633. return peer ? dev_net(peer) : dev_net(dev);
  1634. }
  1635. static unsigned int veth_get_num_queues(void)
  1636. {
  1637. /* enforce the same queue limit as rtnl_create_link */
  1638. int queues = num_possible_cpus();
  1639. if (queues > 4096)
  1640. queues = 4096;
  1641. return queues;
  1642. }
  1643. static struct rtnl_link_ops veth_link_ops = {
  1644. .kind = DRV_NAME,
  1645. .priv_size = sizeof(struct veth_priv),
  1646. .setup = veth_setup,
  1647. .validate = veth_validate,
  1648. .newlink = veth_newlink,
  1649. .dellink = veth_dellink,
  1650. .policy = veth_policy,
  1651. .peer_type = VETH_INFO_PEER,
  1652. .maxtype = VETH_INFO_MAX,
  1653. .get_link_net = veth_get_link_net,
  1654. .get_num_tx_queues = veth_get_num_queues,
  1655. .get_num_rx_queues = veth_get_num_queues,
  1656. };
  1657. /*
  1658. * init/fini
  1659. */
  1660. static __init int veth_init(void)
  1661. {
  1662. return rtnl_link_register(&veth_link_ops);
  1663. }
  1664. static __exit void veth_exit(void)
  1665. {
  1666. rtnl_link_unregister(&veth_link_ops);
  1667. }
  1668. module_init(veth_init);
  1669. module_exit(veth_exit);
  1670. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  1671. MODULE_LICENSE("GPL v2");
  1672. MODULE_ALIAS_RTNL_LINK(DRV_NAME);