Skip to content

Commit

Permalink
mctp: Implement a timeout for tags
Browse files Browse the repository at this point in the history
Currently, a MCTP (local-eid,remote-eid,tag) tuple is allocated to a
socket on send, and only expires when the socket is closed.

This change introduces a tag timeout, freeing the tuple after a fixed
expiry - currently six seconds. This is greater than (but close to) the
max response timeout in upper-layer bindings.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Jeremy Kerr authored and David S. Miller committed Sep 29, 2021
1 parent 43f55f2 commit 7b14e15
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/net/mctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ struct mctp_sock {
* by sk->net->keys_lock
*/
struct hlist_head keys;

/* mechanism for expiring allocated keys; will release an allocated
* tag, and any netdev state for a request/response pairing
*/
struct timer_list key_expiry;
};

/* Key for matching incoming packets to sockets or reassembly contexts.
Expand Down Expand Up @@ -107,6 +112,8 @@ struct mctp_sock {
* the (complete) reply, or during reassembly errors. Here, we clean up
* the reassembly context (marking reasm_dead, to prevent another from
* starting), and remove the socket from the netns & socket lists.
*
* - through an expiry timeout, on a per-socket timer
*/
struct mctp_sk_key {
mctp_eid_t peer_addr;
Expand Down Expand Up @@ -138,6 +145,9 @@ struct mctp_sk_key {

/* key validity */
bool valid;

/* expiry timeout; valid (above) cleared on expiry */
unsigned long expiry;
};

struct mctp_skb_cb {
Expand Down
44 changes: 44 additions & 0 deletions net/mctp/af_mctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,60 @@ static const struct proto_ops mctp_dgram_ops = {
.sendpage = sock_no_sendpage,
};

static void mctp_sk_expire_keys(struct timer_list *timer)
{
struct mctp_sock *msk = container_of(timer, struct mctp_sock,
key_expiry);
struct net *net = sock_net(&msk->sk);
unsigned long next_expiry, flags;
struct mctp_sk_key *key;
struct hlist_node *tmp;
bool next_expiry_valid = false;

spin_lock_irqsave(&net->mctp.keys_lock, flags);

hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) {
spin_lock(&key->lock);

if (!time_after_eq(key->expiry, jiffies)) {
key->valid = false;
hlist_del_rcu(&key->hlist);
hlist_del_rcu(&key->sklist);
spin_unlock(&key->lock);
mctp_key_unref(key);
continue;
}

if (next_expiry_valid) {
if (time_before(key->expiry, next_expiry))
next_expiry = key->expiry;
} else {
next_expiry = key->expiry;
next_expiry_valid = true;
}
spin_unlock(&key->lock);
}

spin_unlock_irqrestore(&net->mctp.keys_lock, flags);

if (next_expiry_valid)
mod_timer(timer, next_expiry);
}

static int mctp_sk_init(struct sock *sk)
{
struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);

INIT_HLIST_HEAD(&msk->keys);
timer_setup(&msk->key_expiry, mctp_sk_expire_keys, 0);
return 0;
}

static void mctp_sk_close(struct sock *sk, long timeout)
{
struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk);

del_timer_sync(&msk->key_expiry);
sk_common_release(sk);
}

Expand Down
8 changes: 8 additions & 0 deletions net/mctp/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <net/sock.h>

static const unsigned int mctp_message_maxlen = 64 * 1024;
static const unsigned long mctp_key_lifetime = 6 * CONFIG_HZ;


/* route output callbacks */
static int mctp_route_discard(struct mctp_route *route, struct sk_buff *skb)
Expand Down Expand Up @@ -175,6 +177,9 @@ static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk)

if (!rc) {
refcount_inc(&key->refs);
key->expiry = jiffies + mctp_key_lifetime;
timer_reduce(&msk->key_expiry, key->expiry);

hlist_add_head(&key->hlist, &net->mctp.keys);
hlist_add_head(&key->sklist, &msk->keys);
}
Expand Down Expand Up @@ -497,6 +502,9 @@ static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,

lockdep_assert_held(&mns->keys_lock);

key->expiry = jiffies + mctp_key_lifetime;
timer_reduce(&msk->key_expiry, key->expiry);

/* we hold the net->key_lock here, allowing updates to both
* then net and sk
*/
Expand Down

0 comments on commit 7b14e15

Please sign in to comment.