1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
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.
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.
13 /* A BPF sock_map is used to store sock objects. This is primarly used
14 * for doing socket redirect with BPF helper routines.
16 * A sock map may have two BPF programs attached to it, a program used
17 * to parse packets and a program to provide a verdict and redirect
18 * decision on the packet. If no BPF parse program is provided it is
19 * assumed that every skb is a "message" (skb->len). Otherwise the
20 * parse program is attached to strparser and used to build messages
21 * that may span multiple skbs. The verdict program will either select
22 * a socket to send/receive the skb on or provide the drop code indicating
23 * the skb should be dropped. More actions may be added later as needed.
24 * The default program will drop packets.
26 * For reference this program is similar to devmap used in XDP context
27 * reviewing these together may be useful. For an example please review
28 * ./samples/bpf/sockmap/.
30 #include <linux/bpf.h>
32 #include <linux/filter.h>
33 #include <linux/errno.h>
34 #include <linux/file.h>
35 #include <linux/kernel.h>
36 #include <linux/net.h>
37 #include <linux/skbuff.h>
38 #include <linux/workqueue.h>
39 #include <linux/list.h>
40 #include <net/strparser.h>
44 struct sock **sock_map;
45 struct bpf_prog *bpf_parse;
46 struct bpf_prog *bpf_verdict;
50 enum smap_psock_state {
57 /* datapath variables */
58 struct sk_buff_head rxqueue;
61 /* datapath error path cache across tx work invocations */
64 struct sk_buff *save_skb;
66 struct strparser strp;
67 struct bpf_prog *bpf_parse;
68 struct bpf_prog *bpf_verdict;
69 struct bpf_stab *stab;
71 /* Back reference used when sock callback trigger sockmap operations */
76 struct work_struct tx_work;
77 struct work_struct gc_work;
79 void (*save_data_ready)(struct sock *sk);
80 void (*save_write_space)(struct sock *sk);
81 void (*save_state_change)(struct sock *sk);
84 static inline struct smap_psock *smap_psock_sk(const struct sock *sk)
86 return (struct smap_psock *)rcu_dereference_sk_user_data(sk);
89 static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
91 struct bpf_prog *prog = READ_ONCE(psock->bpf_verdict);
98 skb->sk = psock->sock;
99 bpf_compute_data_end(skb);
100 rc = (*prog->bpf_func)(skb, prog->insnsi);
106 static void smap_do_verdict(struct smap_psock *psock, struct sk_buff *skb)
111 /* Because we use per cpu values to feed input from sock redirect
112 * in BPF program to do_sk_redirect_map() call we need to ensure we
113 * are not preempted. RCU read lock is not sufficient in this case
114 * with CONFIG_PREEMPT_RCU enabled so we must be explicit here.
117 rc = smap_verdict_func(psock, skb);
120 sock = do_sk_redirect_map();
123 struct smap_psock *peer = smap_psock_sk(sock);
126 test_bit(SMAP_TX_RUNNING, &peer->state) &&
127 sk_stream_memory_free(peer->sock))) {
128 peer->sock->sk_wmem_queued += skb->truesize;
129 sk_mem_charge(peer->sock, skb->truesize);
130 skb_queue_tail(&peer->rxqueue, skb);
131 schedule_work(&peer->tx_work);
135 /* Fall through and free skb otherwise */
143 static void smap_report_sk_error(struct smap_psock *psock, int err)
145 struct sock *sk = psock->sock;
148 sk->sk_error_report(sk);
151 static void smap_release_sock(struct sock *sock);
153 /* Called with lock_sock(sk) held */
154 static void smap_state_change(struct sock *sk)
156 struct smap_psock *psock;
161 /* Allowing transitions into an established syn_recv states allows
162 * for early binding sockets to a smap object before the connection
165 switch (sk->sk_state) {
167 case TCP_ESTABLISHED:
177 /* Only release if the map entry is in fact the sock in
178 * question. There is a case where the operator deletes
179 * the sock from the map, but the TCP sock is closed before
180 * the psock is detached. Use cmpxchg to verify correct
183 psock = smap_psock_sk(sk);
184 if (unlikely(!psock))
186 osk = cmpxchg(&psock->stab->sock_map[psock->key], sk, NULL);
188 smap_release_sock(sk);
191 smap_report_sk_error(psock, EPIPE);
197 static void smap_read_sock_strparser(struct strparser *strp,
200 struct smap_psock *psock;
203 psock = container_of(strp, struct smap_psock, strp);
204 smap_do_verdict(psock, skb);
208 /* Called with lock held on socket */
209 static void smap_data_ready(struct sock *sk)
211 struct smap_psock *psock;
213 write_lock_bh(&sk->sk_callback_lock);
214 psock = smap_psock_sk(sk);
216 strp_data_ready(&psock->strp);
217 write_unlock_bh(&sk->sk_callback_lock);
220 static void smap_tx_work(struct work_struct *w)
222 struct smap_psock *psock;
226 psock = container_of(w, struct smap_psock, tx_work);
228 /* lock sock to avoid losing sk_socket at some point during loop */
229 lock_sock(psock->sock);
230 if (psock->save_skb) {
231 skb = psock->save_skb;
232 rem = psock->save_rem;
233 off = psock->save_off;
234 psock->save_skb = NULL;
238 while ((skb = skb_dequeue(&psock->rxqueue))) {
243 if (likely(psock->sock->sk_socket))
244 n = skb_send_sock_locked(psock->sock,
250 /* Retry when space is available */
251 psock->save_skb = skb;
252 psock->save_rem = rem;
253 psock->save_off = off;
256 /* Hard errors break pipe and stop xmit */
257 smap_report_sk_error(psock, n ? -n : EPIPE);
258 clear_bit(SMAP_TX_RUNNING, &psock->state);
259 sk_mem_uncharge(psock->sock, skb->truesize);
260 psock->sock->sk_wmem_queued -= skb->truesize;
267 sk_mem_uncharge(psock->sock, skb->truesize);
268 psock->sock->sk_wmem_queued -= skb->truesize;
272 release_sock(psock->sock);
275 static void smap_write_space(struct sock *sk)
277 struct smap_psock *psock;
280 psock = smap_psock_sk(sk);
281 if (likely(psock && test_bit(SMAP_TX_RUNNING, &psock->state)))
282 schedule_work(&psock->tx_work);
286 static void smap_stop_sock(struct smap_psock *psock, struct sock *sk)
288 write_lock_bh(&sk->sk_callback_lock);
289 if (!psock->strp_enabled)
291 sk->sk_data_ready = psock->save_data_ready;
292 sk->sk_write_space = psock->save_write_space;
293 sk->sk_state_change = psock->save_state_change;
294 psock->save_data_ready = NULL;
295 psock->save_write_space = NULL;
296 psock->save_state_change = NULL;
297 strp_stop(&psock->strp);
298 psock->strp_enabled = false;
300 write_unlock_bh(&sk->sk_callback_lock);
303 static void smap_destroy_psock(struct rcu_head *rcu)
305 struct smap_psock *psock = container_of(rcu,
306 struct smap_psock, rcu);
308 /* Now that a grace period has passed there is no longer
309 * any reference to this sock in the sockmap so we can
310 * destroy the psock, strparser, and bpf programs. But,
311 * because we use workqueue sync operations we can not
312 * do it in rcu context
314 schedule_work(&psock->gc_work);
317 static void smap_release_sock(struct sock *sock)
319 struct smap_psock *psock = smap_psock_sk(sock);
321 smap_stop_sock(psock, sock);
322 clear_bit(SMAP_TX_RUNNING, &psock->state);
323 rcu_assign_sk_user_data(sock, NULL);
324 call_rcu_sched(&psock->rcu, smap_destroy_psock);
327 static int smap_parse_func_strparser(struct strparser *strp,
330 struct smap_psock *psock;
331 struct bpf_prog *prog;
335 psock = container_of(strp, struct smap_psock, strp);
336 prog = READ_ONCE(psock->bpf_parse);
338 if (unlikely(!prog)) {
343 /* Attach socket for bpf program to use if needed we can do this
344 * because strparser clones the skb before handing it to a upper
345 * layer, meaning skb_orphan has been called. We NULL sk on the
346 * way out to ensure we don't trigger a BUG_ON in skb/sk operations
347 * later and because we are not charging the memory of this skb to
350 skb->sk = psock->sock;
351 bpf_compute_data_end(skb);
352 rc = (*prog->bpf_func)(skb, prog->insnsi);
359 static int smap_read_sock_done(struct strparser *strp, int err)
364 static int smap_init_sock(struct smap_psock *psock,
367 struct strp_callbacks cb;
369 memset(&cb, 0, sizeof(cb));
370 cb.rcv_msg = smap_read_sock_strparser;
371 cb.parse_msg = smap_parse_func_strparser;
372 cb.read_sock_done = smap_read_sock_done;
373 return strp_init(&psock->strp, sk, &cb);
376 static void smap_init_progs(struct smap_psock *psock,
377 struct bpf_stab *stab,
378 struct bpf_prog *verdict,
379 struct bpf_prog *parse)
381 struct bpf_prog *orig_parse, *orig_verdict;
383 orig_parse = xchg(&psock->bpf_parse, parse);
384 orig_verdict = xchg(&psock->bpf_verdict, verdict);
387 bpf_prog_put(orig_verdict);
389 bpf_prog_put(orig_parse);
392 static void smap_start_sock(struct smap_psock *psock, struct sock *sk)
394 if (sk->sk_data_ready == smap_data_ready)
396 psock->save_data_ready = sk->sk_data_ready;
397 psock->save_write_space = sk->sk_write_space;
398 psock->save_state_change = sk->sk_state_change;
399 sk->sk_data_ready = smap_data_ready;
400 sk->sk_write_space = smap_write_space;
401 sk->sk_state_change = smap_state_change;
402 psock->strp_enabled = true;
405 static void sock_map_remove_complete(struct bpf_stab *stab)
407 bpf_map_area_free(stab->sock_map);
411 static void smap_gc_work(struct work_struct *w)
413 struct smap_psock *psock;
415 psock = container_of(w, struct smap_psock, gc_work);
417 /* no callback lock needed because we already detached sockmap ops */
418 if (psock->strp_enabled)
419 strp_done(&psock->strp);
421 cancel_work_sync(&psock->tx_work);
422 __skb_queue_purge(&psock->rxqueue);
424 /* At this point all strparser and xmit work must be complete */
425 if (psock->bpf_parse)
426 bpf_prog_put(psock->bpf_parse);
427 if (psock->bpf_verdict)
428 bpf_prog_put(psock->bpf_verdict);
430 if (refcount_dec_and_test(&psock->stab->refcnt))
431 sock_map_remove_complete(psock->stab);
433 sock_put(psock->sock);
437 static struct smap_psock *smap_init_psock(struct sock *sock,
438 struct bpf_stab *stab)
440 struct smap_psock *psock;
442 psock = kzalloc(sizeof(struct smap_psock), GFP_ATOMIC | __GFP_NOWARN);
444 return ERR_PTR(-ENOMEM);
447 skb_queue_head_init(&psock->rxqueue);
448 INIT_WORK(&psock->tx_work, smap_tx_work);
449 INIT_WORK(&psock->gc_work, smap_gc_work);
451 rcu_assign_sk_user_data(sock, psock);
456 static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
458 struct bpf_stab *stab;
462 /* check sanity of attributes */
463 if (attr->max_entries == 0 || attr->key_size != 4 ||
464 attr->value_size != 4 || attr->map_flags)
465 return ERR_PTR(-EINVAL);
467 if (attr->value_size > KMALLOC_MAX_SIZE)
468 return ERR_PTR(-E2BIG);
470 stab = kzalloc(sizeof(*stab), GFP_USER);
472 return ERR_PTR(-ENOMEM);
474 /* mandatory map attributes */
475 stab->map.map_type = attr->map_type;
476 stab->map.key_size = attr->key_size;
477 stab->map.value_size = attr->value_size;
478 stab->map.max_entries = attr->max_entries;
479 stab->map.map_flags = attr->map_flags;
481 /* make sure page count doesn't overflow */
482 cost = (u64) stab->map.max_entries * sizeof(struct sock *);
483 if (cost >= U32_MAX - PAGE_SIZE)
486 stab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT;
488 /* if map size is larger than memlock limit, reject it early */
489 err = bpf_map_precharge_memlock(stab->map.pages);
493 stab->sock_map = bpf_map_area_alloc(stab->map.max_entries *
494 sizeof(struct sock *));
498 refcount_set(&stab->refcnt, 1);
505 static void sock_map_free(struct bpf_map *map)
507 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
512 /* At this point no update, lookup or delete operations can happen.
513 * However, be aware we can still get a socket state event updates,
514 * and data ready callabacks that reference the psock from sk_user_data
515 * Also psock worker threads are still in-flight. So smap_release_sock
516 * will only free the psock after cancel_sync on the worker threads
517 * and a grace period expire to ensure psock is really safe to remove.
520 for (i = 0; i < stab->map.max_entries; i++) {
523 sock = xchg(&stab->sock_map[i], NULL);
527 smap_release_sock(sock);
531 if (stab->bpf_verdict)
532 bpf_prog_put(stab->bpf_verdict);
534 bpf_prog_put(stab->bpf_parse);
536 if (refcount_dec_and_test(&stab->refcnt))
537 sock_map_remove_complete(stab);
540 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
542 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
543 u32 i = key ? *(u32 *)key : U32_MAX;
544 u32 *next = (u32 *)next_key;
546 if (i >= stab->map.max_entries) {
551 if (i == stab->map.max_entries - 1)
558 struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
560 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
562 if (key >= map->max_entries)
565 return READ_ONCE(stab->sock_map[key]);
568 static int sock_map_delete_elem(struct bpf_map *map, void *key)
570 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
574 if (k >= map->max_entries)
577 sock = xchg(&stab->sock_map[k], NULL);
581 smap_release_sock(sock);
585 /* Locking notes: Concurrent updates, deletes, and lookups are allowed and are
586 * done inside rcu critical sections. This ensures on updates that the psock
587 * will not be released via smap_release_sock() until concurrent updates/deletes
588 * complete. All operations operate on sock_map using cmpxchg and xchg
589 * operations to ensure we do not get stale references. Any reads into the
590 * map must be done with READ_ONCE() because of this.
592 * A psock is destroyed via call_rcu and after any worker threads are cancelled
593 * and syncd so we are certain all references from the update/lookup/delete
594 * operations as well as references in the data path are no longer in use.
596 * A psock object holds a refcnt on the sockmap it is attached to and this is
597 * not decremented until after a RCU grace period and garbage collection occurs.
598 * This ensures the map is not free'd until psocks linked to it are removed. The
599 * map link is used when the independent sock events trigger map deletion.
601 * Psocks may only participate in one sockmap at a time. Users that try to
602 * join a single sock to multiple maps will get an error.
604 * Last, but not least, it is possible the socket is closed while running
605 * an update on an existing psock. This will release the psock, but again
606 * not until the update has completed due to rcu grace period rules.
608 static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
610 void *key, u64 flags, u64 map_flags)
612 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
613 struct bpf_prog *verdict, *parse;
614 struct smap_psock *psock = NULL;
615 struct sock *old_sock, *sock;
620 if (unlikely(flags > BPF_EXIST))
623 if (unlikely(i >= stab->map.max_entries))
626 if (unlikely(map_flags > BPF_SOCKMAP_STRPARSER))
629 verdict = parse = NULL;
630 sock = READ_ONCE(stab->sock_map[i]);
632 if (flags == BPF_EXIST || flags == BPF_ANY) {
633 if (!sock && flags == BPF_EXIST) {
635 } else if (sock && sock != skops->sk) {
638 psock = smap_psock_sk(sock);
639 if (unlikely(!psock))
643 } else if (sock && BPF_NOEXIST) {
647 /* reserve BPF programs early so can abort easily on failures */
648 if (map_flags & BPF_SOCKMAP_STRPARSER) {
649 verdict = READ_ONCE(stab->bpf_verdict);
650 parse = READ_ONCE(stab->bpf_parse);
652 if (!verdict || !parse)
655 /* bpf prog refcnt may be zero if a concurrent attach operation
656 * removes the program after the above READ_ONCE() but before
657 * we increment the refcnt. If this is the case abort with an
660 verdict = bpf_prog_inc_not_zero(stab->bpf_verdict);
662 return PTR_ERR(verdict);
664 parse = bpf_prog_inc_not_zero(stab->bpf_parse);
666 bpf_prog_put(verdict);
667 return PTR_ERR(parse);
673 if (rcu_dereference_sk_user_data(sock))
675 psock = smap_init_psock(sock, stab);
678 bpf_prog_put(verdict);
681 return PTR_ERR(psock);
685 refcount_inc(&stab->refcnt);
686 set_bit(SMAP_TX_RUNNING, &psock->state);
689 if (map_flags & BPF_SOCKMAP_STRPARSER) {
690 write_lock_bh(&sock->sk_callback_lock);
691 if (psock->strp_enabled)
693 err = smap_init_sock(psock, sock);
696 smap_init_progs(psock, stab, verdict, parse);
697 smap_start_sock(psock, sock);
699 write_unlock_bh(&sock->sk_callback_lock);
701 smap_stop_sock(psock, sock);
705 old_sock = xchg(&stab->sock_map[i], skops->sk);
707 smap_release_sock(old_sock);
712 write_unlock_bh(&sock->sk_callback_lock);
714 smap_release_sock(sock);
718 static int sock_map_attach_prog(struct bpf_map *map,
719 struct bpf_prog *parse,
720 struct bpf_prog *verdict)
722 struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
723 struct bpf_prog *_parse, *_verdict;
725 _parse = xchg(&stab->bpf_parse, parse);
726 _verdict = xchg(&stab->bpf_verdict, verdict);
729 bpf_prog_put(_parse);
731 bpf_prog_put(_verdict);
736 static void *sock_map_lookup(struct bpf_map *map, void *key)
741 static int sock_map_update_elem(struct bpf_map *map,
742 void *key, void *value, u64 flags)
744 struct bpf_sock_ops_kern skops;
745 u32 fd = *(u32 *)value;
746 struct socket *socket;
749 socket = sockfd_lookup(fd, &err);
753 skops.sk = socket->sk;
759 err = sock_map_ctx_update_elem(&skops, map, key,
760 flags, BPF_SOCKMAP_STRPARSER);
765 const struct bpf_map_ops sock_map_ops = {
766 .map_alloc = sock_map_alloc,
767 .map_free = sock_map_free,
768 .map_lookup_elem = sock_map_lookup,
769 .map_get_next_key = sock_map_get_next_key,
770 .map_update_elem = sock_map_update_elem,
771 .map_delete_elem = sock_map_delete_elem,
772 .map_attach = sock_map_attach_prog,
775 BPF_CALL_5(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock,
776 struct bpf_map *, map, void *, key, u64, flags, u64, map_flags)
778 WARN_ON_ONCE(!rcu_read_lock_held());
779 return sock_map_ctx_update_elem(bpf_sock, map, key, flags, map_flags);
782 const struct bpf_func_proto bpf_sock_map_update_proto = {
783 .func = bpf_sock_map_update,
786 .ret_type = RET_INTEGER,
787 .arg1_type = ARG_PTR_TO_CTX,
788 .arg2_type = ARG_CONST_MAP_PTR,
789 .arg3_type = ARG_PTR_TO_MAP_KEY,
790 .arg4_type = ARG_ANYTHING,
791 .arg5_type = ARG_ANYTHING,