Store addresses instead of sockaddrs inside llentry.

This permits us having all (not fully true yet) all the info
needed in lookup process in first 64 bytes of 'struct llentry'.

struct llentry layout:
BEFORE:
[rwlock .. state .. state .. MAC ] (lle+1) [sockaddr_in[6]]
AFTER
[ in[6]_addr MAC .. state .. rwlock ]

Currently, address part of struct llentry has only 16 bytes for the key.
However, lltable does not restrict any custom lltable consumers with long
keys use the previous approach (store key at (lle+1)).

Sponsored by:	Yandex LLC
This commit is contained in:
Alexander V. Chernikov 2015-08-11 09:26:11 +00:00
parent 6be7d38913
commit 314294de5c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=286624
7 changed files with 52 additions and 66 deletions

View File

@ -54,7 +54,18 @@ extern struct rwlock lltable_rwlock;
*/
struct llentry {
LIST_ENTRY(llentry) lle_next;
struct rwlock lle_lock;
union {
struct in_addr addr4;
struct in6_addr addr6;
} r_l3addr;
union {
uint64_t mac_aligned;
uint16_t mac16[3];
uint8_t mac8[20]; /* IB needs 20 bytes. */
} ll_addr;
uint32_t spare0;
uint64_t spare1;
struct lltable *lle_tbl;
struct llentries *lle_head;
void (*lle_free)(struct llentry *);
@ -70,19 +81,13 @@ struct llentry {
time_t ln_ntick;
int lle_refcnt;
union {
uint64_t mac_aligned;
uint16_t mac16[3];
uint8_t mac8[20]; /* IB needs 20 bytes. */
} ll_addr;
LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */
/* XXX af-private? */
union {
struct callout ln_timer_ch;
struct callout la_timer;
} lle_timer;
/* NB: struct sockaddr must immediately follow */
struct rwlock lle_lock;
};
#define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock)
@ -133,11 +138,6 @@ struct llentry {
#define ln_timer_ch lle_timer.ln_timer_ch
#define la_timer lle_timer.la_timer
/* XXX bad name */
#define L3_CADDR(lle) ((const struct sockaddr *)(&lle[1]))
#define L3_ADDR(lle) ((struct sockaddr *)(&lle[1]))
#define L3_ADDR_LEN(lle) (((struct sockaddr *)(&lle[1]))->sa_len)
typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
const struct sockaddr *l3addr);
typedef struct llentry *(llt_create_t)(struct lltable *, u_int flags,

View File

@ -813,7 +813,7 @@ in_arpinput(struct mbuf *m)
m_hold = la->la_hold;
la->la_hold = NULL;
la->la_numheld = 0;
memcpy(&sa, L3_ADDR(la), sizeof(sa));
lltable_fill_sa_entry(la, (struct sockaddr *)&sa);
LLE_WUNLOCK(la);
for (; m_hold != NULL; m_hold = m_hold_next) {
m_hold_next = m_hold->m_nextpkt;

View File

@ -958,7 +958,6 @@ in_purgemaddrs(struct ifnet *ifp)
struct in_llentry {
struct llentry base;
struct sockaddr_in l3_addr4;
};
#define IN_LLTBL_DEFAULT_HSIZE 32
@ -980,7 +979,7 @@ in_lltable_destroy_lle(struct llentry *lle)
}
static struct llentry *
in_lltable_new(const struct sockaddr *l3addr, u_int flags)
in_lltable_new(struct in_addr addr4, u_int flags)
{
struct in_llentry *lle;
@ -993,7 +992,7 @@ in_lltable_new(const struct sockaddr *l3addr, u_int flags)
* an ARP request.
*/
lle->base.la_expire = time_uptime; /* mark expired */
lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
lle->base.r_l3addr.addr4 = addr4;
lle->base.lle_refcnt = 1;
lle->base.lle_free = in_lltable_destroy_lle;
LLE_LOCK_INIT(&lle->base);
@ -1003,7 +1002,7 @@ in_lltable_new(const struct sockaddr *l3addr, u_int flags)
}
#define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
(((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
(((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
static int
in_lltable_match_prefix(const struct sockaddr *prefix,
@ -1016,7 +1015,7 @@ in_lltable_match_prefix(const struct sockaddr *prefix,
* (flags & LLE_STATIC) means deleting all entries
* including static ARP entries.
*/
if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)), pfx, msk) &&
if (IN_ARE_MASKED_ADDR_EQUAL(lle->r_l3addr.addr4, pfx, msk) &&
((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
return (1);
@ -1132,11 +1131,8 @@ in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
static uint32_t
in_lltable_hash(const struct llentry *lle, uint32_t hsize)
{
const struct sockaddr_in *sin;
sin = (const struct sockaddr_in *)(L3_CADDR(lle));
return (in_lltable_hash_dst(sin->sin_addr, hsize));
return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
}
static void
@ -1148,7 +1144,7 @@ in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
bzero(sin, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_len = sizeof(*sin);
sin->sin_addr = ((const struct sockaddr_in *)(L3_CADDR(lle)))->sin_addr;
sin->sin_addr = lle->r_l3addr.addr4;
}
static inline struct llentry *
@ -1156,16 +1152,14 @@ in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
{
struct llentry *lle;
struct llentries *lleh;
struct sockaddr_in *sin;
u_int hashidx;
hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
lleh = &llt->lle_head[hashidx];
LIST_FOREACH(lle, lleh, lle_next) {
sin = satosin(L3_ADDR(lle));
if (lle->la_flags & LLE_DELETED)
continue;
if (sin->sin_addr.s_addr == dst.s_addr)
if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
break;
}
@ -1236,7 +1230,7 @@ in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3add
in_lltable_rtcheck(ifp, flags, l3addr) != 0)
return (NULL);
lle = in_lltable_new(l3addr, flags);
lle = in_lltable_new(sin->sin_addr, flags);
if (lle == NULL) {
log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
return (NULL);

View File

@ -390,27 +390,30 @@ toe_lle_event(void *arg __unused, struct llentry *lle, int evt)
struct sockaddr *sa;
uint8_t *lladdr;
uint16_t vtag;
int family;
struct sockaddr_in6 sin6;
LLE_WLOCK_ASSERT(lle);
ifp = lle->lle_tbl->llt_ifp;
sa = L3_ADDR(lle);
KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
("%s: lle_event %d for lle %p but sa %p !INET && !INET6",
__func__, evt, lle, sa));
ifp = lltable_get_ifp(lle->lle_tbl);
family = lltable_get_af(lle->lle_tbl);
if (family != AF_INET && family != AF_INET6)
return;
/*
* Not interested if the interface's TOE capability is not enabled.
*/
if ((sa->sa_family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) ||
(sa->sa_family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)))
if ((family == AF_INET && !(ifp->if_capenable & IFCAP_TOE4)) ||
(family == AF_INET6 && !(ifp->if_capenable & IFCAP_TOE6)))
return;
tod = TOEDEV(ifp);
if (tod == NULL)
return;
sa = (struct sockaddr *)&sin6;
lltable_fill_sa_entry(lle, sa);
vtag = 0xfff;
if (evt != LLENTRY_RESOLVED) {

View File

@ -2047,7 +2047,6 @@ in6_if2idlen(struct ifnet *ifp)
struct in6_llentry {
struct llentry base;
struct sockaddr_in6 l3_addr6;
};
#define IN6_LLTBL_DEFAULT_HSIZE 32
@ -2069,7 +2068,7 @@ in6_lltable_destroy_lle(struct llentry *lle)
}
static struct llentry *
in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
in6_lltable_new(const struct in6_addr *addr6, u_int flags)
{
struct in6_llentry *lle;
@ -2077,7 +2076,7 @@ in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
if (lle == NULL) /* NB: caller generates msg */
return NULL;
lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
lle->base.r_l3addr.addr6 = *addr6;
lle->base.lle_refcnt = 1;
lle->base.lle_free = in6_lltable_destroy_lle;
LLE_LOCK_INIT(&lle->base);
@ -2093,7 +2092,7 @@ in6_lltable_match_prefix(const struct sockaddr *prefix,
const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
if (IN6_ARE_MASKED_ADDR_EQUAL(&satosin6(L3_ADDR(lle))->sin6_addr,
if (IN6_ARE_MASKED_ADDR_EQUAL(&lle->r_l3addr.addr6,
&pfx->sin6_addr, &msk->sin6_addr) &&
((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
return (1);
@ -2172,11 +2171,8 @@ in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
static uint32_t
in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
{
const struct sockaddr_in6 *sin6;
sin6 = (const struct sockaddr_in6 *)L3_CADDR(lle);
return (in6_lltable_hash_dst(&sin6->sin6_addr, hsize));
return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
}
static void
@ -2188,7 +2184,7 @@ in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
bzero(sin6, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_len = sizeof(*sin6);
sin6->sin6_addr =((const struct sockaddr_in6*)L3_CADDR(lle))->sin6_addr;
sin6->sin6_addr = lle->r_l3addr.addr6;
}
static inline struct llentry *
@ -2196,16 +2192,14 @@ in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
{
struct llentry *lle;
struct llentries *lleh;
const struct sockaddr_in6 *sin6;
u_int hashidx;
hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
lleh = &llt->lle_head[hashidx];
LIST_FOREACH(lle, lleh, lle_next) {
sin6 = (const struct sockaddr_in6 *)L3_CADDR(lle);
if (lle->la_flags & LLE_DELETED)
continue;
if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, dst))
if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
break;
}
@ -2272,7 +2266,7 @@ in6_lltable_create(struct lltable *llt, u_int flags,
in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
return (NULL);
lle = in6_lltable_new(l3addr, flags);
lle = in6_lltable_new(&sin6->sin6_addr, flags);
if (lle == NULL) {
log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
return (NULL);

View File

@ -66,7 +66,6 @@ __FBSDID("$FreeBSD$");
#include <netinet/in.h>
#include <netinet/in_kdtrace.h>
#include <net/if_llatbl.h>
#define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
#include <netinet/if_ether.h>
#include <netinet6/in6_var.h>
#include <netinet/ip6.h>
@ -150,13 +149,16 @@ static void
nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
{
struct rt_addrinfo rtinfo;
struct sockaddr_in6 dst, *sa6;
struct sockaddr_in6 dst;
struct sockaddr_dl gw;
struct ifnet *ifp;
int type;
LLE_WLOCK_ASSERT(lle);
if (lltable_get_af(lle->lle_tbl) != AF_INET6)
return;
switch (evt) {
case LLENTRY_RESOLVED:
type = RTM_ADD;
@ -170,20 +172,14 @@ nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
return;
}
sa6 = L3_ADDR_SIN6(lle);
if (sa6->sin6_family != AF_INET6)
return;
ifp = lle->lle_tbl->llt_ifp;
ifp = lltable_get_ifp(lle->lle_tbl);
bzero(&dst, sizeof(dst));
bzero(&gw, sizeof(gw));
bzero(&rtinfo, sizeof(rtinfo));
dst.sin6_len = sizeof(struct sockaddr_in6);
dst.sin6_family = AF_INET6;
dst.sin6_addr = sa6->sin6_addr;
lltable_fill_sa_entry(lle, (struct sockaddr *)&dst);
dst.sin6_scope_id = in6_getscopezone(ifp,
in6_addrscope(&sa6->sin6_addr));
in6_clearscope(&dst.sin6_addr); /* XXX */
in6_addrscope(&dst.sin6_addr));
gw.sdl_len = sizeof(struct sockaddr_dl);
gw.sdl_family = AF_LINK;
gw.sdl_alen = ifp->if_addrlen;
@ -569,7 +565,7 @@ nd6_llinfo_timer(void *arg)
}
ndi = ND_IFINFO(ifp);
dst = &L3_ADDR_SIN6(ln)->sin6_addr;
dst = &ln->r_l3addr.addr6;
if (ln->la_flags & LLE_STATIC) {
goto done;
}
@ -1122,7 +1118,7 @@ nd6_free(struct llentry *ln, int gc)
ifp = ln->lle_tbl->llt_ifp;
if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
dr = defrouter_lookup(&ln->r_l3addr.addr6, ifp);
if (dr != NULL && dr->expire &&
ln->ln_state == ND6_LLINFO_STALE && gc) {
@ -1183,7 +1179,7 @@ nd6_free(struct llentry *ln, int gc)
* is in the Default Router List.
* See a corresponding comment in nd6_na_input().
*/
rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
rt6_flush(&ln->r_l3addr.addr6, ifp);
}
if (dr) {
@ -1907,7 +1903,7 @@ nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
*chain = ln->la_hold;
ln->la_hold = NULL;
memcpy(sin6, L3_ADDR_SIN6(ln), sizeof(*sin6));
lltable_fill_sa_entry(ln, (struct sockaddr *)sin6);
if (ln->ln_state == ND6_LLINFO_STALE) {

View File

@ -68,7 +68,6 @@ __FBSDID("$FreeBSD$");
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <net/if_llatbl.h>
#define L3_ADDR_SIN6(le) ((struct sockaddr_in6 *) L3_ADDR(le))
#include <netinet6/in6_var.h>
#include <netinet6/in6_ifattach.h>
#include <netinet/ip6.h>
@ -898,7 +897,7 @@ nd6_na_input(struct mbuf *m, int off, int icmp6len)
struct nd_defrouter *dr;
struct in6_addr *in6;
in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
in6 = &ln->r_l3addr.addr6;
/*
* Lock to protect the default router list.