devmap.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. /* Devmaps primary use is as a backend map for XDP BPF helper call
  13. * bpf_redirect_map(). Because XDP is mostly concerned with performance we
  14. * spent some effort to ensure the datapath with redirect maps does not use
  15. * any locking. This is a quick note on the details.
  16. *
  17. * We have three possible paths to get into the devmap control plane bpf
  18. * syscalls, bpf programs, and driver side xmit/flush operations. A bpf syscall
  19. * will invoke an update, delete, or lookup operation. To ensure updates and
  20. * deletes appear atomic from the datapath side xchg() is used to modify the
  21. * netdev_map array. Then because the datapath does a lookup into the netdev_map
  22. * array (read-only) from an RCU critical section we use call_rcu() to wait for
  23. * an rcu grace period before free'ing the old data structures. This ensures the
  24. * datapath always has a valid copy. However, the datapath does a "flush"
  25. * operation that pushes any pending packets in the driver outside the RCU
  26. * critical section. Each bpf_dtab_netdev tracks these pending operations using
  27. * an atomic per-cpu bitmap. The bpf_dtab_netdev object will not be destroyed
  28. * until all bits are cleared indicating outstanding flush operations have
  29. * completed.
  30. *
  31. * BPF syscalls may race with BPF program calls on any of the update, delete
  32. * or lookup operations. As noted above the xchg() operation also keep the
  33. * netdev_map consistent in this case. From the devmap side BPF programs
  34. * calling into these operations are the same as multiple user space threads
  35. * making system calls.
  36. */
  37. #include <linux/bpf.h>
  38. #include <linux/jhash.h>
  39. #include <linux/filter.h>
  40. #include <linux/rculist_nulls.h>
  41. #include "percpu_freelist.h"
  42. #include "bpf_lru_list.h"
  43. #include "map_in_map.h"
  44. struct bpf_dtab_netdev {
  45. struct net_device *dev;
  46. int key;
  47. struct rcu_head rcu;
  48. struct bpf_dtab *dtab;
  49. };
  50. struct bpf_dtab {
  51. struct bpf_map map;
  52. struct bpf_dtab_netdev **netdev_map;
  53. };
  54. static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
  55. {
  56. struct bpf_dtab *dtab;
  57. u64 cost;
  58. int err;
  59. /* check sanity of attributes */
  60. if (attr->max_entries == 0 || attr->key_size != 4 ||
  61. attr->value_size != 4 || attr->map_flags)
  62. return ERR_PTR(-EINVAL);
  63. /* if value_size is bigger, the user space won't be able to
  64. * access the elements.
  65. */
  66. if (attr->value_size > KMALLOC_MAX_SIZE)
  67. return ERR_PTR(-E2BIG);
  68. dtab = kzalloc(sizeof(*dtab), GFP_USER);
  69. if (!dtab)
  70. return ERR_PTR(-ENOMEM);
  71. /* mandatory map attributes */
  72. dtab->map.map_type = attr->map_type;
  73. dtab->map.key_size = attr->key_size;
  74. dtab->map.value_size = attr->value_size;
  75. dtab->map.max_entries = attr->max_entries;
  76. dtab->map.map_flags = attr->map_flags;
  77. err = -ENOMEM;
  78. /* make sure page count doesn't overflow */
  79. cost = (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *);
  80. if (cost >= U32_MAX - PAGE_SIZE)
  81. goto free_dtab;
  82. dtab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
  83. /* if map size is larger than memlock limit, reject it early */
  84. err = bpf_map_precharge_memlock(dtab->map.pages);
  85. if (err)
  86. goto free_dtab;
  87. dtab->netdev_map = bpf_map_area_alloc(dtab->map.max_entries *
  88. sizeof(struct bpf_dtab_netdev *));
  89. if (!dtab->netdev_map)
  90. goto free_dtab;
  91. return &dtab->map;
  92. free_dtab:
  93. kfree(dtab);
  94. return ERR_PTR(err);
  95. }
  96. static void dev_map_free(struct bpf_map *map)
  97. {
  98. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  99. int i;
  100. /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  101. * so the programs (can be more than one that used this map) were
  102. * disconnected from events. Wait for outstanding critical sections in
  103. * these programs to complete. The rcu critical section only guarantees
  104. * no further reads against netdev_map. It does __not__ ensure pending
  105. * flush operations (if any) are complete.
  106. */
  107. synchronize_rcu();
  108. for (i = 0; i < dtab->map.max_entries; i++) {
  109. struct bpf_dtab_netdev *dev;
  110. dev = dtab->netdev_map[i];
  111. if (!dev)
  112. continue;
  113. dev_put(dev->dev);
  114. kfree(dev);
  115. }
  116. /* At this point bpf program is detached and all pending operations
  117. * _must_ be complete
  118. */
  119. bpf_map_area_free(dtab->netdev_map);
  120. kfree(dtab);
  121. }
  122. static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  123. {
  124. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  125. u32 index = key ? *(u32 *)key : U32_MAX;
  126. u32 *next = (u32 *)next_key;
  127. if (index >= dtab->map.max_entries) {
  128. *next = 0;
  129. return 0;
  130. }
  131. if (index == dtab->map.max_entries - 1)
  132. return -ENOENT;
  133. *next = index + 1;
  134. return 0;
  135. }
  136. /* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
  137. * update happens in parallel here a dev_put wont happen until after reading the
  138. * ifindex.
  139. */
  140. static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
  141. {
  142. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  143. struct bpf_dtab_netdev *dev;
  144. u32 i = *(u32 *)key;
  145. if (i >= map->max_entries)
  146. return NULL;
  147. dev = READ_ONCE(dtab->netdev_map[i]);
  148. return dev ? &dev->dev->ifindex : NULL;
  149. }
  150. static void __dev_map_entry_free(struct rcu_head *rcu)
  151. {
  152. struct bpf_dtab_netdev *old_dev;
  153. old_dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
  154. dev_put(old_dev->dev);
  155. kfree(old_dev);
  156. }
  157. static int dev_map_delete_elem(struct bpf_map *map, void *key)
  158. {
  159. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  160. struct bpf_dtab_netdev *old_dev;
  161. int k = *(u32 *)key;
  162. if (k >= map->max_entries)
  163. return -EINVAL;
  164. /* Use synchronize_rcu() here to ensure any rcu critical sections
  165. * have completed, but this does not guarantee a flush has happened
  166. * yet. Because driver side rcu_read_lock/unlock only protects the
  167. * running XDP program. However, for pending flush operations the
  168. * dev and ctx are stored in another per cpu map. And additionally,
  169. * the driver tear down ensures all soft irqs are complete before
  170. * removing the net device in the case of dev_put equals zero.
  171. */
  172. old_dev = xchg(&dtab->netdev_map[k], NULL);
  173. if (old_dev)
  174. call_rcu(&old_dev->rcu, __dev_map_entry_free);
  175. return 0;
  176. }
  177. static int dev_map_update_elem(struct bpf_map *map, void *key, void *value,
  178. u64 map_flags)
  179. {
  180. struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
  181. struct net *net = current->nsproxy->net_ns;
  182. struct bpf_dtab_netdev *dev, *old_dev;
  183. u32 i = *(u32 *)key;
  184. u32 ifindex = *(u32 *)value;
  185. if (unlikely(map_flags > BPF_EXIST))
  186. return -EINVAL;
  187. if (unlikely(i >= dtab->map.max_entries))
  188. return -E2BIG;
  189. if (unlikely(map_flags == BPF_NOEXIST))
  190. return -EEXIST;
  191. if (!ifindex) {
  192. dev = NULL;
  193. } else {
  194. dev = kmalloc(sizeof(*dev), GFP_ATOMIC | __GFP_NOWARN);
  195. if (!dev)
  196. return -ENOMEM;
  197. dev->dev = dev_get_by_index(net, ifindex);
  198. if (!dev->dev) {
  199. kfree(dev);
  200. return -EINVAL;
  201. }
  202. dev->key = i;
  203. dev->dtab = dtab;
  204. }
  205. /* Use call_rcu() here to ensure rcu critical sections have completed
  206. * Remembering the driver side flush operation will happen before the
  207. * net device is removed.
  208. */
  209. old_dev = xchg(&dtab->netdev_map[i], dev);
  210. if (old_dev)
  211. call_rcu(&old_dev->rcu, __dev_map_entry_free);
  212. return 0;
  213. }
  214. const struct bpf_map_ops dev_map_ops = {
  215. .map_alloc = dev_map_alloc,
  216. .map_free = dev_map_free,
  217. .map_get_next_key = dev_map_get_next_key,
  218. .map_lookup_elem = dev_map_lookup_elem,
  219. .map_update_elem = dev_map_update_elem,
  220. .map_delete_elem = dev_map_delete_elem,
  221. };