devmap.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
  3. */
  4. /* Devmaps primary use is as a backend map for XDP BPF helper call
  5. * bpf_redirect_map(). Because XDP is mostly concerned with performance we
  6. * spent some effort to ensure the datapath with redirect maps does not use
  7. * any locking. This is a quick note on the details.
  8. *
  9. * We have three possible paths to get into the devmap control plane bpf
  10. * syscalls, bpf programs, and driver side xmit/flush operations. A bpf syscall
  11. * will invoke an update, delete, or lookup operation. To ensure updates and
  12. * deletes appear atomic from the datapath side xchg() is used to modify the
  13. * netdev_map array. Then because the datapath does a lookup into the netdev_map
  14. * array (read-only) from an RCU critical section we use call_rcu() to wait for
  15. * an rcu grace period before free'ing the old data structures. This ensures the
  16. * datapath always has a valid copy. However, the datapath does a "flush"
  17. * operation that pushes any pending packets in the driver outside the RCU
  18. * critical section. Each bpf_dtab_netdev tracks these pending operations using
  19. * a per-cpu flush list. The bpf_dtab_netdev object will not be destroyed until
  20. * this list is empty, indicating outstanding flush operations have completed.
  21. *
  22. * BPF syscalls may race with BPF program calls on any of the update, delete
  23. * or lookup operations. As noted above the xchg() operation also keep the
  24. * netdev_map consistent in this case. From the devmap side BPF programs
  25. * calling into these operations are the same as multiple user space threads
  26. * making system calls.
  27. *
  28. * Finally, any of the above may race with a netdev_unregister notifier. The
  29. * unregister notifier must search for net devices in the map structure that
  30. * contain a reference to the net device and remove them. This is a two step
  31. * process (a) dereference the bpf_dtab_netdev object in netdev_map and (b)
  32. * check to see if the ifindex is the same as the net_device being removed.
  33. * When removing the dev a cmpxchg() is used to ensure the correct dev is
  34. * removed, in the case of a concurrent update or delete operation it is
  35. * possible that the initially referenced dev is no longer in the map. As the
  36. * notifier hook walks the map we know that new dev references can not be
  37. * added by the user because core infrastructure ensures dev_get_by_index()
  38. * calls will fail at this point.
  39. *
  40. * The devmap_hash type is a map type which interprets keys as ifindexes and
  41. * indexes these using a hashmap. This allows maps that use ifindex as key to be
  42. * densely packed instead of having holes in the lookup array for unused
  43. * ifindexes. The setup and packet enqueue/send code is shared between the two
  44. * types of devmap; only the lookup and insertion is different.
  45. */
  46. #include <linux/bpf.h>
  47. #include <net/xdp.h>
  48. #include <linux/filter.h>
  49. #include <trace/events/xdp.h>
  50. #include <linux/btf_ids.h>
  51. #define DEV_CREATE_FLAG_MASK \
  52. (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
  53. struct xdp_dev_bulk_queue {
  54. struct xdp_frame *q[DEV_MAP_BULK_SIZE];
  55. struct list_head flush_node;
  56. struct net_device *dev;
  57. struct net_device *dev_rx;
  58. struct bpf_prog *xdp_prog;
  59. unsigned int count;
  60. };
  61. struct bpf_dtab_netdev {
  62. struct net_device *dev; /* must be first member, due to tracepoint */
  63. struct hlist_node index_hlist;
  64. struct bpf_prog *xdp_prog;
  65. struct rcu_head rcu;
  66. unsigned int idx;
  67. struct bpf_devmap_val val;
  68. };
  69. struct bpf_dtab {
  70. struct bpf_map map;
  71. struct bpf_dtab_netdev __rcu **netdev_map; /* DEVMAP type only */
  72. struct list_head list;
  73. /* these are only used for DEVMAP_HASH type maps */
  74. struct hlist_head *dev_index_head;
  75. spinlock_t index_lock;
  76. unsigned int items;
  77. u32 n_buckets;
  78. };
  79. static DEFINE_SPINLOCK(dev_map_lock);
  80. static LIST_HEAD(dev_map_list);
  81. static struct hlist_head *dev_map_create_hash(unsigned int entries,
  82. int numa_node)
  83. {
  84. int i;
  85. struct hlist_head *hash;
  86. hash = bpf_map_area_alloc((u64) entries * sizeof(*hash), numa_node);
  87. if (hash != NULL)
  88. for (i = 0; i < entries; i++)
  89. INIT_HLIST_HEAD(&hash[i]);
  90. return hash;
  91. }
  92. static inline struct hlist_head *dev_map_index_hash(struct bpf_dtab *dtab,
  93. int idx)
  94. {
  95. return &dtab->dev_index_head[idx & (dtab->n_buckets - 1)];
  96. }
  97. static int dev_map_alloc_check(union bpf_attr *attr)
  98. {
  99. u32 valsize = attr->value_size;
  100. /* check sanity of attributes. 2 value sizes supported:
  101. * 4 bytes: ifindex
  102. * 8 bytes: ifindex + prog fd
  103. */
  104. if (attr->max_entries == 0 || attr->key_size != 4 ||
  105. (valsize != offsetofend(struct bpf_devmap_val, ifindex) &&
  106. valsize != offsetofend(struct bpf_devmap_val, bpf_prog.fd)) ||
  107. attr->map_flags & ~DEV_CREATE_FLAG_MASK)
  108. return -EINVAL;
  109. if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
  110. /* Hash table size must be power of 2; roundup_pow_of_two()
  111. * can overflow into UB on 32-bit arches
  112. */
  113. if (attr->max_entries > 1UL << 31)
  114. return -EINVAL;
  115. }
  116. return 0;
  117. }
  118. static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
  119. {
  120. /* Lookup returns a pointer straight to dev->ifindex, so make sure the
  121. * verifier prevents writes from the BPF side
  122. */
  123. attr->map_flags |= BPF_F_RDONLY_PROG;
  124. bpf_map_init_from_attr(&dtab->map, attr);
  125. if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
  126. /* Hash table size must be power of 2 */
  127. dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries);
  128. dtab->dev_index_head = dev_map_create_hash(dtab->n_buckets,
  129. dtab->map.numa_node);
  130. if (!dtab->dev_index_head)
  131. return -ENOMEM;
  132. spin_lock_init(&dtab->index_lock);
  133. } else {
  134. dtab->netdev_map = bpf_map_area_alloc((u64) dtab->map.max_entries *
  135. sizeof(struct bpf_dtab_netdev *),
  136. dtab->map.numa_node);
  137. if (!dtab->netdev_map)
  138. return -ENOMEM;
  139. }
  140. return 0;
  141. }
  142. static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
  143. {
  144. struct bpf_dtab *dtab;
  145. int err;
  146. dtab = bpf_map_area_alloc(sizeof(*dtab), NUMA_NO_NODE);
  147. if (!dtab)
  148. return ERR_PTR(-ENOMEM);
  149. err = dev_map_init_map(dtab, attr);
  150. if (err) {
  151. bpf_map_area_free(dtab);
  152. return ERR_PTR(err);
  153. }
  154. spin_lock(&dev_map_lock);
  155. list_add_tail_rcu(&dtab->list, &dev_map_list);
  156. spin_unlock(&dev_map_lock);
  157. return &dtab->map;
  158. }
  159. static void dev_map_free(struct bpf_map *map)
  160. {
  161. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  162. u32 i;
  163. /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  164. * so the programs (can be more than one that used this map) were
  165. * disconnected from events. The following synchronize_rcu() guarantees
  166. * both rcu read critical sections complete and waits for
  167. * preempt-disable regions (NAPI being the relevant context here) so we
  168. * are certain there will be no further reads against the netdev_map and
  169. * all flush operations are complete. Flush operations can only be done
  170. * from NAPI context for this reason.
  171. */
  172. spin_lock(&dev_map_lock);
  173. list_del_rcu(&dtab->list);
  174. spin_unlock(&dev_map_lock);
  175. /* bpf_redirect_info->map is assigned in __bpf_xdp_redirect_map()
  176. * during NAPI callback and cleared after the XDP redirect. There is no
  177. * explicit RCU read section which protects bpf_redirect_info->map but
  178. * local_bh_disable() also marks the beginning an RCU section. This
  179. * makes the complete softirq callback RCU protected. Thus after
  180. * following synchronize_rcu() there no bpf_redirect_info->map == map
  181. * assignment.
  182. */
  183. synchronize_rcu();
  184. /* Make sure prior __dev_map_entry_free() have completed. */
  185. rcu_barrier();
  186. if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
  187. for (i = 0; i < dtab->n_buckets; i++) {
  188. struct bpf_dtab_netdev *dev;
  189. struct hlist_head *head;
  190. struct hlist_node *next;
  191. head = dev_map_index_hash(dtab, i);
  192. hlist_for_each_entry_safe(dev, next, head, index_hlist) {
  193. hlist_del_rcu(&dev->index_hlist);
  194. if (dev->xdp_prog)
  195. bpf_prog_put(dev->xdp_prog);
  196. dev_put(dev->dev);
  197. kfree(dev);
  198. }
  199. }
  200. bpf_map_area_free(dtab->dev_index_head);
  201. } else {
  202. for (i = 0; i < dtab->map.max_entries; i++) {
  203. struct bpf_dtab_netdev *dev;
  204. dev = rcu_dereference_raw(dtab->netdev_map[i]);
  205. if (!dev)
  206. continue;
  207. if (dev->xdp_prog)
  208. bpf_prog_put(dev->xdp_prog);
  209. dev_put(dev->dev);
  210. kfree(dev);
  211. }
  212. bpf_map_area_free(dtab->netdev_map);
  213. }
  214. bpf_map_area_free(dtab);
  215. }
  216. static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  217. {
  218. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  219. u32 index = key ? *(u32 *)key : U32_MAX;
  220. u32 *next = next_key;
  221. if (index >= dtab->map.max_entries) {
  222. *next = 0;
  223. return 0;
  224. }
  225. if (index == dtab->map.max_entries - 1)
  226. return -ENOENT;
  227. *next = index + 1;
  228. return 0;
  229. }
  230. /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
  231. * by local_bh_disable() (from XDP calls inside NAPI). The
  232. * rcu_read_lock_bh_held() below makes lockdep accept both.
  233. */
  234. static void *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key)
  235. {
  236. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  237. struct hlist_head *head = dev_map_index_hash(dtab, key);
  238. struct bpf_dtab_netdev *dev;
  239. hlist_for_each_entry_rcu(dev, head, index_hlist,
  240. lockdep_is_held(&dtab->index_lock))
  241. if (dev->idx == key)
  242. return dev;
  243. return NULL;
  244. }
  245. static int dev_map_hash_get_next_key(struct bpf_map *map, void *key,
  246. void *next_key)
  247. {
  248. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  249. u32 idx, *next = next_key;
  250. struct bpf_dtab_netdev *dev, *next_dev;
  251. struct hlist_head *head;
  252. int i = 0;
  253. if (!key)
  254. goto find_first;
  255. idx = *(u32 *)key;
  256. dev = __dev_map_hash_lookup_elem(map, idx);
  257. if (!dev)
  258. goto find_first;
  259. next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&dev->index_hlist)),
  260. struct bpf_dtab_netdev, index_hlist);
  261. if (next_dev) {
  262. *next = next_dev->idx;
  263. return 0;
  264. }
  265. i = idx & (dtab->n_buckets - 1);
  266. i++;
  267. find_first:
  268. for (; i < dtab->n_buckets; i++) {
  269. head = dev_map_index_hash(dtab, i);
  270. next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
  271. struct bpf_dtab_netdev,
  272. index_hlist);
  273. if (next_dev) {
  274. *next = next_dev->idx;
  275. return 0;
  276. }
  277. }
  278. return -ENOENT;
  279. }
  280. static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,
  281. struct xdp_frame **frames, int n,
  282. struct net_device *tx_dev,
  283. struct net_device *rx_dev)
  284. {
  285. struct xdp_txq_info txq = { .dev = tx_dev };
  286. struct xdp_rxq_info rxq = { .dev = rx_dev };
  287. struct xdp_buff xdp;
  288. int i, nframes = 0;
  289. for (i = 0; i < n; i++) {
  290. struct xdp_frame *xdpf = frames[i];
  291. u32 act;
  292. int err;
  293. xdp_convert_frame_to_buff(xdpf, &xdp);
  294. xdp.txq = &txq;
  295. xdp.rxq = &rxq;
  296. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  297. switch (act) {
  298. case XDP_PASS:
  299. err = xdp_update_frame_from_buff(&xdp, xdpf);
  300. if (unlikely(err < 0))
  301. xdp_return_frame_rx_napi(xdpf);
  302. else
  303. frames[nframes++] = xdpf;
  304. break;
  305. default:
  306. bpf_warn_invalid_xdp_action(NULL, xdp_prog, act);
  307. fallthrough;
  308. case XDP_ABORTED:
  309. trace_xdp_exception(tx_dev, xdp_prog, act);
  310. fallthrough;
  311. case XDP_DROP:
  312. xdp_return_frame_rx_napi(xdpf);
  313. break;
  314. }
  315. }
  316. return nframes; /* sent frames count */
  317. }
  318. static void bq_xmit_all(struct xdp_dev_bulk_queue *bq, u32 flags)
  319. {
  320. struct net_device *dev = bq->dev;
  321. unsigned int cnt = bq->count;
  322. int sent = 0, err = 0;
  323. int to_send = cnt;
  324. int i;
  325. if (unlikely(!cnt))
  326. return;
  327. for (i = 0; i < cnt; i++) {
  328. struct xdp_frame *xdpf = bq->q[i];
  329. prefetch(xdpf);
  330. }
  331. if (bq->xdp_prog) {
  332. to_send = dev_map_bpf_prog_run(bq->xdp_prog, bq->q, cnt, dev, bq->dev_rx);
  333. if (!to_send)
  334. goto out;
  335. }
  336. sent = dev->netdev_ops->ndo_xdp_xmit(dev, to_send, bq->q, flags);
  337. if (sent < 0) {
  338. /* If ndo_xdp_xmit fails with an errno, no frames have
  339. * been xmit'ed.
  340. */
  341. err = sent;
  342. sent = 0;
  343. }
  344. /* If not all frames have been transmitted, it is our
  345. * responsibility to free them
  346. */
  347. for (i = sent; unlikely(i < to_send); i++)
  348. xdp_return_frame_rx_napi(bq->q[i]);
  349. out:
  350. bq->count = 0;
  351. trace_xdp_devmap_xmit(bq->dev_rx, dev, sent, cnt - sent, err);
  352. }
  353. /* __dev_flush is called from xdp_do_flush() which _must_ be signalled from the
  354. * driver before returning from its napi->poll() routine. See the comment above
  355. * xdp_do_flush() in filter.c.
  356. */
  357. void __dev_flush(struct list_head *flush_list)
  358. {
  359. struct xdp_dev_bulk_queue *bq, *tmp;
  360. list_for_each_entry_safe(bq, tmp, flush_list, flush_node) {
  361. bq_xmit_all(bq, XDP_XMIT_FLUSH);
  362. bq->dev_rx = NULL;
  363. bq->xdp_prog = NULL;
  364. __list_del_clearprev(&bq->flush_node);
  365. }
  366. }
  367. /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
  368. * by local_bh_disable() (from XDP calls inside NAPI). The
  369. * rcu_read_lock_bh_held() below makes lockdep accept both.
  370. */
  371. static void *__dev_map_lookup_elem(struct bpf_map *map, u32 key)
  372. {
  373. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  374. struct bpf_dtab_netdev *obj;
  375. if (key >= map->max_entries)
  376. return NULL;
  377. obj = rcu_dereference_check(dtab->netdev_map[key],
  378. rcu_read_lock_bh_held());
  379. return obj;
  380. }
  381. /* Runs in NAPI, i.e., softirq under local_bh_disable(). Thus, safe percpu
  382. * variable access, and map elements stick around. See comment above
  383. * xdp_do_flush() in filter.c.
  384. */
  385. static void bq_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
  386. struct net_device *dev_rx, struct bpf_prog *xdp_prog)
  387. {
  388. struct xdp_dev_bulk_queue *bq = this_cpu_ptr(dev->xdp_bulkq);
  389. if (unlikely(bq->count == DEV_MAP_BULK_SIZE))
  390. bq_xmit_all(bq, 0);
  391. /* Ingress dev_rx will be the same for all xdp_frame's in
  392. * bulk_queue, because bq stored per-CPU and must be flushed
  393. * from net_device drivers NAPI func end.
  394. *
  395. * Do the same with xdp_prog and flush_list since these fields
  396. * are only ever modified together.
  397. */
  398. if (!bq->dev_rx) {
  399. struct list_head *flush_list = bpf_net_ctx_get_dev_flush_list();
  400. bq->dev_rx = dev_rx;
  401. bq->xdp_prog = xdp_prog;
  402. list_add(&bq->flush_node, flush_list);
  403. }
  404. bq->q[bq->count++] = xdpf;
  405. }
  406. static inline int __xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
  407. struct net_device *dev_rx,
  408. struct bpf_prog *xdp_prog)
  409. {
  410. int err;
  411. if (!(dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT))
  412. return -EOPNOTSUPP;
  413. if (unlikely(!(dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT_SG) &&
  414. xdp_frame_has_frags(xdpf)))
  415. return -EOPNOTSUPP;
  416. err = xdp_ok_fwd_dev(dev, xdp_get_frame_len(xdpf));
  417. if (unlikely(err))
  418. return err;
  419. bq_enqueue(dev, xdpf, dev_rx, xdp_prog);
  420. return 0;
  421. }
  422. static u32 dev_map_bpf_prog_run_skb(struct sk_buff *skb, struct bpf_dtab_netdev *dst)
  423. {
  424. struct xdp_txq_info txq = { .dev = dst->dev };
  425. struct xdp_buff xdp;
  426. u32 act;
  427. if (!dst->xdp_prog)
  428. return XDP_PASS;
  429. __skb_pull(skb, skb->mac_len);
  430. xdp.txq = &txq;
  431. act = bpf_prog_run_generic_xdp(skb, &xdp, dst->xdp_prog);
  432. switch (act) {
  433. case XDP_PASS:
  434. __skb_push(skb, skb->mac_len);
  435. break;
  436. default:
  437. bpf_warn_invalid_xdp_action(NULL, dst->xdp_prog, act);
  438. fallthrough;
  439. case XDP_ABORTED:
  440. trace_xdp_exception(dst->dev, dst->xdp_prog, act);
  441. fallthrough;
  442. case XDP_DROP:
  443. kfree_skb(skb);
  444. break;
  445. }
  446. return act;
  447. }
  448. int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
  449. struct net_device *dev_rx)
  450. {
  451. return __xdp_enqueue(dev, xdpf, dev_rx, NULL);
  452. }
  453. int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
  454. struct net_device *dev_rx)
  455. {
  456. struct net_device *dev = dst->dev;
  457. return __xdp_enqueue(dev, xdpf, dev_rx, dst->xdp_prog);
  458. }
  459. static bool is_valid_dst(struct bpf_dtab_netdev *obj, struct xdp_frame *xdpf)
  460. {
  461. if (!obj)
  462. return false;
  463. if (!(obj->dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT))
  464. return false;
  465. if (unlikely(!(obj->dev->xdp_features & NETDEV_XDP_ACT_NDO_XMIT_SG) &&
  466. xdp_frame_has_frags(xdpf)))
  467. return false;
  468. if (xdp_ok_fwd_dev(obj->dev, xdp_get_frame_len(xdpf)))
  469. return false;
  470. return true;
  471. }
  472. static int dev_map_enqueue_clone(struct bpf_dtab_netdev *obj,
  473. struct net_device *dev_rx,
  474. struct xdp_frame *xdpf)
  475. {
  476. struct xdp_frame *nxdpf;
  477. nxdpf = xdpf_clone(xdpf);
  478. if (!nxdpf)
  479. return -ENOMEM;
  480. bq_enqueue(obj->dev, nxdpf, dev_rx, obj->xdp_prog);
  481. return 0;
  482. }
  483. static inline bool is_ifindex_excluded(int *excluded, int num_excluded, int ifindex)
  484. {
  485. while (num_excluded--) {
  486. if (ifindex == excluded[num_excluded])
  487. return true;
  488. }
  489. return false;
  490. }
  491. /* Get ifindex of each upper device. 'indexes' must be able to hold at
  492. * least MAX_NEST_DEV elements.
  493. * Returns the number of ifindexes added.
  494. */
  495. static int get_upper_ifindexes(struct net_device *dev, int *indexes)
  496. {
  497. struct net_device *upper;
  498. struct list_head *iter;
  499. int n = 0;
  500. netdev_for_each_upper_dev_rcu(dev, upper, iter) {
  501. indexes[n++] = upper->ifindex;
  502. }
  503. return n;
  504. }
  505. int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
  506. struct bpf_map *map, bool exclude_ingress)
  507. {
  508. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  509. struct bpf_dtab_netdev *dst, *last_dst = NULL;
  510. int excluded_devices[1+MAX_NEST_DEV];
  511. struct hlist_head *head;
  512. int num_excluded = 0;
  513. unsigned int i;
  514. int err;
  515. if (exclude_ingress) {
  516. num_excluded = get_upper_ifindexes(dev_rx, excluded_devices);
  517. excluded_devices[num_excluded++] = dev_rx->ifindex;
  518. }
  519. if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
  520. for (i = 0; i < map->max_entries; i++) {
  521. dst = rcu_dereference_check(dtab->netdev_map[i],
  522. rcu_read_lock_bh_held());
  523. if (!is_valid_dst(dst, xdpf))
  524. continue;
  525. if (is_ifindex_excluded(excluded_devices, num_excluded, dst->dev->ifindex))
  526. continue;
  527. /* we only need n-1 clones; last_dst enqueued below */
  528. if (!last_dst) {
  529. last_dst = dst;
  530. continue;
  531. }
  532. err = dev_map_enqueue_clone(last_dst, dev_rx, xdpf);
  533. if (err)
  534. return err;
  535. last_dst = dst;
  536. }
  537. } else { /* BPF_MAP_TYPE_DEVMAP_HASH */
  538. for (i = 0; i < dtab->n_buckets; i++) {
  539. head = dev_map_index_hash(dtab, i);
  540. hlist_for_each_entry_rcu(dst, head, index_hlist,
  541. lockdep_is_held(&dtab->index_lock)) {
  542. if (!is_valid_dst(dst, xdpf))
  543. continue;
  544. if (is_ifindex_excluded(excluded_devices, num_excluded,
  545. dst->dev->ifindex))
  546. continue;
  547. /* we only need n-1 clones; last_dst enqueued below */
  548. if (!last_dst) {
  549. last_dst = dst;
  550. continue;
  551. }
  552. err = dev_map_enqueue_clone(last_dst, dev_rx, xdpf);
  553. if (err)
  554. return err;
  555. last_dst = dst;
  556. }
  557. }
  558. }
  559. /* consume the last copy of the frame */
  560. if (last_dst)
  561. bq_enqueue(last_dst->dev, xdpf, dev_rx, last_dst->xdp_prog);
  562. else
  563. xdp_return_frame_rx_napi(xdpf); /* dtab is empty */
  564. return 0;
  565. }
  566. int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
  567. struct bpf_prog *xdp_prog)
  568. {
  569. int err;
  570. err = xdp_ok_fwd_dev(dst->dev, skb->len);
  571. if (unlikely(err))
  572. return err;
  573. /* Redirect has already succeeded semantically at this point, so we just
  574. * return 0 even if packet is dropped. Helper below takes care of
  575. * freeing skb.
  576. */
  577. if (dev_map_bpf_prog_run_skb(skb, dst) != XDP_PASS)
  578. return 0;
  579. skb->dev = dst->dev;
  580. generic_xdp_tx(skb, xdp_prog);
  581. return 0;
  582. }
  583. static int dev_map_redirect_clone(struct bpf_dtab_netdev *dst,
  584. struct sk_buff *skb,
  585. struct bpf_prog *xdp_prog)
  586. {
  587. struct sk_buff *nskb;
  588. int err;
  589. nskb = skb_clone(skb, GFP_ATOMIC);
  590. if (!nskb)
  591. return -ENOMEM;
  592. err = dev_map_generic_redirect(dst, nskb, xdp_prog);
  593. if (unlikely(err)) {
  594. consume_skb(nskb);
  595. return err;
  596. }
  597. return 0;
  598. }
  599. int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
  600. struct bpf_prog *xdp_prog, struct bpf_map *map,
  601. bool exclude_ingress)
  602. {
  603. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  604. struct bpf_dtab_netdev *dst, *last_dst = NULL;
  605. int excluded_devices[1+MAX_NEST_DEV];
  606. struct hlist_head *head;
  607. struct hlist_node *next;
  608. int num_excluded = 0;
  609. unsigned int i;
  610. int err;
  611. if (exclude_ingress) {
  612. num_excluded = get_upper_ifindexes(dev, excluded_devices);
  613. excluded_devices[num_excluded++] = dev->ifindex;
  614. }
  615. if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
  616. for (i = 0; i < map->max_entries; i++) {
  617. dst = rcu_dereference_check(dtab->netdev_map[i],
  618. rcu_read_lock_bh_held());
  619. if (!dst)
  620. continue;
  621. if (is_ifindex_excluded(excluded_devices, num_excluded, dst->dev->ifindex))
  622. continue;
  623. /* we only need n-1 clones; last_dst enqueued below */
  624. if (!last_dst) {
  625. last_dst = dst;
  626. continue;
  627. }
  628. err = dev_map_redirect_clone(last_dst, skb, xdp_prog);
  629. if (err)
  630. return err;
  631. last_dst = dst;
  632. }
  633. } else { /* BPF_MAP_TYPE_DEVMAP_HASH */
  634. for (i = 0; i < dtab->n_buckets; i++) {
  635. head = dev_map_index_hash(dtab, i);
  636. hlist_for_each_entry_safe(dst, next, head, index_hlist) {
  637. if (is_ifindex_excluded(excluded_devices, num_excluded,
  638. dst->dev->ifindex))
  639. continue;
  640. /* we only need n-1 clones; last_dst enqueued below */
  641. if (!last_dst) {
  642. last_dst = dst;
  643. continue;
  644. }
  645. err = dev_map_redirect_clone(last_dst, skb, xdp_prog);
  646. if (err)
  647. return err;
  648. last_dst = dst;
  649. }
  650. }
  651. }
  652. /* consume the first skb and return */
  653. if (last_dst)
  654. return dev_map_generic_redirect(last_dst, skb, xdp_prog);
  655. /* dtab is empty */
  656. consume_skb(skb);
  657. return 0;
  658. }
  659. static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
  660. {
  661. struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key);
  662. return obj ? &obj->val : NULL;
  663. }
  664. static void *dev_map_hash_lookup_elem(struct bpf_map *map, void *key)
  665. {
  666. struct bpf_dtab_netdev *obj = __dev_map_hash_lookup_elem(map,
  667. *(u32 *)key);
  668. return obj ? &obj->val : NULL;
  669. }
  670. static void __dev_map_entry_free(struct rcu_head *rcu)
  671. {
  672. struct bpf_dtab_netdev *dev;
  673. dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
  674. if (dev->xdp_prog)
  675. bpf_prog_put(dev->xdp_prog);
  676. dev_put(dev->dev);
  677. kfree(dev);
  678. }
  679. static long dev_map_delete_elem(struct bpf_map *map, void *key)
  680. {
  681. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  682. struct bpf_dtab_netdev *old_dev;
  683. u32 k = *(u32 *)key;
  684. if (k >= map->max_entries)
  685. return -EINVAL;
  686. old_dev = unrcu_pointer(xchg(&dtab->netdev_map[k], NULL));
  687. if (old_dev) {
  688. call_rcu(&old_dev->rcu, __dev_map_entry_free);
  689. atomic_dec((atomic_t *)&dtab->items);
  690. }
  691. return 0;
  692. }
  693. static long dev_map_hash_delete_elem(struct bpf_map *map, void *key)
  694. {
  695. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  696. struct bpf_dtab_netdev *old_dev;
  697. u32 k = *(u32 *)key;
  698. unsigned long flags;
  699. int ret = -ENOENT;
  700. spin_lock_irqsave(&dtab->index_lock, flags);
  701. old_dev = __dev_map_hash_lookup_elem(map, k);
  702. if (old_dev) {
  703. dtab->items--;
  704. hlist_del_init_rcu(&old_dev->index_hlist);
  705. call_rcu(&old_dev->rcu, __dev_map_entry_free);
  706. ret = 0;
  707. }
  708. spin_unlock_irqrestore(&dtab->index_lock, flags);
  709. return ret;
  710. }
  711. static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net,
  712. struct bpf_dtab *dtab,
  713. struct bpf_devmap_val *val,
  714. unsigned int idx)
  715. {
  716. struct bpf_prog *prog = NULL;
  717. struct bpf_dtab_netdev *dev;
  718. dev = bpf_map_kmalloc_node(&dtab->map, sizeof(*dev),
  719. GFP_NOWAIT | __GFP_NOWARN,
  720. dtab->map.numa_node);
  721. if (!dev)
  722. return ERR_PTR(-ENOMEM);
  723. dev->dev = dev_get_by_index(net, val->ifindex);
  724. if (!dev->dev)
  725. goto err_out;
  726. if (val->bpf_prog.fd > 0) {
  727. prog = bpf_prog_get_type_dev(val->bpf_prog.fd,
  728. BPF_PROG_TYPE_XDP, false);
  729. if (IS_ERR(prog))
  730. goto err_put_dev;
  731. if (prog->expected_attach_type != BPF_XDP_DEVMAP ||
  732. !bpf_prog_map_compatible(&dtab->map, prog))
  733. goto err_put_prog;
  734. }
  735. dev->idx = idx;
  736. if (prog) {
  737. dev->xdp_prog = prog;
  738. dev->val.bpf_prog.id = prog->aux->id;
  739. } else {
  740. dev->xdp_prog = NULL;
  741. dev->val.bpf_prog.id = 0;
  742. }
  743. dev->val.ifindex = val->ifindex;
  744. return dev;
  745. err_put_prog:
  746. bpf_prog_put(prog);
  747. err_put_dev:
  748. dev_put(dev->dev);
  749. err_out:
  750. kfree(dev);
  751. return ERR_PTR(-EINVAL);
  752. }
  753. static long __dev_map_update_elem(struct net *net, struct bpf_map *map,
  754. void *key, void *value, u64 map_flags)
  755. {
  756. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  757. struct bpf_dtab_netdev *dev, *old_dev;
  758. struct bpf_devmap_val val = {};
  759. u32 i = *(u32 *)key;
  760. if (unlikely(map_flags > BPF_EXIST))
  761. return -EINVAL;
  762. if (unlikely(i >= dtab->map.max_entries))
  763. return -E2BIG;
  764. if (unlikely(map_flags == BPF_NOEXIST))
  765. return -EEXIST;
  766. /* already verified value_size <= sizeof val */
  767. memcpy(&val, value, map->value_size);
  768. if (!val.ifindex) {
  769. dev = NULL;
  770. /* can not specify fd if ifindex is 0 */
  771. if (val.bpf_prog.fd > 0)
  772. return -EINVAL;
  773. } else {
  774. dev = __dev_map_alloc_node(net, dtab, &val, i);
  775. if (IS_ERR(dev))
  776. return PTR_ERR(dev);
  777. }
  778. /* Use call_rcu() here to ensure rcu critical sections have completed
  779. * Remembering the driver side flush operation will happen before the
  780. * net device is removed.
  781. */
  782. old_dev = unrcu_pointer(xchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev)));
  783. if (old_dev)
  784. call_rcu(&old_dev->rcu, __dev_map_entry_free);
  785. else
  786. atomic_inc((atomic_t *)&dtab->items);
  787. return 0;
  788. }
  789. static long dev_map_update_elem(struct bpf_map *map, void *key, void *value,
  790. u64 map_flags)
  791. {
  792. return __dev_map_update_elem(current->nsproxy->net_ns,
  793. map, key, value, map_flags);
  794. }
  795. static long __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
  796. void *key, void *value, u64 map_flags)
  797. {
  798. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  799. struct bpf_dtab_netdev *dev, *old_dev;
  800. struct bpf_devmap_val val = {};
  801. u32 idx = *(u32 *)key;
  802. unsigned long flags;
  803. int err = -EEXIST;
  804. /* already verified value_size <= sizeof val */
  805. memcpy(&val, value, map->value_size);
  806. if (unlikely(map_flags > BPF_EXIST || !val.ifindex))
  807. return -EINVAL;
  808. spin_lock_irqsave(&dtab->index_lock, flags);
  809. old_dev = __dev_map_hash_lookup_elem(map, idx);
  810. if (old_dev && (map_flags & BPF_NOEXIST))
  811. goto out_err;
  812. dev = __dev_map_alloc_node(net, dtab, &val, idx);
  813. if (IS_ERR(dev)) {
  814. err = PTR_ERR(dev);
  815. goto out_err;
  816. }
  817. if (old_dev) {
  818. hlist_del_rcu(&old_dev->index_hlist);
  819. } else {
  820. if (dtab->items >= dtab->map.max_entries) {
  821. spin_unlock_irqrestore(&dtab->index_lock, flags);
  822. call_rcu(&dev->rcu, __dev_map_entry_free);
  823. return -E2BIG;
  824. }
  825. dtab->items++;
  826. }
  827. hlist_add_head_rcu(&dev->index_hlist,
  828. dev_map_index_hash(dtab, idx));
  829. spin_unlock_irqrestore(&dtab->index_lock, flags);
  830. if (old_dev)
  831. call_rcu(&old_dev->rcu, __dev_map_entry_free);
  832. return 0;
  833. out_err:
  834. spin_unlock_irqrestore(&dtab->index_lock, flags);
  835. return err;
  836. }
  837. static long dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,
  838. u64 map_flags)
  839. {
  840. return __dev_map_hash_update_elem(current->nsproxy->net_ns,
  841. map, key, value, map_flags);
  842. }
  843. static long dev_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags)
  844. {
  845. return __bpf_xdp_redirect_map(map, ifindex, flags,
  846. BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS,
  847. __dev_map_lookup_elem);
  848. }
  849. static long dev_hash_map_redirect(struct bpf_map *map, u64 ifindex, u64 flags)
  850. {
  851. return __bpf_xdp_redirect_map(map, ifindex, flags,
  852. BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS,
  853. __dev_map_hash_lookup_elem);
  854. }
  855. static u64 dev_map_mem_usage(const struct bpf_map *map)
  856. {
  857. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  858. u64 usage = sizeof(struct bpf_dtab);
  859. if (map->map_type == BPF_MAP_TYPE_DEVMAP_HASH)
  860. usage += (u64)dtab->n_buckets * sizeof(struct hlist_head);
  861. else
  862. usage += (u64)map->max_entries * sizeof(struct bpf_dtab_netdev *);
  863. usage += atomic_read((atomic_t *)&dtab->items) *
  864. (u64)sizeof(struct bpf_dtab_netdev);
  865. return usage;
  866. }
  867. BTF_ID_LIST_SINGLE(dev_map_btf_ids, struct, bpf_dtab)
  868. const struct bpf_map_ops dev_map_ops = {
  869. .map_meta_equal = bpf_map_meta_equal,
  870. .map_alloc_check = dev_map_alloc_check,
  871. .map_alloc = dev_map_alloc,
  872. .map_free = dev_map_free,
  873. .map_get_next_key = dev_map_get_next_key,
  874. .map_lookup_elem = dev_map_lookup_elem,
  875. .map_update_elem = dev_map_update_elem,
  876. .map_delete_elem = dev_map_delete_elem,
  877. .map_check_btf = map_check_no_btf,
  878. .map_mem_usage = dev_map_mem_usage,
  879. .map_btf_id = &dev_map_btf_ids[0],
  880. .map_redirect = dev_map_redirect,
  881. };
  882. const struct bpf_map_ops dev_map_hash_ops = {
  883. .map_meta_equal = bpf_map_meta_equal,
  884. .map_alloc_check = dev_map_alloc_check,
  885. .map_alloc = dev_map_alloc,
  886. .map_free = dev_map_free,
  887. .map_get_next_key = dev_map_hash_get_next_key,
  888. .map_lookup_elem = dev_map_hash_lookup_elem,
  889. .map_update_elem = dev_map_hash_update_elem,
  890. .map_delete_elem = dev_map_hash_delete_elem,
  891. .map_check_btf = map_check_no_btf,
  892. .map_mem_usage = dev_map_mem_usage,
  893. .map_btf_id = &dev_map_btf_ids[0],
  894. .map_redirect = dev_hash_map_redirect,
  895. };
  896. static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
  897. struct net_device *netdev)
  898. {
  899. unsigned long flags;
  900. u32 i;
  901. spin_lock_irqsave(&dtab->index_lock, flags);
  902. for (i = 0; i < dtab->n_buckets; i++) {
  903. struct bpf_dtab_netdev *dev;
  904. struct hlist_head *head;
  905. struct hlist_node *next;
  906. head = dev_map_index_hash(dtab, i);
  907. hlist_for_each_entry_safe(dev, next, head, index_hlist) {
  908. if (netdev != dev->dev)
  909. continue;
  910. dtab->items--;
  911. hlist_del_rcu(&dev->index_hlist);
  912. call_rcu(&dev->rcu, __dev_map_entry_free);
  913. }
  914. }
  915. spin_unlock_irqrestore(&dtab->index_lock, flags);
  916. }
  917. static int dev_map_notification(struct notifier_block *notifier,
  918. ulong event, void *ptr)
  919. {
  920. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  921. struct bpf_dtab *dtab;
  922. int i, cpu;
  923. switch (event) {
  924. case NETDEV_REGISTER:
  925. if (!netdev->netdev_ops->ndo_xdp_xmit || netdev->xdp_bulkq)
  926. break;
  927. /* will be freed in free_netdev() */
  928. netdev->xdp_bulkq = alloc_percpu(struct xdp_dev_bulk_queue);
  929. if (!netdev->xdp_bulkq)
  930. return NOTIFY_BAD;
  931. for_each_possible_cpu(cpu)
  932. per_cpu_ptr(netdev->xdp_bulkq, cpu)->dev = netdev;
  933. break;
  934. case NETDEV_UNREGISTER:
  935. /* This rcu_read_lock/unlock pair is needed because
  936. * dev_map_list is an RCU list AND to ensure a delete
  937. * operation does not free a netdev_map entry while we
  938. * are comparing it against the netdev being unregistered.
  939. */
  940. rcu_read_lock();
  941. list_for_each_entry_rcu(dtab, &dev_map_list, list) {
  942. if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
  943. dev_map_hash_remove_netdev(dtab, netdev);
  944. continue;
  945. }
  946. for (i = 0; i < dtab->map.max_entries; i++) {
  947. struct bpf_dtab_netdev *dev, *odev;
  948. dev = rcu_dereference(dtab->netdev_map[i]);
  949. if (!dev || netdev != dev->dev)
  950. continue;
  951. odev = unrcu_pointer(cmpxchg(&dtab->netdev_map[i], RCU_INITIALIZER(dev), NULL));
  952. if (dev == odev) {
  953. call_rcu(&dev->rcu,
  954. __dev_map_entry_free);
  955. atomic_dec((atomic_t *)&dtab->items);
  956. }
  957. }
  958. }
  959. rcu_read_unlock();
  960. break;
  961. default:
  962. break;
  963. }
  964. return NOTIFY_OK;
  965. }
  966. static struct notifier_block dev_map_notifier = {
  967. .notifier_call = dev_map_notification,
  968. };
  969. static int __init dev_map_init(void)
  970. {
  971. /* Assure tracepoint shadow struct _bpf_dtab_netdev is in sync */
  972. BUILD_BUG_ON(offsetof(struct bpf_dtab_netdev, dev) !=
  973. offsetof(struct _bpf_dtab_netdev, dev));
  974. register_netdevice_notifier(&dev_map_notifier);
  975. return 0;
  976. }
  977. subsys_initcall(dev_map_init);