veth.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * drivers/net/veth.c
  3. *
  4. * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
  5. *
  6. * Author: Pavel Emelianov <xemul@openvz.org>
  7. * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
  8. *
  9. */
  10. #include <linux/netdevice.h>
  11. #include <linux/slab.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/u64_stats_sync.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/dst.h>
  17. #include <net/xfrm.h>
  18. #include <net/xdp.h>
  19. #include <linux/veth.h>
  20. #include <linux/module.h>
  21. #include <linux/bpf.h>
  22. #include <linux/filter.h>
  23. #include <linux/ptr_ring.h>
  24. #include <linux/bpf_trace.h>
  25. #define DRV_NAME "veth"
  26. #define DRV_VERSION "1.0"
  27. #define VETH_XDP_FLAG BIT(0)
  28. #define VETH_RING_SIZE 256
  29. #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
  30. /* Separating two types of XDP xmit */
  31. #define VETH_XDP_TX BIT(0)
  32. #define VETH_XDP_REDIR BIT(1)
  33. struct pcpu_vstats {
  34. u64 packets;
  35. u64 bytes;
  36. struct u64_stats_sync syncp;
  37. };
  38. struct veth_rq {
  39. struct napi_struct xdp_napi;
  40. struct net_device *dev;
  41. struct bpf_prog __rcu *xdp_prog;
  42. struct xdp_mem_info xdp_mem;
  43. bool rx_notify_masked;
  44. struct ptr_ring xdp_ring;
  45. struct xdp_rxq_info xdp_rxq;
  46. };
  47. struct veth_priv {
  48. struct net_device __rcu *peer;
  49. atomic64_t dropped;
  50. struct bpf_prog *_xdp_prog;
  51. struct veth_rq *rq;
  52. unsigned int requested_headroom;
  53. };
  54. /*
  55. * ethtool interface
  56. */
  57. static struct {
  58. const char string[ETH_GSTRING_LEN];
  59. } ethtool_stats_keys[] = {
  60. { "peer_ifindex" },
  61. };
  62. static int veth_get_link_ksettings(struct net_device *dev,
  63. struct ethtool_link_ksettings *cmd)
  64. {
  65. cmd->base.speed = SPEED_10000;
  66. cmd->base.duplex = DUPLEX_FULL;
  67. cmd->base.port = PORT_TP;
  68. cmd->base.autoneg = AUTONEG_DISABLE;
  69. return 0;
  70. }
  71. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  72. {
  73. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  74. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  75. }
  76. static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  77. {
  78. switch(stringset) {
  79. case ETH_SS_STATS:
  80. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  81. break;
  82. }
  83. }
  84. static int veth_get_sset_count(struct net_device *dev, int sset)
  85. {
  86. switch (sset) {
  87. case ETH_SS_STATS:
  88. return ARRAY_SIZE(ethtool_stats_keys);
  89. default:
  90. return -EOPNOTSUPP;
  91. }
  92. }
  93. static void veth_get_ethtool_stats(struct net_device *dev,
  94. struct ethtool_stats *stats, u64 *data)
  95. {
  96. struct veth_priv *priv = netdev_priv(dev);
  97. struct net_device *peer = rtnl_dereference(priv->peer);
  98. data[0] = peer ? peer->ifindex : 0;
  99. }
  100. static const struct ethtool_ops veth_ethtool_ops = {
  101. .get_drvinfo = veth_get_drvinfo,
  102. .get_link = ethtool_op_get_link,
  103. .get_strings = veth_get_strings,
  104. .get_sset_count = veth_get_sset_count,
  105. .get_ethtool_stats = veth_get_ethtool_stats,
  106. .get_link_ksettings = veth_get_link_ksettings,
  107. };
  108. /* general routines */
  109. static bool veth_is_xdp_frame(void *ptr)
  110. {
  111. return (unsigned long)ptr & VETH_XDP_FLAG;
  112. }
  113. static void *veth_ptr_to_xdp(void *ptr)
  114. {
  115. return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
  116. }
  117. static void *veth_xdp_to_ptr(void *ptr)
  118. {
  119. return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
  120. }
  121. static void veth_ptr_free(void *ptr)
  122. {
  123. if (veth_is_xdp_frame(ptr))
  124. xdp_return_frame(veth_ptr_to_xdp(ptr));
  125. else
  126. kfree_skb(ptr);
  127. }
  128. static void __veth_xdp_flush(struct veth_rq *rq)
  129. {
  130. /* Write ptr_ring before reading rx_notify_masked */
  131. smp_mb();
  132. if (!rq->rx_notify_masked) {
  133. rq->rx_notify_masked = true;
  134. napi_schedule(&rq->xdp_napi);
  135. }
  136. }
  137. static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
  138. {
  139. if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
  140. dev_kfree_skb_any(skb);
  141. return NET_RX_DROP;
  142. }
  143. return NET_RX_SUCCESS;
  144. }
  145. static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
  146. struct veth_rq *rq, bool xdp)
  147. {
  148. return __dev_forward_skb(dev, skb) ?: xdp ?
  149. veth_xdp_rx(rq, skb) :
  150. netif_rx(skb);
  151. }
  152. static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
  153. {
  154. struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
  155. struct veth_rq *rq = NULL;
  156. struct net_device *rcv;
  157. int length = skb->len;
  158. bool rcv_xdp = false;
  159. int rxq;
  160. rcu_read_lock();
  161. rcv = rcu_dereference(priv->peer);
  162. if (unlikely(!rcv)) {
  163. kfree_skb(skb);
  164. goto drop;
  165. }
  166. rcv_priv = netdev_priv(rcv);
  167. rxq = skb_get_queue_mapping(skb);
  168. if (rxq < rcv->real_num_rx_queues) {
  169. rq = &rcv_priv->rq[rxq];
  170. rcv_xdp = rcu_access_pointer(rq->xdp_prog);
  171. skb_record_rx_queue(skb, rxq);
  172. }
  173. if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
  174. struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
  175. u64_stats_update_begin(&stats->syncp);
  176. stats->bytes += length;
  177. stats->packets++;
  178. u64_stats_update_end(&stats->syncp);
  179. } else {
  180. drop:
  181. atomic64_inc(&priv->dropped);
  182. }
  183. if (rcv_xdp)
  184. __veth_xdp_flush(rq);
  185. rcu_read_unlock();
  186. return NETDEV_TX_OK;
  187. }
  188. static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
  189. {
  190. struct veth_priv *priv = netdev_priv(dev);
  191. int cpu;
  192. result->packets = 0;
  193. result->bytes = 0;
  194. for_each_possible_cpu(cpu) {
  195. struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
  196. u64 packets, bytes;
  197. unsigned int start;
  198. do {
  199. start = u64_stats_fetch_begin_irq(&stats->syncp);
  200. packets = stats->packets;
  201. bytes = stats->bytes;
  202. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  203. result->packets += packets;
  204. result->bytes += bytes;
  205. }
  206. return atomic64_read(&priv->dropped);
  207. }
  208. static void veth_get_stats64(struct net_device *dev,
  209. struct rtnl_link_stats64 *tot)
  210. {
  211. struct veth_priv *priv = netdev_priv(dev);
  212. struct net_device *peer;
  213. struct pcpu_vstats one;
  214. tot->tx_dropped = veth_stats_one(&one, dev);
  215. tot->tx_bytes = one.bytes;
  216. tot->tx_packets = one.packets;
  217. rcu_read_lock();
  218. peer = rcu_dereference(priv->peer);
  219. if (peer) {
  220. tot->rx_dropped = veth_stats_one(&one, peer);
  221. tot->rx_bytes = one.bytes;
  222. tot->rx_packets = one.packets;
  223. }
  224. rcu_read_unlock();
  225. }
  226. /* fake multicast ability */
  227. static void veth_set_multicast_list(struct net_device *dev)
  228. {
  229. }
  230. static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
  231. int buflen)
  232. {
  233. struct sk_buff *skb;
  234. if (!buflen) {
  235. buflen = SKB_DATA_ALIGN(headroom + len) +
  236. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  237. }
  238. skb = build_skb(head, buflen);
  239. if (!skb)
  240. return NULL;
  241. skb_reserve(skb, headroom);
  242. skb_put(skb, len);
  243. return skb;
  244. }
  245. static int veth_select_rxq(struct net_device *dev)
  246. {
  247. return smp_processor_id() % dev->real_num_rx_queues;
  248. }
  249. static int veth_xdp_xmit(struct net_device *dev, int n,
  250. struct xdp_frame **frames, u32 flags)
  251. {
  252. struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
  253. struct net_device *rcv;
  254. unsigned int max_len;
  255. struct veth_rq *rq;
  256. int i, drops = 0;
  257. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  258. return -EINVAL;
  259. rcv = rcu_dereference(priv->peer);
  260. if (unlikely(!rcv))
  261. return -ENXIO;
  262. rcv_priv = netdev_priv(rcv);
  263. rq = &rcv_priv->rq[veth_select_rxq(rcv)];
  264. /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
  265. * side. This means an XDP program is loaded on the peer and the peer
  266. * device is up.
  267. */
  268. if (!rcu_access_pointer(rq->xdp_prog))
  269. return -ENXIO;
  270. max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
  271. spin_lock(&rq->xdp_ring.producer_lock);
  272. for (i = 0; i < n; i++) {
  273. struct xdp_frame *frame = frames[i];
  274. void *ptr = veth_xdp_to_ptr(frame);
  275. if (unlikely(frame->len > max_len ||
  276. __ptr_ring_produce(&rq->xdp_ring, ptr))) {
  277. xdp_return_frame_rx_napi(frame);
  278. drops++;
  279. }
  280. }
  281. spin_unlock(&rq->xdp_ring.producer_lock);
  282. if (flags & XDP_XMIT_FLUSH)
  283. __veth_xdp_flush(rq);
  284. return n - drops;
  285. }
  286. static void veth_xdp_flush(struct net_device *dev)
  287. {
  288. struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
  289. struct net_device *rcv;
  290. struct veth_rq *rq;
  291. rcu_read_lock();
  292. rcv = rcu_dereference(priv->peer);
  293. if (unlikely(!rcv))
  294. goto out;
  295. rcv_priv = netdev_priv(rcv);
  296. rq = &rcv_priv->rq[veth_select_rxq(rcv)];
  297. /* xdp_ring is initialized on receive side? */
  298. if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
  299. goto out;
  300. __veth_xdp_flush(rq);
  301. out:
  302. rcu_read_unlock();
  303. }
  304. static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
  305. {
  306. struct xdp_frame *frame = convert_to_xdp_frame(xdp);
  307. if (unlikely(!frame))
  308. return -EOVERFLOW;
  309. return veth_xdp_xmit(dev, 1, &frame, 0);
  310. }
  311. static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
  312. struct xdp_frame *frame,
  313. unsigned int *xdp_xmit)
  314. {
  315. void *hard_start = frame->data - frame->headroom;
  316. int len = frame->len, delta = 0;
  317. struct xdp_frame orig_frame;
  318. struct bpf_prog *xdp_prog;
  319. unsigned int headroom;
  320. struct sk_buff *skb;
  321. /* bpf_xdp_adjust_head() assures BPF cannot access xdp_frame area */
  322. hard_start -= sizeof(struct xdp_frame);
  323. rcu_read_lock();
  324. xdp_prog = rcu_dereference(rq->xdp_prog);
  325. if (likely(xdp_prog)) {
  326. struct xdp_buff xdp;
  327. u32 act;
  328. xdp.data_hard_start = hard_start;
  329. xdp.data = frame->data;
  330. xdp.data_end = frame->data + frame->len;
  331. xdp.data_meta = frame->data - frame->metasize;
  332. xdp.rxq = &rq->xdp_rxq;
  333. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  334. switch (act) {
  335. case XDP_PASS:
  336. delta = frame->data - xdp.data;
  337. len = xdp.data_end - xdp.data;
  338. break;
  339. case XDP_TX:
  340. orig_frame = *frame;
  341. xdp.rxq->mem = frame->mem;
  342. if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
  343. trace_xdp_exception(rq->dev, xdp_prog, act);
  344. frame = &orig_frame;
  345. goto err_xdp;
  346. }
  347. *xdp_xmit |= VETH_XDP_TX;
  348. rcu_read_unlock();
  349. goto xdp_xmit;
  350. case XDP_REDIRECT:
  351. orig_frame = *frame;
  352. xdp.rxq->mem = frame->mem;
  353. if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
  354. frame = &orig_frame;
  355. goto err_xdp;
  356. }
  357. *xdp_xmit |= VETH_XDP_REDIR;
  358. rcu_read_unlock();
  359. goto xdp_xmit;
  360. default:
  361. bpf_warn_invalid_xdp_action(act);
  362. case XDP_ABORTED:
  363. trace_xdp_exception(rq->dev, xdp_prog, act);
  364. case XDP_DROP:
  365. goto err_xdp;
  366. }
  367. }
  368. rcu_read_unlock();
  369. headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
  370. skb = veth_build_skb(hard_start, headroom, len, 0);
  371. if (!skb) {
  372. xdp_return_frame(frame);
  373. goto err;
  374. }
  375. xdp_scrub_frame(frame);
  376. skb->protocol = eth_type_trans(skb, rq->dev);
  377. err:
  378. return skb;
  379. err_xdp:
  380. rcu_read_unlock();
  381. xdp_return_frame(frame);
  382. xdp_xmit:
  383. return NULL;
  384. }
  385. static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq, struct sk_buff *skb,
  386. unsigned int *xdp_xmit)
  387. {
  388. u32 pktlen, headroom, act, metalen;
  389. void *orig_data, *orig_data_end;
  390. struct bpf_prog *xdp_prog;
  391. int mac_len, delta, off;
  392. struct xdp_buff xdp;
  393. skb_orphan(skb);
  394. rcu_read_lock();
  395. xdp_prog = rcu_dereference(rq->xdp_prog);
  396. if (unlikely(!xdp_prog)) {
  397. rcu_read_unlock();
  398. goto out;
  399. }
  400. mac_len = skb->data - skb_mac_header(skb);
  401. pktlen = skb->len + mac_len;
  402. headroom = skb_headroom(skb) - mac_len;
  403. if (skb_shared(skb) || skb_head_is_locked(skb) ||
  404. skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
  405. struct sk_buff *nskb;
  406. int size, head_off;
  407. void *head, *start;
  408. struct page *page;
  409. size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
  410. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  411. if (size > PAGE_SIZE)
  412. goto drop;
  413. page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
  414. if (!page)
  415. goto drop;
  416. head = page_address(page);
  417. start = head + VETH_XDP_HEADROOM;
  418. if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
  419. page_frag_free(head);
  420. goto drop;
  421. }
  422. nskb = veth_build_skb(head,
  423. VETH_XDP_HEADROOM + mac_len, skb->len,
  424. PAGE_SIZE);
  425. if (!nskb) {
  426. page_frag_free(head);
  427. goto drop;
  428. }
  429. skb_copy_header(nskb, skb);
  430. head_off = skb_headroom(nskb) - skb_headroom(skb);
  431. skb_headers_offset_update(nskb, head_off);
  432. consume_skb(skb);
  433. skb = nskb;
  434. }
  435. xdp.data_hard_start = skb->head;
  436. xdp.data = skb_mac_header(skb);
  437. xdp.data_end = xdp.data + pktlen;
  438. xdp.data_meta = xdp.data;
  439. xdp.rxq = &rq->xdp_rxq;
  440. orig_data = xdp.data;
  441. orig_data_end = xdp.data_end;
  442. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  443. switch (act) {
  444. case XDP_PASS:
  445. break;
  446. case XDP_TX:
  447. get_page(virt_to_page(xdp.data));
  448. consume_skb(skb);
  449. xdp.rxq->mem = rq->xdp_mem;
  450. if (unlikely(veth_xdp_tx(rq->dev, &xdp) < 0)) {
  451. trace_xdp_exception(rq->dev, xdp_prog, act);
  452. goto err_xdp;
  453. }
  454. *xdp_xmit |= VETH_XDP_TX;
  455. rcu_read_unlock();
  456. goto xdp_xmit;
  457. case XDP_REDIRECT:
  458. get_page(virt_to_page(xdp.data));
  459. consume_skb(skb);
  460. xdp.rxq->mem = rq->xdp_mem;
  461. if (xdp_do_redirect(rq->dev, &xdp, xdp_prog))
  462. goto err_xdp;
  463. *xdp_xmit |= VETH_XDP_REDIR;
  464. rcu_read_unlock();
  465. goto xdp_xmit;
  466. default:
  467. bpf_warn_invalid_xdp_action(act);
  468. case XDP_ABORTED:
  469. trace_xdp_exception(rq->dev, xdp_prog, act);
  470. case XDP_DROP:
  471. goto drop;
  472. }
  473. rcu_read_unlock();
  474. delta = orig_data - xdp.data;
  475. off = mac_len + delta;
  476. if (off > 0)
  477. __skb_push(skb, off);
  478. else if (off < 0)
  479. __skb_pull(skb, -off);
  480. skb->mac_header -= delta;
  481. off = xdp.data_end - orig_data_end;
  482. if (off != 0)
  483. __skb_put(skb, off);
  484. skb->protocol = eth_type_trans(skb, rq->dev);
  485. metalen = xdp.data - xdp.data_meta;
  486. if (metalen)
  487. skb_metadata_set(skb, metalen);
  488. out:
  489. return skb;
  490. drop:
  491. rcu_read_unlock();
  492. kfree_skb(skb);
  493. return NULL;
  494. err_xdp:
  495. rcu_read_unlock();
  496. page_frag_free(xdp.data);
  497. xdp_xmit:
  498. return NULL;
  499. }
  500. static int veth_xdp_rcv(struct veth_rq *rq, int budget, unsigned int *xdp_xmit)
  501. {
  502. int i, done = 0;
  503. for (i = 0; i < budget; i++) {
  504. void *ptr = __ptr_ring_consume(&rq->xdp_ring);
  505. struct sk_buff *skb;
  506. if (!ptr)
  507. break;
  508. if (veth_is_xdp_frame(ptr)) {
  509. skb = veth_xdp_rcv_one(rq, veth_ptr_to_xdp(ptr),
  510. xdp_xmit);
  511. } else {
  512. skb = veth_xdp_rcv_skb(rq, ptr, xdp_xmit);
  513. }
  514. if (skb)
  515. napi_gro_receive(&rq->xdp_napi, skb);
  516. done++;
  517. }
  518. return done;
  519. }
  520. static int veth_poll(struct napi_struct *napi, int budget)
  521. {
  522. struct veth_rq *rq =
  523. container_of(napi, struct veth_rq, xdp_napi);
  524. unsigned int xdp_xmit = 0;
  525. int done;
  526. xdp_set_return_frame_no_direct();
  527. done = veth_xdp_rcv(rq, budget, &xdp_xmit);
  528. if (done < budget && napi_complete_done(napi, done)) {
  529. /* Write rx_notify_masked before reading ptr_ring */
  530. smp_store_mb(rq->rx_notify_masked, false);
  531. if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
  532. rq->rx_notify_masked = true;
  533. napi_schedule(&rq->xdp_napi);
  534. }
  535. }
  536. if (xdp_xmit & VETH_XDP_TX)
  537. veth_xdp_flush(rq->dev);
  538. if (xdp_xmit & VETH_XDP_REDIR)
  539. xdp_do_flush_map();
  540. xdp_clear_return_frame_no_direct();
  541. return done;
  542. }
  543. static int veth_napi_add(struct net_device *dev)
  544. {
  545. struct veth_priv *priv = netdev_priv(dev);
  546. int err, i;
  547. for (i = 0; i < dev->real_num_rx_queues; i++) {
  548. struct veth_rq *rq = &priv->rq[i];
  549. err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
  550. if (err)
  551. goto err_xdp_ring;
  552. }
  553. for (i = 0; i < dev->real_num_rx_queues; i++) {
  554. struct veth_rq *rq = &priv->rq[i];
  555. netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
  556. napi_enable(&rq->xdp_napi);
  557. }
  558. return 0;
  559. err_xdp_ring:
  560. for (i--; i >= 0; i--)
  561. ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
  562. return err;
  563. }
  564. static void veth_napi_del(struct net_device *dev)
  565. {
  566. struct veth_priv *priv = netdev_priv(dev);
  567. int i;
  568. for (i = 0; i < dev->real_num_rx_queues; i++) {
  569. struct veth_rq *rq = &priv->rq[i];
  570. napi_disable(&rq->xdp_napi);
  571. napi_hash_del(&rq->xdp_napi);
  572. }
  573. synchronize_net();
  574. for (i = 0; i < dev->real_num_rx_queues; i++) {
  575. struct veth_rq *rq = &priv->rq[i];
  576. netif_napi_del(&rq->xdp_napi);
  577. rq->rx_notify_masked = false;
  578. ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
  579. }
  580. }
  581. static int veth_enable_xdp(struct net_device *dev)
  582. {
  583. struct veth_priv *priv = netdev_priv(dev);
  584. int err, i;
  585. if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
  586. for (i = 0; i < dev->real_num_rx_queues; i++) {
  587. struct veth_rq *rq = &priv->rq[i];
  588. err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i);
  589. if (err < 0)
  590. goto err_rxq_reg;
  591. err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
  592. MEM_TYPE_PAGE_SHARED,
  593. NULL);
  594. if (err < 0)
  595. goto err_reg_mem;
  596. /* Save original mem info as it can be overwritten */
  597. rq->xdp_mem = rq->xdp_rxq.mem;
  598. }
  599. err = veth_napi_add(dev);
  600. if (err)
  601. goto err_rxq_reg;
  602. }
  603. for (i = 0; i < dev->real_num_rx_queues; i++)
  604. rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
  605. return 0;
  606. err_reg_mem:
  607. xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
  608. err_rxq_reg:
  609. for (i--; i >= 0; i--)
  610. xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
  611. return err;
  612. }
  613. static void veth_disable_xdp(struct net_device *dev)
  614. {
  615. struct veth_priv *priv = netdev_priv(dev);
  616. int i;
  617. for (i = 0; i < dev->real_num_rx_queues; i++)
  618. rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
  619. veth_napi_del(dev);
  620. for (i = 0; i < dev->real_num_rx_queues; i++) {
  621. struct veth_rq *rq = &priv->rq[i];
  622. rq->xdp_rxq.mem = rq->xdp_mem;
  623. xdp_rxq_info_unreg(&rq->xdp_rxq);
  624. }
  625. }
  626. static int veth_open(struct net_device *dev)
  627. {
  628. struct veth_priv *priv = netdev_priv(dev);
  629. struct net_device *peer = rtnl_dereference(priv->peer);
  630. int err;
  631. if (!peer)
  632. return -ENOTCONN;
  633. if (priv->_xdp_prog) {
  634. err = veth_enable_xdp(dev);
  635. if (err)
  636. return err;
  637. }
  638. if (peer->flags & IFF_UP) {
  639. netif_carrier_on(dev);
  640. netif_carrier_on(peer);
  641. }
  642. return 0;
  643. }
  644. static int veth_close(struct net_device *dev)
  645. {
  646. struct veth_priv *priv = netdev_priv(dev);
  647. struct net_device *peer = rtnl_dereference(priv->peer);
  648. netif_carrier_off(dev);
  649. if (peer)
  650. netif_carrier_off(peer);
  651. if (priv->_xdp_prog)
  652. veth_disable_xdp(dev);
  653. return 0;
  654. }
  655. static int is_valid_veth_mtu(int mtu)
  656. {
  657. return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
  658. }
  659. static int veth_alloc_queues(struct net_device *dev)
  660. {
  661. struct veth_priv *priv = netdev_priv(dev);
  662. int i;
  663. priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
  664. if (!priv->rq)
  665. return -ENOMEM;
  666. for (i = 0; i < dev->num_rx_queues; i++)
  667. priv->rq[i].dev = dev;
  668. return 0;
  669. }
  670. static void veth_free_queues(struct net_device *dev)
  671. {
  672. struct veth_priv *priv = netdev_priv(dev);
  673. kfree(priv->rq);
  674. }
  675. static int veth_dev_init(struct net_device *dev)
  676. {
  677. int err;
  678. dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
  679. if (!dev->vstats)
  680. return -ENOMEM;
  681. err = veth_alloc_queues(dev);
  682. if (err) {
  683. free_percpu(dev->vstats);
  684. return err;
  685. }
  686. return 0;
  687. }
  688. static void veth_dev_free(struct net_device *dev)
  689. {
  690. veth_free_queues(dev);
  691. free_percpu(dev->vstats);
  692. }
  693. #ifdef CONFIG_NET_POLL_CONTROLLER
  694. static void veth_poll_controller(struct net_device *dev)
  695. {
  696. /* veth only receives frames when its peer sends one
  697. * Since it has nothing to do with disabling irqs, we are guaranteed
  698. * never to have pending data when we poll for it so
  699. * there is nothing to do here.
  700. *
  701. * We need this though so netpoll recognizes us as an interface that
  702. * supports polling, which enables bridge devices in virt setups to
  703. * still use netconsole
  704. */
  705. }
  706. #endif /* CONFIG_NET_POLL_CONTROLLER */
  707. static int veth_get_iflink(const struct net_device *dev)
  708. {
  709. struct veth_priv *priv = netdev_priv(dev);
  710. struct net_device *peer;
  711. int iflink;
  712. rcu_read_lock();
  713. peer = rcu_dereference(priv->peer);
  714. iflink = peer ? peer->ifindex : 0;
  715. rcu_read_unlock();
  716. return iflink;
  717. }
  718. static netdev_features_t veth_fix_features(struct net_device *dev,
  719. netdev_features_t features)
  720. {
  721. struct veth_priv *priv = netdev_priv(dev);
  722. struct net_device *peer;
  723. peer = rtnl_dereference(priv->peer);
  724. if (peer) {
  725. struct veth_priv *peer_priv = netdev_priv(peer);
  726. if (peer_priv->_xdp_prog)
  727. features &= ~NETIF_F_GSO_SOFTWARE;
  728. }
  729. return features;
  730. }
  731. static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
  732. {
  733. struct veth_priv *peer_priv, *priv = netdev_priv(dev);
  734. struct net_device *peer;
  735. if (new_hr < 0)
  736. new_hr = 0;
  737. rcu_read_lock();
  738. peer = rcu_dereference(priv->peer);
  739. if (unlikely(!peer))
  740. goto out;
  741. peer_priv = netdev_priv(peer);
  742. priv->requested_headroom = new_hr;
  743. new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
  744. dev->needed_headroom = new_hr;
  745. peer->needed_headroom = new_hr;
  746. out:
  747. rcu_read_unlock();
  748. }
  749. static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  750. struct netlink_ext_ack *extack)
  751. {
  752. struct veth_priv *priv = netdev_priv(dev);
  753. struct bpf_prog *old_prog;
  754. struct net_device *peer;
  755. unsigned int max_mtu;
  756. int err;
  757. old_prog = priv->_xdp_prog;
  758. priv->_xdp_prog = prog;
  759. peer = rtnl_dereference(priv->peer);
  760. if (prog) {
  761. if (!peer) {
  762. NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
  763. err = -ENOTCONN;
  764. goto err;
  765. }
  766. max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
  767. peer->hard_header_len -
  768. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  769. if (peer->mtu > max_mtu) {
  770. NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
  771. err = -ERANGE;
  772. goto err;
  773. }
  774. if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
  775. NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
  776. err = -ENOSPC;
  777. goto err;
  778. }
  779. if (dev->flags & IFF_UP) {
  780. err = veth_enable_xdp(dev);
  781. if (err) {
  782. NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
  783. goto err;
  784. }
  785. }
  786. if (!old_prog) {
  787. peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
  788. peer->max_mtu = max_mtu;
  789. }
  790. }
  791. if (old_prog) {
  792. if (!prog) {
  793. if (dev->flags & IFF_UP)
  794. veth_disable_xdp(dev);
  795. if (peer) {
  796. peer->hw_features |= NETIF_F_GSO_SOFTWARE;
  797. peer->max_mtu = ETH_MAX_MTU;
  798. }
  799. }
  800. bpf_prog_put(old_prog);
  801. }
  802. if ((!!old_prog ^ !!prog) && peer)
  803. netdev_update_features(peer);
  804. return 0;
  805. err:
  806. priv->_xdp_prog = old_prog;
  807. return err;
  808. }
  809. static u32 veth_xdp_query(struct net_device *dev)
  810. {
  811. struct veth_priv *priv = netdev_priv(dev);
  812. const struct bpf_prog *xdp_prog;
  813. xdp_prog = priv->_xdp_prog;
  814. if (xdp_prog)
  815. return xdp_prog->aux->id;
  816. return 0;
  817. }
  818. static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  819. {
  820. switch (xdp->command) {
  821. case XDP_SETUP_PROG:
  822. return veth_xdp_set(dev, xdp->prog, xdp->extack);
  823. case XDP_QUERY_PROG:
  824. xdp->prog_id = veth_xdp_query(dev);
  825. return 0;
  826. default:
  827. return -EINVAL;
  828. }
  829. }
  830. static const struct net_device_ops veth_netdev_ops = {
  831. .ndo_init = veth_dev_init,
  832. .ndo_open = veth_open,
  833. .ndo_stop = veth_close,
  834. .ndo_start_xmit = veth_xmit,
  835. .ndo_get_stats64 = veth_get_stats64,
  836. .ndo_set_rx_mode = veth_set_multicast_list,
  837. .ndo_set_mac_address = eth_mac_addr,
  838. #ifdef CONFIG_NET_POLL_CONTROLLER
  839. .ndo_poll_controller = veth_poll_controller,
  840. #endif
  841. .ndo_get_iflink = veth_get_iflink,
  842. .ndo_fix_features = veth_fix_features,
  843. .ndo_features_check = passthru_features_check,
  844. .ndo_set_rx_headroom = veth_set_rx_headroom,
  845. .ndo_bpf = veth_xdp,
  846. .ndo_xdp_xmit = veth_xdp_xmit,
  847. };
  848. #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
  849. NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
  850. NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
  851. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
  852. NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
  853. static void veth_setup(struct net_device *dev)
  854. {
  855. ether_setup(dev);
  856. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  857. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  858. dev->priv_flags |= IFF_NO_QUEUE;
  859. dev->priv_flags |= IFF_PHONY_HEADROOM;
  860. dev->netdev_ops = &veth_netdev_ops;
  861. dev->ethtool_ops = &veth_ethtool_ops;
  862. dev->features |= NETIF_F_LLTX;
  863. dev->features |= VETH_FEATURES;
  864. dev->vlan_features = dev->features &
  865. ~(NETIF_F_HW_VLAN_CTAG_TX |
  866. NETIF_F_HW_VLAN_STAG_TX |
  867. NETIF_F_HW_VLAN_CTAG_RX |
  868. NETIF_F_HW_VLAN_STAG_RX);
  869. dev->needs_free_netdev = true;
  870. dev->priv_destructor = veth_dev_free;
  871. dev->max_mtu = ETH_MAX_MTU;
  872. dev->hw_features = VETH_FEATURES;
  873. dev->hw_enc_features = VETH_FEATURES;
  874. dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
  875. }
  876. /*
  877. * netlink interface
  878. */
  879. static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
  880. struct netlink_ext_ack *extack)
  881. {
  882. if (tb[IFLA_ADDRESS]) {
  883. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  884. return -EINVAL;
  885. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  886. return -EADDRNOTAVAIL;
  887. }
  888. if (tb[IFLA_MTU]) {
  889. if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
  890. return -EINVAL;
  891. }
  892. return 0;
  893. }
  894. static struct rtnl_link_ops veth_link_ops;
  895. static int veth_newlink(struct net *src_net, struct net_device *dev,
  896. struct nlattr *tb[], struct nlattr *data[],
  897. struct netlink_ext_ack *extack)
  898. {
  899. int err;
  900. struct net_device *peer;
  901. struct veth_priv *priv;
  902. char ifname[IFNAMSIZ];
  903. struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
  904. unsigned char name_assign_type;
  905. struct ifinfomsg *ifmp;
  906. struct net *net;
  907. /*
  908. * create and register peer first
  909. */
  910. if (data != NULL && data[VETH_INFO_PEER] != NULL) {
  911. struct nlattr *nla_peer;
  912. nla_peer = data[VETH_INFO_PEER];
  913. ifmp = nla_data(nla_peer);
  914. err = rtnl_nla_parse_ifla(peer_tb,
  915. nla_data(nla_peer) + sizeof(struct ifinfomsg),
  916. nla_len(nla_peer) - sizeof(struct ifinfomsg),
  917. NULL);
  918. if (err < 0)
  919. return err;
  920. err = veth_validate(peer_tb, NULL, extack);
  921. if (err < 0)
  922. return err;
  923. tbp = peer_tb;
  924. } else {
  925. ifmp = NULL;
  926. tbp = tb;
  927. }
  928. if (ifmp && tbp[IFLA_IFNAME]) {
  929. nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
  930. name_assign_type = NET_NAME_USER;
  931. } else {
  932. snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
  933. name_assign_type = NET_NAME_ENUM;
  934. }
  935. net = rtnl_link_get_net(src_net, tbp);
  936. if (IS_ERR(net))
  937. return PTR_ERR(net);
  938. peer = rtnl_create_link(net, ifname, name_assign_type,
  939. &veth_link_ops, tbp);
  940. if (IS_ERR(peer)) {
  941. put_net(net);
  942. return PTR_ERR(peer);
  943. }
  944. if (!ifmp || !tbp[IFLA_ADDRESS])
  945. eth_hw_addr_random(peer);
  946. if (ifmp && (dev->ifindex != 0))
  947. peer->ifindex = ifmp->ifi_index;
  948. peer->gso_max_size = dev->gso_max_size;
  949. peer->gso_max_segs = dev->gso_max_segs;
  950. err = register_netdevice(peer);
  951. put_net(net);
  952. net = NULL;
  953. if (err < 0)
  954. goto err_register_peer;
  955. netif_carrier_off(peer);
  956. err = rtnl_configure_link(peer, ifmp);
  957. if (err < 0)
  958. goto err_configure_peer;
  959. /*
  960. * register dev last
  961. *
  962. * note, that since we've registered new device the dev's name
  963. * should be re-allocated
  964. */
  965. if (tb[IFLA_ADDRESS] == NULL)
  966. eth_hw_addr_random(dev);
  967. if (tb[IFLA_IFNAME])
  968. nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
  969. else
  970. snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
  971. err = register_netdevice(dev);
  972. if (err < 0)
  973. goto err_register_dev;
  974. netif_carrier_off(dev);
  975. /*
  976. * tie the deviced together
  977. */
  978. priv = netdev_priv(dev);
  979. rcu_assign_pointer(priv->peer, peer);
  980. priv = netdev_priv(peer);
  981. rcu_assign_pointer(priv->peer, dev);
  982. return 0;
  983. err_register_dev:
  984. /* nothing to do */
  985. err_configure_peer:
  986. unregister_netdevice(peer);
  987. return err;
  988. err_register_peer:
  989. free_netdev(peer);
  990. return err;
  991. }
  992. static void veth_dellink(struct net_device *dev, struct list_head *head)
  993. {
  994. struct veth_priv *priv;
  995. struct net_device *peer;
  996. priv = netdev_priv(dev);
  997. peer = rtnl_dereference(priv->peer);
  998. /* Note : dellink() is called from default_device_exit_batch(),
  999. * before a rcu_synchronize() point. The devices are guaranteed
  1000. * not being freed before one RCU grace period.
  1001. */
  1002. RCU_INIT_POINTER(priv->peer, NULL);
  1003. unregister_netdevice_queue(dev, head);
  1004. if (peer) {
  1005. priv = netdev_priv(peer);
  1006. RCU_INIT_POINTER(priv->peer, NULL);
  1007. unregister_netdevice_queue(peer, head);
  1008. }
  1009. }
  1010. static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
  1011. [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
  1012. };
  1013. static struct net *veth_get_link_net(const struct net_device *dev)
  1014. {
  1015. struct veth_priv *priv = netdev_priv(dev);
  1016. struct net_device *peer = rtnl_dereference(priv->peer);
  1017. return peer ? dev_net(peer) : dev_net(dev);
  1018. }
  1019. static struct rtnl_link_ops veth_link_ops = {
  1020. .kind = DRV_NAME,
  1021. .priv_size = sizeof(struct veth_priv),
  1022. .setup = veth_setup,
  1023. .validate = veth_validate,
  1024. .newlink = veth_newlink,
  1025. .dellink = veth_dellink,
  1026. .policy = veth_policy,
  1027. .maxtype = VETH_INFO_MAX,
  1028. .get_link_net = veth_get_link_net,
  1029. };
  1030. /*
  1031. * init/fini
  1032. */
  1033. static __init int veth_init(void)
  1034. {
  1035. return rtnl_link_register(&veth_link_ops);
  1036. }
  1037. static __exit void veth_exit(void)
  1038. {
  1039. rtnl_link_unregister(&veth_link_ops);
  1040. }
  1041. module_init(veth_init);
  1042. module_exit(veth_exit);
  1043. MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
  1044. MODULE_LICENSE("GPL v2");
  1045. MODULE_ALIAS_RTNL_LINK(DRV_NAME);