Skip to content

Commit d6c0256

Browse files
lxindavem330
authored andcommitted
sctp: add the rhashtable apis for sctp global transport hashtable
tranport hashtbale will replace the association hashtable to do the lookup for transport, and then get association by t->assoc, rhashtable apis will be used because of it's resizable, scalable and using rcu. lport + rport + paddr will be the base hashkey to locate the chain, with net to protect one netns from another, then plus the laddr to compare to get the target. this patch will provider the lookup functions: - sctp_epaddr_lookup_transport - sctp_addrs_lookup_transport hash/unhash functions: - sctp_hash_transport - sctp_unhash_transport init/destroy functions: - sctp_transport_hashtable_init - sctp_transport_hashtable_destroy Signed-off-by: Xin Long <[email protected]> Signed-off-by: Marcelo Ricardo Leitner <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6a5ef90 commit d6c0256

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

include/net/sctp/sctp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ void sctp_icmp_proto_unreachable(struct sock *sk,
143143
struct sctp_transport *t);
144144
void sctp_backlog_migrate(struct sctp_association *assoc,
145145
struct sock *oldsk, struct sock *newsk);
146+
int sctp_transport_hashtable_init(void);
147+
void sctp_transport_hashtable_destroy(void);
148+
void sctp_hash_transport(struct sctp_transport *t);
149+
void sctp_unhash_transport(struct sctp_transport *t);
150+
struct sctp_transport *sctp_addrs_lookup_transport(
151+
struct net *net,
152+
const union sctp_addr *laddr,
153+
const union sctp_addr *paddr);
154+
struct sctp_transport *sctp_epaddr_lookup_transport(
155+
const struct sctp_endpoint *ep,
156+
const union sctp_addr *paddr);
146157

