Skip to content

Commit 2baec2c

Browse files
dhowellsdavem330
authored andcommitted
rxrpc: Support network namespacing
Support network namespacing in AF_RXRPC with the following changes: (1) All the local endpoint, peer and call lists, locks, counters, etc. are moved into the per-namespace record. (2) All the connection tracking is moved into the per-namespace record with the exception of the client connection ID tree, which is kept global so that connection IDs are kept unique per-machine. (3) Each namespace gets its own epoch. This allows each network namespace to pretend to be a separate client machine. (4) The /proc/net/rxrpc_xxx files are now called /proc/net/rxrpc/xxx and the contents reflect the namespace. fs/afs/ should be okay with this patch as it explicitly requires the current net namespace to be init_net to permit a mount to proceed at the moment. It will, however, need updating so that cells, IP addresses and DNS records are per-namespace also. Signed-off-by: David Howells <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 878cd3b commit 2baec2c

12 files changed

+356
-216
lines changed

net/rxrpc/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rxrpc-y := \
1919
local_event.o \
2020
local_object.o \
2121
misc.o \
22+
net_ns.o \
2223
output.o \
2324
peer_event.o \
2425
peer_object.o \

net/rxrpc/af_rxrpc.c

+15-20
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ MODULE_PARM_DESC(debug, "RxRPC debugging mask");
3838
static struct proto rxrpc_proto;
3939
static const struct proto_ops rxrpc_rpc_ops;
4040

41-
/* local epoch for detecting local-end reset */
42-
u32 rxrpc_epoch;
43-
4441
/* current debugging ID */
4542
atomic_t rxrpc_debug_id;
4643

@@ -155,7 +152,7 @@ static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
155152

156153
memcpy(&rx->srx, srx, sizeof(rx->srx));
157154

158-
local = rxrpc_lookup_local(&rx->srx);
155+
local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
159156
if (IS_ERR(local)) {
160157
ret = PTR_ERR(local);
161158
goto error_unlock;
@@ -434,7 +431,7 @@ static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
434431
ret = -EAFNOSUPPORT;
435432
goto error_unlock;
436433
}
437-
local = rxrpc_lookup_local(&rx->srx);
434+
local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
438435
if (IS_ERR(local)) {
439436
ret = PTR_ERR(local);
440437
goto error_unlock;
@@ -582,9 +579,6 @@ static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
582579

583580
_enter("%p,%d", sock, protocol);
584581

585-
if (!net_eq(net, &init_net))
586-
return -EAFNOSUPPORT;
587-
588582
/* we support transport protocol UDP/UDP6 only */
589583
if (protocol != PF_INET &&
590584
IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
@@ -780,8 +774,6 @@ static int __init af_rxrpc_init(void)
780774

781775
BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
782776

783-
get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch));
784-
rxrpc_epoch |= RXRPC_RANDOM_EPOCH;
785777
get_random_bytes(&tmp, sizeof(tmp));
786778
tmp &= 0x3fffffff;
787779
if (tmp == 0)
@@ -809,6 +801,10 @@ static int __init af_rxrpc_init(void)
809801
goto error_security;
810802
}
811803

804+
ret = register_pernet_subsys(&rxrpc_net_ops);
805+
if (ret)
806+
goto error_pernet;
807+
812808
ret = proto_register(&rxrpc_proto, 1);
813809
if (ret < 0) {
814810
pr_crit("Cannot register protocol\n");
@@ -839,11 +835,6 @@ static int __init af_rxrpc_init(void)
839835
goto error_sysctls;
840836
}
841837

842-
#ifdef CONFIG_PROC_FS
843-
proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
844-
proc_create("rxrpc_conns", 0, init_net.proc_net,
845-
&rxrpc_connection_seq_fops);
846-
#endif
847838
return 0;
848839

849840
error_sysctls:
@@ -855,6 +846,8 @@ static int __init af_rxrpc_init(void)
855846
error_sock:
856847
proto_unregister(&rxrpc_proto);
857848
error_proto:
849+
unregister_pernet_subsys(&rxrpc_net_ops);
850+
error_pernet:
858851
rxrpc_exit_security();
859852
error_security:
860853
destroy_workqueue(rxrpc_workqueue);
@@ -875,14 +868,16 @@ static void __exit af_rxrpc_exit(void)
875868
unregister_key_type(&key_type_rxrpc);
876869
sock_unregister(PF_RXRPC);
877870
proto_unregister(&rxrpc_proto);
878-
rxrpc_destroy_all_calls();
879-
rxrpc_destroy_all_connections();
871+
unregister_pernet_subsys(&rxrpc_net_ops);
880872
ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
881873
ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
882-
rxrpc_destroy_all_locals();
883874

884-
remove_proc_entry("rxrpc_conns", init_net.proc_net);
885-
remove_proc_entry("rxrpc_calls", init_net.proc_net);
875+
/* Make sure the local and peer records pinned by any dying connections
876+
* are released.
877+
*/
878+
rcu_barrier();
879+
rxrpc_destroy_client_conn_ids();
880+
886881
destroy_workqueue(rxrpc_workqueue);
887882
rxrpc_exit_security();
888883
kmem_cache_destroy(rxrpc_call_jar);

