1 /* RxRPC remote transport endpoint record management
3 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/udp.h>
19 #include <linux/slab.h>
20 #include <linux/hashtable.h>
22 #include <net/af_rxrpc.h>
24 #include <net/route.h>
25 #include "ar-internal.h"
27 static DEFINE_HASHTABLE(rxrpc_peer_hash
, 10);
28 static DEFINE_SPINLOCK(rxrpc_peer_hash_lock
);
33 static unsigned long rxrpc_peer_hash_key(struct rxrpc_local
*local
,
34 const struct sockaddr_rxrpc
*srx
)
38 unsigned long hash_key
;
42 hash_key
= (unsigned long)local
/ __alignof__(*local
);
43 hash_key
+= srx
->transport_type
;
44 hash_key
+= srx
->transport_len
;
45 hash_key
+= srx
->transport
.family
;
47 switch (srx
->transport
.family
) {
49 hash_key
+= (u16 __force
)srx
->transport
.sin
.sin_port
;
50 size
= sizeof(srx
->transport
.sin
.sin_addr
);
51 p
= (u16
*)&srx
->transport
.sin
.sin_addr
;
55 /* Step through the peer address in 16-bit portions for speed */
56 for (i
= 0; i
< size
; i
+= sizeof(*p
), p
++)
59 _leave(" 0x%lx", hash_key
);
64 * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
67 * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
68 * buckets and mid-bucket insertion, so we don't make full use of this
69 * information at this point.
71 static long rxrpc_peer_cmp_key(const struct rxrpc_peer
*peer
,
72 struct rxrpc_local
*local
,
73 const struct sockaddr_rxrpc
*srx
,
74 unsigned long hash_key
)
78 diff
= ((peer
->hash_key
- hash_key
) ?:
79 ((unsigned long)peer
->local
- (unsigned long)local
) ?:
80 (peer
->srx
.transport_type
- srx
->transport_type
) ?:
81 (peer
->srx
.transport_len
- srx
->transport_len
) ?:
82 (peer
->srx
.transport
.family
- srx
->transport
.family
));
86 switch (srx
->transport
.family
) {
88 return ((u16 __force
)peer
->srx
.transport
.sin
.sin_port
-
89 (u16 __force
)srx
->transport
.sin
.sin_port
) ?:
90 memcmp(&peer
->srx
.transport
.sin
.sin_addr
,
91 &srx
->transport
.sin
.sin_addr
,
92 sizeof(struct in_addr
));
99 * Look up a remote transport endpoint for the specified address using RCU.
101 static struct rxrpc_peer
*__rxrpc_lookup_peer_rcu(
102 struct rxrpc_local
*local
,
103 const struct sockaddr_rxrpc
*srx
,
104 unsigned long hash_key
)
106 struct rxrpc_peer
*peer
;
108 hash_for_each_possible_rcu(rxrpc_peer_hash
, peer
, hash_link
, hash_key
) {
109 if (rxrpc_peer_cmp_key(peer
, local
, srx
, hash_key
) == 0) {
110 if (atomic_read(&peer
->usage
) == 0)
120 * Look up a remote transport endpoint for the specified address using RCU.
122 struct rxrpc_peer
*rxrpc_lookup_peer_rcu(struct rxrpc_local
*local
,
123 const struct sockaddr_rxrpc
*srx
)
125 struct rxrpc_peer
*peer
;
126 unsigned long hash_key
= rxrpc_peer_hash_key(local
, srx
);
128 peer
= __rxrpc_lookup_peer_rcu(local
, srx
, hash_key
);
130 switch (srx
->transport
.family
) {
132 _net("PEER %d {%d,%u,%pI4+%hu}",
134 peer
->srx
.transport_type
,
135 peer
->srx
.transport
.family
,
136 &peer
->srx
.transport
.sin
.sin_addr
,
137 ntohs(peer
->srx
.transport
.sin
.sin_port
));
141 _leave(" = %p {u=%d}", peer
, atomic_read(&peer
->usage
));
147 * assess the MTU size for the network interface through which this peer is
150 static void rxrpc_assess_MTU_size(struct rxrpc_peer
*peer
)
157 rt
= ip_route_output_ports(&init_net
, &fl4
, NULL
,
158 peer
->srx
.transport
.sin
.sin_addr
.s_addr
, 0,
159 htons(7000), htons(7001),
162 _leave(" [route err %ld]", PTR_ERR(rt
));
166 peer
->if_mtu
= dst_mtu(&rt
->dst
);
167 dst_release(&rt
->dst
);
169 _leave(" [if_mtu %u]", peer
->if_mtu
);
175 struct rxrpc_peer
*rxrpc_alloc_peer(struct rxrpc_local
*local
, gfp_t gfp
)
177 struct rxrpc_peer
*peer
;
181 peer
= kzalloc(sizeof(struct rxrpc_peer
), gfp
);
183 atomic_set(&peer
->usage
, 1);
185 INIT_LIST_HEAD(&peer
->error_targets
);
186 spin_lock_init(&peer
->lock
);
187 peer
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
190 _leave(" = %p", peer
);
197 static struct rxrpc_peer
*rxrpc_create_peer(struct rxrpc_local
*local
,
198 struct sockaddr_rxrpc
*srx
,
199 unsigned long hash_key
,
202 struct rxrpc_peer
*peer
;
206 peer
= rxrpc_alloc_peer(local
, gfp
);
208 peer
->hash_key
= hash_key
;
209 memcpy(&peer
->srx
, srx
, sizeof(*srx
));
211 rxrpc_assess_MTU_size(peer
);
212 peer
->mtu
= peer
->if_mtu
;
214 if (srx
->transport
.family
== AF_INET
) {
215 peer
->hdrsize
= sizeof(struct iphdr
);
216 switch (srx
->transport_type
) {
218 peer
->hdrsize
+= sizeof(struct udphdr
);
228 peer
->hdrsize
+= sizeof(struct rxrpc_wire_header
);
229 peer
->maxdata
= peer
->mtu
- peer
->hdrsize
;
232 _leave(" = %p", peer
);
237 * obtain a remote transport endpoint for the specified address
239 struct rxrpc_peer
*rxrpc_lookup_peer(struct rxrpc_local
*local
,
240 struct sockaddr_rxrpc
*srx
, gfp_t gfp
)
242 struct rxrpc_peer
*peer
, *candidate
;
243 unsigned long hash_key
= rxrpc_peer_hash_key(local
, srx
);
245 _enter("{%d,%d,%pI4+%hu}",
248 &srx
->transport
.sin
.sin_addr
,
249 ntohs(srx
->transport
.sin
.sin_port
));
251 /* search the peer list first */
253 peer
= __rxrpc_lookup_peer_rcu(local
, srx
, hash_key
);
254 if (peer
&& !rxrpc_get_peer_maybe(peer
))
259 /* The peer is not yet present in hash - create a candidate
260 * for a new record and then redo the search.
262 candidate
= rxrpc_create_peer(local
, srx
, hash_key
, gfp
);
264 _leave(" = NULL [nomem]");
268 spin_lock(&rxrpc_peer_hash_lock
);
270 /* Need to check that we aren't racing with someone else */
271 peer
= __rxrpc_lookup_peer_rcu(local
, srx
, hash_key
);
272 if (peer
&& !rxrpc_get_peer_maybe(peer
))
275 hash_add_rcu(rxrpc_peer_hash
,
276 &candidate
->hash_link
, hash_key
);
278 spin_unlock(&rxrpc_peer_hash_lock
);
286 _net("PEER %d {%d,%pI4+%hu}",
288 peer
->srx
.transport_type
,
289 &peer
->srx
.transport
.sin
.sin_addr
,
290 ntohs(peer
->srx
.transport
.sin
.sin_port
));
292 _leave(" = %p {u=%d}", peer
, atomic_read(&peer
->usage
));
297 * Discard a ref on a remote peer record.
299 void __rxrpc_put_peer(struct rxrpc_peer
*peer
)
301 ASSERT(list_empty(&peer
->error_targets
));
303 spin_lock(&rxrpc_peer_hash_lock
);
304 hash_del_rcu(&peer
->hash_link
);
305 spin_unlock(&rxrpc_peer_hash_lock
);
307 kfree_rcu(peer
, rcu
);
This page took 0.039735 seconds and 5 git commands to generate.