#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"
#ifdef TCP_OFFLOAD
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/fnv_hash.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sbuf.h>
#include <sys/taskqueue.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/ethernet.h>
#include <net/if_vlan_var.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_pcb.h>
#include <netinet/tcp_var.h>
#include <netinet/toecore.h>
#include "common/common.h"
#include "common/t4_msg.h"
#include "tom/t4_tom_l2t.h"
#include "tom/t4_tom.h"
#define VLAN_NONE 0xfff
static inline void
l2t_hold(struct l2t_data *d, struct l2t_entry *e)
{
if (atomic_fetchadd_int(&e->refcnt, 1) == 0)
atomic_subtract_int(&d->nfree, 1);
}
static inline u_int
l2_hash(struct l2t_data *d, const struct sockaddr *sa, int ifindex)
{
u_int hash, half = d->l2t_size / 2, start = 0;
const void *key;
size_t len;
KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
("%s: sa %p has unexpected sa_family %d", __func__, sa,
sa->sa_family));
if (sa->sa_family == AF_INET) {
const struct sockaddr_in *sin = (const void *)sa;
key = &sin->sin_addr;
len = sizeof(sin->sin_addr);
} else {
const struct sockaddr_in6 *sin6 = (const void *)sa;
key = &sin6->sin6_addr;
len = sizeof(sin6->sin6_addr);
start = half;
}
hash = fnv_32_buf(key, len, FNV1_32_INIT);
hash = fnv_32_buf(&ifindex, sizeof(ifindex), hash);
hash %= half;
return (hash + start);
}
static inline int
l2_cmp(const struct sockaddr *sa, struct l2t_entry *e)
{
KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
("%s: sa %p has unexpected sa_family %d", __func__, sa,
sa->sa_family));
if (sa->sa_family == AF_INET) {
const struct sockaddr_in *sin = (const void *)sa;
return (e->addr[0] != sin->sin_addr.s_addr);
} else {
const struct sockaddr_in6 *sin6 = (const void *)sa;
return (memcmp(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr)));
}
}
static inline void
l2_store(const struct sockaddr *sa, struct l2t_entry *e)
{
KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
("%s: sa %p has unexpected sa_family %d", __func__, sa,
sa->sa_family));
if (sa->sa_family == AF_INET) {
const struct sockaddr_in *sin = (const void *)sa;
e->addr[0] = sin->sin_addr.s_addr;
e->ipv6 = 0;
} else {
const struct sockaddr_in6 *sin6 = (const void *)sa;
memcpy(&e->addr[0], &sin6->sin6_addr, sizeof(e->addr));
e->ipv6 = 1;
}
}
static inline void
arpq_enqueue(struct l2t_entry *e, struct wrqe *wr)
{
mtx_assert(&e->lock, MA_OWNED);
STAILQ_INSERT_TAIL(&e->wr_list, wr, link);
}
static inline void
send_pending(struct adapter *sc, struct l2t_entry *e)
{
struct wrqe *wr;
mtx_assert(&e->lock, MA_OWNED);
while ((wr = STAILQ_FIRST(&e->wr_list)) != NULL) {
STAILQ_REMOVE_HEAD(&e->wr_list, link);
t4_wrq_tx(sc, wr);
}
}
static void
resolution_failed(struct adapter *sc, struct l2t_entry *e)
{
struct tom_data *td = sc->tom_softc;
mtx_assert(&e->lock, MA_OWNED);
mtx_lock(&td->unsent_wr_lock);
STAILQ_CONCAT(&td->unsent_wr_list, &e->wr_list);
mtx_unlock(&td->unsent_wr_lock);
taskqueue_enqueue(taskqueue_thread, &td->reclaim_wr_resources);
}
static void
update_entry(struct adapter *sc, struct l2t_entry *e, uint8_t *lladdr,
uint16_t vtag)
{
mtx_assert(&e->lock, MA_OWNED);
if (lladdr == NULL &&
(e->state == L2T_STATE_RESOLVING || e->state == L2T_STATE_FAILED)) {
e->state = L2T_STATE_FAILED;
resolution_failed(sc, e);
return;
} else if (lladdr == NULL) {
KASSERT(e->state == L2T_STATE_VALID ||
e->state == L2T_STATE_STALE,
("%s: lladdr NULL, state %d", __func__, e->state));
e->state = L2T_STATE_STALE;
} else if (e->state == L2T_STATE_RESOLVING ||
e->state == L2T_STATE_FAILED ||
memcmp(e->dmac, lladdr, ETHER_ADDR_LEN)) {
memcpy(e->dmac, lladdr, ETHER_ADDR_LEN);
e->vlan = vtag;
if (t4_write_l2e(e, 1) == 0)
e->state = L2T_STATE_VALID;
} else
e->state = L2T_STATE_VALID;
}
static int
resolve_entry(struct adapter *sc, struct l2t_entry *e)
{
struct tom_data *td = sc->tom_softc;
struct toedev *tod = &td->tod;
struct sockaddr_in sin = {0};
struct sockaddr_in6 sin6 = {0};
struct sockaddr *sa;
uint8_t dmac[ETHER_HDR_LEN];
uint16_t vtag;
int rc;
if (e->ipv6 == 0) {
sin.sin_family = AF_INET;
sin.sin_len = sizeof(struct sockaddr_in);
sin.sin_addr.s_addr = e->addr[0];
sa = (void *)&sin;
} else {
sin6.sin6_family = AF_INET6;
sin6.sin6_len = sizeof(struct sockaddr_in6);
memcpy(&sin6.sin6_addr, &e->addr[0], sizeof(e->addr));
sa = (void *)&sin6;
}
vtag = EVL_MAKETAG(VLAN_NONE, 0, 0);
rc = toe_l2_resolve(tod, e->ifp, sa, dmac, &vtag);
if (rc == EWOULDBLOCK)
return (rc);
mtx_lock(&e->lock);
update_entry(sc, e, rc == 0 ? dmac : NULL, vtag);
mtx_unlock(&e->lock);
return (rc);
}
int
t4_l2t_send_slow(struct adapter *sc, struct wrqe *wr, struct l2t_entry *e)
{
again:
switch (e->state) {
case L2T_STATE_STALE:
resolve_entry(sc, e);
case L2T_STATE_VALID:
t4_wrq_tx(sc, wr);
return (0);
case L2T_STATE_RESOLVING:
case L2T_STATE_SYNC_WRITE:
mtx_lock(&e->lock);
if (e->state != L2T_STATE_SYNC_WRITE &&
e->state != L2T_STATE_RESOLVING) {
mtx_unlock(&e->lock);
goto again;
}
if (!hw_all_ok(sc))
free(wr, M_CXGBE);
else
arpq_enqueue(e, wr);
mtx_unlock(&e->lock);
if (resolve_entry(sc, e) == EWOULDBLOCK)
break;
mtx_lock(&e->lock);
if (e->state == L2T_STATE_VALID && !STAILQ_EMPTY(&e->wr_list))
send_pending(sc, e);
if (e->state == L2T_STATE_FAILED)
resolution_failed(sc, e);
mtx_unlock(&e->lock);
break;
case L2T_STATE_FAILED:
return (EHOSTUNREACH);
}
return (0);
}
int
do_l2t_write_rpl2(struct sge_iq *iq, const struct rss_header *rss,
struct mbuf *m)
{
struct adapter *sc = iq->adapter;
const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
const u_int hwidx = GET_TID(rpl) & ~(F_SYNC_WR | V_TID_QID(M_TID_QID));
const bool sync = GET_TID(rpl) & F_SYNC_WR;
MPASS(iq->abs_id == G_TID_QID(GET_TID(rpl)));
if (__predict_false(hwidx < sc->vres.l2t.start) ||
__predict_false(hwidx >= sc->vres.l2t.start + sc->vres.l2t.size) ||
__predict_false(rpl->status != CPL_ERR_NONE)) {
CH_ERR(sc, "%s: hwidx %u, rpl %u, sync %u; L2T st %u, sz %u\n",
__func__, hwidx, rpl->status, sync, sc->vres.l2t.start,
sc->vres.l2t.size);
return (EINVAL);
}
if (sync) {
const u_int idx = hwidx - sc->vres.l2t.start;
struct l2t_entry *e = &sc->l2t->l2tab[idx];
mtx_lock(&e->lock);
if (e->state != L2T_STATE_SWITCHING) {
send_pending(sc, e);
e->state = L2T_STATE_VALID;
}
mtx_unlock(&e->lock);
}
return (0);
}
struct l2t_entry *
t4_l2t_get(struct port_info *pi, if_t ifp, struct sockaddr *sa)
{
struct l2t_entry *e;
struct adapter *sc = pi->adapter;
struct l2t_data *d = sc->l2t;
u_int hash;
uint16_t vid, pcp, vtag;
KASSERT(sa->sa_family == AF_INET || sa->sa_family == AF_INET6,
("%s: sa %p has unexpected sa_family %d", __func__, sa,
sa->sa_family));
vid = VLAN_NONE;
pcp = 0;
if (if_gettype(ifp) == IFT_L2VLAN) {
VLAN_TAG(ifp, &vid);
VLAN_PCP(ifp, &pcp);
} else if ((pcp = if_getpcp(ifp)) != IFNET_PCP_NONE)
vid = 0;
else
pcp = 0;
vtag = EVL_MAKETAG(vid, pcp, 0);
hash = l2_hash(d, sa, if_getindex(ifp));
rw_wlock(&d->lock);
if (__predict_false(d->l2t_stopped)) {
e = NULL;
goto done;
}
for (e = d->l2tab[hash].first; e; e = e->next) {
if (l2_cmp(sa, e) == 0 && e->ifp == ifp && e->vlan == vtag) {
l2t_hold(d, e);
goto done;
}
}
e = t4_alloc_l2e(d);
if (e) {
mtx_lock(&e->lock);
e->next = d->l2tab[hash].first;
d->l2tab[hash].first = e;
e->state = L2T_STATE_RESOLVING;
l2_store(sa, e);
e->ifp = ifp;
e->hash = hash;
e->hw_port = pi->hw_port;
e->wrq = &sc->sge.ctrlq[pi->port_id];
e->iqid = sc->sge.ofld_rxq[pi->vi[0].first_ofld_rxq].iq.abs_id;
atomic_store_rel_int(&e->refcnt, 1);
e->vlan = vtag;
mtx_unlock(&e->lock);
}
done:
rw_wunlock(&d->lock);
return e;
}
void
t4_l2_update(struct toedev *tod, if_t ifp, struct sockaddr *sa,
uint8_t *lladdr, uint16_t vtag)
{
struct adapter *sc = tod->tod_softc;
struct l2t_entry *e;
struct l2t_data *d = sc->l2t;
u_int hash;
KASSERT(d != NULL, ("%s: no L2 table", __func__));
hash = l2_hash(d, sa, if_getindex(ifp));
rw_rlock(&d->lock);
if (__predict_false(d->l2t_stopped))
goto done;
for (e = d->l2tab[hash].first; e; e = e->next) {
if (l2_cmp(sa, e) == 0 && e->ifp == ifp) {
mtx_lock(&e->lock);
if (atomic_load_acq_int(&e->refcnt))
goto found;
if (e->state == L2T_STATE_VALID)
e->state = L2T_STATE_STALE;
mtx_unlock(&e->lock);
break;
}
}
done:
rw_runlock(&d->lock);
return;
found:
rw_runlock(&d->lock);
KASSERT(e->state != L2T_STATE_UNUSED,
("%s: unused entry in the hash.", __func__));
update_entry(sc, e, lladdr, vtag);
mtx_unlock(&e->lock);
}
#endif