net/rxrpc/ar-internal.h

+53-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <linux/atomic.h>
1313
#include <linux/seqlock.h>
14+
#include <net/net_namespace.h>
15+
#include <net/netns/generic.h>
1416
#include <net/sock.h>
1517
#include <net/af_rxrpc.h>
1618
#include <rxrpc/packet.h>
@@ -64,6 +66,37 @@ enum {
6466
RXRPC_CLOSE, /* socket is being closed */
6567
};
6668

69+
/*
70+
* Per-network namespace data.
71+
*/
72+
struct rxrpc_net {
73+
struct proc_dir_entry *proc_net; /* Subdir in /proc/net */
74+
u32 epoch; /* Local epoch for detecting local-end reset */
75+
struct list_head calls; /* List of calls active in this namespace */
76+
rwlock_t call_lock; /* Lock for ->calls */
77+
78+
struct list_head conn_proc_list; /* List of conns in this namespace for proc */
79+
struct list_head service_conns; /* Service conns in this namespace */
80+
rwlock_t conn_lock; /* Lock for ->conn_proc_list, ->service_conns */
81+
struct delayed_work service_conn_reaper;
82+
83+
unsigned int nr_client_conns;
84+
unsigned int nr_active_client_conns;
85+
bool kill_all_client_conns;
86+
spinlock_t client_conn_cache_lock; /* Lock for ->*_client_conns */
87+
spinlock_t client_conn_discard_lock; /* Prevent multiple discarders */
88+
struct list_head waiting_client_conns;
89+
struct list_head active_client_conns;
90+
struct list_head idle_client_conns;
91+
struct delayed_work client_conn_reaper;
92+
93+
struct list_head local_endpoints;
94+
struct mutex local_mutex; /* Lock for ->local_endpoints */
95+
96+
spinlock_t peer_hash_lock; /* Lock for ->peer_hash */
97+
DECLARE_HASHTABLE (peer_hash, 10);
98+
};
99+
67100
/*
68101
* Service backlog preallocation.
69102
*
@@ -211,6 +244,7 @@ struct rxrpc_security {
211244
struct rxrpc_local {
212245
struct rcu_head rcu;
213246
atomic_t usage;
247+
struct rxrpc_net *rxnet; /* The network ns in which this resides */
214248
struct list_head link;
215249
struct socket *socket; /* my UDP socket */
216250
struct work_struct processor;
@@ -601,7 +635,6 @@ struct rxrpc_ack_summary {
601635
* af_rxrpc.c
602636
*/
603637
extern atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
604-
extern u32 rxrpc_epoch;
605638
extern atomic_t rxrpc_debug_id;
606639
extern struct workqueue_struct *rxrpc_workqueue;
607640

@@ -634,8 +667,6 @@ extern const char *const rxrpc_call_states[];
634667
extern const char *const rxrpc_call_completions[];
635668
extern unsigned int rxrpc_max_call_lifetime;
636669
extern struct kmem_cache *rxrpc_call_jar;
637-
extern struct list_head rxrpc_calls;
638-
extern rwlock_t rxrpc_call_lock;
639670

640671
struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *, unsigned long);
641672
struct rxrpc_call *rxrpc_alloc_call(gfp_t);
@@ -653,7 +684,7 @@ void rxrpc_see_call(struct rxrpc_call *);
653684
void rxrpc_get_call(struct rxrpc_call *, enum rxrpc_call_trace);
654685
void rxrpc_put_call(struct rxrpc_call *, enum rxrpc_call_trace);
655686
void rxrpc_cleanup_call(struct rxrpc_call *);
656-
void __exit rxrpc_destroy_all_calls(void);
687+
void rxrpc_destroy_all_calls(struct rxrpc_net *);
657688