147158
/*
148159
* sctp/proc.c

include/net/sctp/structs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define __sctp_structs_h__
4949

5050
#include <linux/ktime.h>
51+
#include <linux/rhashtable.h>
5152
#include <linux/socket.h> /* linux/in.h needs this!! */
5253
#include <linux/in.h> /* We get struct sockaddr_in. */
5354
#include <linux/in6.h> /* We get struct in6_addr */
@@ -123,6 +124,8 @@ extern struct sctp_globals {
123124
struct sctp_hashbucket *assoc_hashtable;
124125
/* This is the sctp port control hash. */
125126
struct sctp_bind_hashbucket *port_hashtable;
127+
/* This is the hash of all transports. */
128+
struct rhashtable transport_hashtable;
126129

127130
/* Sizes of above hashtables. */
128131
int ep_hashsize;
@@ -147,6 +150,7 @@ extern struct sctp_globals {
147150
#define sctp_assoc_hashtable (sctp_globals.assoc_hashtable)
148151
#define sctp_port_hashsize (sctp_globals.port_hashsize)
149152
#define sctp_port_hashtable (sctp_globals.port_hashtable)
153+
#define sctp_transport_hashtable (sctp_globals.transport_hashtable)
150154
#define sctp_checksum_disable (sctp_globals.checksum_disable)
151155

152156
/* SCTP Socket type: UDP or TCP style. */
@@ -753,6 +757,7 @@ static inline int sctp_packet_empty(struct sctp_packet *packet)
753757
struct sctp_transport {
754758
/* A list of transports. */
755759
struct list_head transports;
760+
struct rhash_head node;
756761

757762
/* Reference counting. */
758763
atomic_t refcnt;

net/sctp/input.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,137 @@ static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
782782
return ep;
783783
}
784784

785+
/* rhashtable for transport */
786+
struct sctp_hash_cmp_arg {
787+
const union sctp_addr *laddr;
788+
const union sctp_addr *paddr;
789+
const struct net *net;
790+
};
791+
792+
static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
793+
const void *ptr)
794+
{
795+
const struct sctp_hash_cmp_arg *x = arg->key;
796+
const struct sctp_transport *t = ptr;
797+
struct sctp_association *asoc = t->asoc;
798+
const struct net *net = x->net;
799+
800+
if (x->laddr->v4.sin_port != htons(asoc->base.bind_addr.port))
801+
return 1;
802+
if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
803+
return 1;
804+
if (!net_eq(sock_net(asoc->base.sk), net))
805+
return 1;
806+
if (!sctp_bind_addr_match(&asoc->base.bind_addr,
807+
x->laddr, sctp_sk(asoc->base.sk)))
808+
return 1;
809+
810+
return 0;
811+
}
812+
813+
static inline u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
814+
{
815+
const struct sctp_transport *t = data;
816+
const union sctp_addr *paddr = &t->ipaddr;
817+
const struct net *net = sock_net(t->asoc->base.sk);
818+
u16 lport = htons(t->asoc->base.bind_addr.port);
819+
u32 addr;
820+
821+
if (paddr->sa.sa_family == AF_INET6)
822+
addr = jhash(&paddr->v6.sin6_addr, 16, seed);
823+
else
824+
addr = paddr->v4.sin_addr.s_addr;
825+
826+
return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
827+
(__force __u32)lport, net_hash_mix(net), seed);
828+
}
829+
830+
static inline u32 sctp_hash_key(const void *data, u32 len, u32 seed)
831+
{
832+
const struct sctp_hash_cmp_arg *x = data;
833+
const union sctp_addr *paddr = x->paddr;
834+
const struct net *net = x->net;
835+
u16 lport = x->laddr->v4.sin_port;
836+
u32 addr;
837+
838+
if (paddr->sa.sa_family == AF_INET6)
839+
addr = jhash(&paddr->v6.sin6_addr, 16, seed);
840+
else
841+
addr = paddr->v4.sin_addr.s_addr;
842+
843+
return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
844+
(__force __u32)lport, net_hash_mix(net), seed);
845+
}
846+
847+
static const struct rhashtable_params sctp_hash_params = {
848+
.head_offset = offsetof(struct sctp_transport, node),
849+
.hashfn = sctp_hash_key,
850+
.obj_hashfn = sctp_hash_obj,
851+
.obj_cmpfn = sctp_hash_cmp,
852+
.automatic_shrinking = true,
853+
};
854+
855+
int sctp_transport_hashtable_init(void)
856+
{
857+
return rhashtable_init(&sctp_transport_hashtable, &sctp_hash_params);
858+
}
859+
860+
void sctp_transport_hashtable_destroy(void)
861+
{
862+
rhashtable_destroy(&sctp_transport_hashtable);
863+
}
864+
865+
void sctp_hash_transport(struct sctp_transport *t)
866+
{
867+
struct sctp_sockaddr_entry *addr;
868+
struct sctp_hash_cmp_arg arg;
869+
870+
addr = list_entry(t->asoc->base.bind_addr.address_list.next,
871+
struct sctp_sockaddr_entry, list);
872+
arg.laddr = &addr->a;
873+
arg.paddr = &t->ipaddr;
874+
arg.net = sock_net(t->asoc->base.sk);
875+
876+
reinsert:
877+
if (rhashtable_lookup_insert_key(&sctp_transport_hashtable, &arg,
878+
&t->node, sctp_hash_params) == -EBUSY)
879+
goto reinsert;
880+
}
881+
882+
void sctp_unhash_transport(struct sctp_transport *t)
883+
{
884+
rhashtable_remove_fast(&sctp_transport_hashtable, &t->node,
885+
sctp_hash_params);
886+
}
887+
888+
struct sctp_transport *sctp_addrs_lookup_transport(
889+
struct net *net,
890+
const union sctp_addr *laddr,
891+
const union sctp_addr *paddr)
892+
{
893+
struct sctp_hash_cmp_arg arg = {
894+
.laddr = laddr,
895+
.paddr = paddr,
896+
.net = net,
897+
};
898+
899+
return rhashtable_lookup_fast(&sctp_transport_hashtable, &arg,
900+
sctp_hash_params);
901+
}
902+
903+
struct sctp_transport *sctp_epaddr_lookup_transport(
904+
const struct sctp_endpoint *ep,
905+
const union sctp_addr *paddr)
906+
{
907+
struct sctp_sockaddr_entry *addr;
908+
struct net *net = sock_net(ep->base.sk);
909+
910+
addr = list_entry(ep->base.bind_addr.address_list.next,
911+
struct sctp_sockaddr_entry, list);
912+
913+
return sctp_addrs_lookup_transport(net, &addr->a, paddr);
914+
}
915+
785916
/* Insert association into the hash table. */
786917
static void __sctp_hash_established(struct sctp_association *asoc)
787918
{

0 commit comments

Comments
 (0)