658689
static inline bool rxrpc_is_service_call(const struct rxrpc_call *call)
659690
{
@@ -773,7 +804,8 @@ int rxrpc_connect_call(struct rxrpc_call *, struct rxrpc_conn_parameters *,
773804
void rxrpc_expose_client_call(struct rxrpc_call *);
774805
void rxrpc_disconnect_client_call(struct rxrpc_call *);
775806
void rxrpc_put_client_conn(struct rxrpc_connection *);
776-
void __exit rxrpc_destroy_all_client_connections(void);
807+
void rxrpc_discard_expired_client_conns(struct work_struct *);
808+
void rxrpc_destroy_all_client_connections(struct rxrpc_net *);
777809

778810
/*
779811
* conn_event.c
@@ -784,9 +816,6 @@ void rxrpc_process_connection(struct work_struct *);
784816
* conn_object.c
785817
*/
786818
extern unsigned int rxrpc_connection_expiry;
787-
extern struct list_head rxrpc_connections;
788-
extern struct list_head rxrpc_connection_proc_list;
789-
extern rwlock_t rxrpc_connection_lock;
790819

791820
int rxrpc_extract_addr_from_skb(struct sockaddr_rxrpc *, struct sk_buff *);
792821
struct rxrpc_connection *rxrpc_alloc_connection(gfp_t);
@@ -800,7 +829,8 @@ void rxrpc_see_connection(struct rxrpc_connection *);
800829
void rxrpc_get_connection(struct rxrpc_connection *);
801830
struct rxrpc_connection *rxrpc_get_connection_maybe(struct rxrpc_connection *);
802831
void rxrpc_put_service_conn(struct rxrpc_connection *);
803-
void __exit rxrpc_destroy_all_connections(void);
832+
void rxrpc_service_connection_reaper(struct work_struct *);
833+
void rxrpc_destroy_all_connections(struct rxrpc_net *);
804834

805835
static inline bool rxrpc_conn_is_client(const struct rxrpc_connection *conn)
806836
{
@@ -828,7 +858,7 @@ static inline void rxrpc_put_connection(struct rxrpc_connection *conn)
828858
*/
829859
struct rxrpc_connection *rxrpc_find_service_conn_rcu(struct rxrpc_peer *,
830860
struct sk_buff *);
831-
struct rxrpc_connection *rxrpc_prealloc_service_connection(gfp_t);
861+
struct rxrpc_connection *rxrpc_prealloc_service_connection(struct rxrpc_net *, gfp_t);
832862
void rxrpc_new_incoming_connection(struct rxrpc_connection *, struct sk_buff *);
833863
void rxrpc_unpublish_service_conn(struct rxrpc_connection *);
834864

@@ -861,9 +891,9 @@ extern void rxrpc_process_local_events(struct rxrpc_local *);
861891
/*
862892
* local_object.c
863893
*/
864-
struct rxrpc_local *rxrpc_lookup_local(const struct sockaddr_rxrpc *);
894+
struct rxrpc_local *rxrpc_lookup_local(struct net *, const struct sockaddr_rxrpc *);
865895
void __rxrpc_put_local(struct rxrpc_local *);
866-
void __exit rxrpc_destroy_all_locals(void);
896+
void rxrpc_destroy_all_locals(struct rxrpc_net *);
867897

868898
static inline void rxrpc_get_local(struct rxrpc_local *local)
869899
{
@@ -901,6 +931,17 @@ extern unsigned int rxrpc_resend_timeout;
901931

902932
extern const s8 rxrpc_ack_priority[];
903933

934+
/*
935+
* net_ns.c
936+
*/
937+
extern unsigned int rxrpc_net_id;
938+
extern struct pernet_operations rxrpc_net_ops;
939+
940+
static inline struct rxrpc_net *rxrpc_net(struct net *net)
941+
{
942+
return net_generic(net, rxrpc_net_id);
943+
}
944+
904945
/*
905946
* output.c
906947
*/

net/rxrpc/call_accept.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
3838
{
3939
const void *here = __builtin_return_address(0);
4040
struct rxrpc_call *call;
41+
struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
4142
int max, tmp;
4243
unsigned int size = RXRPC_BACKLOG_MAX;
4344
unsigned int head, tail, call_head, call_tail;
@@ -79,7 +80,7 @@ static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
7980
if (CIRC_CNT(head, tail, size) < max) {
8081
struct rxrpc_connection *conn;
8182

82-
conn = rxrpc_prealloc_service_connection(gfp);
83+
conn = rxrpc_prealloc_service_connection(rxnet, gfp);
8384
if (!conn)
8485
return -ENOMEM;
8586
b->conn_backlog[head] = conn;
@@ -136,9 +137,9 @@ static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
136137

137138
write_unlock(&rx->call_lock);
138139

139-
write_lock(&rxrpc_call_lock);
140-
list_add_tail(&call->link, &rxrpc_calls);
141-
write_unlock(&rxrpc_call_lock);
140+
write_lock(&rxnet->call_lock);
141+
list_add_tail(&call->link, &rxnet->calls);
142+
write_unlock(&rxnet->call_lock);
142143

143144
b->call_backlog[call_head] = call;
144145
smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
@@ -185,6 +186,7 @@ int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
185186
void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
186187
{
187188
struct rxrpc_backlog *b = rx->backlog;
189+
struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
188190
unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
189191

190192
if (!b)
@@ -209,10 +211,10 @@ void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
209211
tail = b->conn_backlog_tail;
210212
while (CIRC_CNT(head, tail, size) > 0) {
211213
struct rxrpc_connection *conn = b->conn_backlog[tail];
212-
write_lock(&rxrpc_connection_lock);
214+
write_lock(&rxnet->conn_lock);
213215
list_del(&conn->link);
214216
list_del(&conn->proc_link);
215-
write_unlock(&rxrpc_connection_lock);
217+
write_unlock(&rxnet->conn_lock);
216218
kfree(conn);
217219
tail = (tail + 1) & (size - 1);
218220
}

0 commit comments

Comments
 (0)