Skip to content

Commit 52db882

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: sctp: migrate cookie life from timeval to ktime
Currently, SCTP code defines its own timeval functions (since timeval is rarely used inside the kernel by others), namely tv_lt() and TIMEVAL_ADD() macros, that operate on SCTP cookie expiration. We might as well remove all those, and operate directly on ktime structures for a couple of reasons: ktime is available on all archs; complexity of ktime calculations depending on the arch is less than (reduces to a simple arithmetic operations on archs with BITS_PER_LONG == 64 or CONFIG_KTIME_SCALAR) or equal to timeval functions (other archs); code becomes more readable; macros can be thrown out. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Acked-by: Vlad Yasevich <vyasevich@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d36f82b commit 52db882

5 files changed

Lines changed: 15 additions & 50 deletions

File tree

‎include/net/sctp/sctp.h‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -560,24 +560,6 @@ for (pos = chunk->subh.fwdtsn_hdr->skip;\
560560
/* Round an int up to the next multiple of 4. */
561561
#define WORD_ROUND(s) (((s)+3)&~3)
562562

563-
/* Compare two timevals. */
564-
#define tv_lt(s, t) \
565-
(s.tv_sec < t.tv_sec || (s.tv_sec == t.tv_sec && s.tv_usec < t.tv_usec))
566-
567-
/* Add tv1 to tv2. */
568-
#define TIMEVAL_ADD(tv1, tv2) \
569-
({ \
570-
suseconds_t usecs = (tv2).tv_usec + (tv1).tv_usec; \
571-
time_t secs = (tv2).tv_sec + (tv1).tv_sec; \
572-
\
573-
if (usecs >= 1000000) { \
574-
usecs -= 1000000; \
575-
secs++; \
576-
} \
577-
(tv2).tv_sec = secs; \
578-
(tv2).tv_usec = usecs; \
579-
})
580-
581563
/* External references. */
582564

583565
extern struct proto sctp_prot;

‎include/net/sctp/structs.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#ifndef __sctp_structs_h__
5555
#define __sctp_structs_h__
5656

57-
#include <linux/time.h> /* We get struct timespec. */
57+
#include <linux/ktime.h>
5858
#include <linux/socket.h> /* linux/in.h needs this!! */
5959
#include <linux/in.h> /* We get struct sockaddr_in. */
6060
#include <linux/in6.h> /* We get struct in6_addr */
@@ -284,7 +284,7 @@ struct sctp_cookie {
284284
__u32 peer_ttag;
285285

286286
/* When does this cookie expire? */
287-
struct timeval expiration;
287+
ktime_t expiration;
288288

289289
/* Number of inbound/outbound streams which are set
290290
* and negotiated during the INIT process.
@@ -1537,7 +1537,7 @@ struct sctp_association {
15371537
sctp_state_t state;
15381538

15391539
/* The cookie life I award for any cookie. */
1540-
struct timeval cookie_life;
1540+
ktime_t cookie_life;
15411541

15421542
/* Overall : The overall association error count.
15431543
* Error Count : [Clear this any time I get something.]

‎net/sctp/associola.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,7 @@ static struct sctp_association *sctp_association_init(struct sctp_association *a
102102
sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port);
103103

104104
asoc->state = SCTP_STATE_CLOSED;
105-
106-
/* Set these values from the socket values, a conversion between
107-
* millsecons to seconds/microseconds must also be done.
108-
*/
109-
asoc->cookie_life.tv_sec = sp->assocparams.sasoc_cookie_life / 1000;
110-
asoc->cookie_life.tv_usec = (sp->assocparams.sasoc_cookie_life % 1000)
111-
* 1000;
105+
asoc->cookie_life = ms_to_ktime(sp->assocparams.sasoc_cookie_life);
112106
asoc->frag_point = 0;
113107
asoc->user_frag = sp->user_frag;
114108

‎net/sctp/sm_make_chunk.c‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,8 @@ static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
16301630
cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
16311631

16321632
/* Set an expiration time for the cookie. */
1633-
do_gettimeofday(&cookie->c.expiration);
1634-
TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1633+
cookie->c.expiration = ktime_add(asoc->cookie_life,
1634+
ktime_get());
16351635

16361636
/* Copy the peer's init packet. */
16371637
memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
@@ -1680,7 +1680,7 @@ struct sctp_association *sctp_unpack_cookie(
16801680
unsigned int len;
16811681
sctp_scope_t scope;
16821682
struct sk_buff *skb = chunk->skb;
1683-
struct timeval tv;
1683+
ktime_t kt;
16841684
struct hash_desc desc;
16851685

16861686
/* Header size is static data prior to the actual cookie, including
@@ -1757,11 +1757,11 @@ struct sctp_association *sctp_unpack_cookie(
17571757
* down the new association establishment instead of every packet.
17581758
*/
17591759
if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1760-
skb_get_timestamp(skb, &tv);
1760+
kt = skb_get_ktime(skb);
17611761
else
1762-
do_gettimeofday(&tv);
1762+
kt = ktime_get();
17631763

1764-
if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1764+
if (!asoc && ktime_compare(bear_cookie->expiration, kt) < 0) {
17651765
/*
17661766
* Section 3.3.10.3 Stale Cookie Error (3)
17671767
*
@@ -1773,9 +1773,7 @@ struct sctp_association *sctp_unpack_cookie(
17731773
len = ntohs(chunk->chunk_hdr->length);
17741774
*errp = sctp_make_op_error_space(asoc, chunk, len);
17751775
if (*errp) {
1776-
suseconds_t usecs = (tv.tv_sec -
1777-
bear_cookie->expiration.tv_sec) * 1000000L +
1778-
tv.tv_usec - bear_cookie->expiration.tv_usec;
1776+
suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
17791777
__be32 n = htonl(usecs);
17801778

17811779
sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
@@ -2514,8 +2512,7 @@ static int sctp_process_param(struct sctp_association *asoc,
25142512
/* Suggested Cookie Life span increment's unit is msec,
25152513
* (1/1000sec).
25162514
*/
2517-
asoc->cookie_life.tv_sec += stale / 1000;
2518-
asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2515+
asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
25192516
break;
25202517

25212518
case SCTP_PARAM_HOST_NAME_ADDRESS:

‎net/sctp/socket.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,13 +2910,8 @@ static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsig
29102910
asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
29112911
}
29122912

2913-
if (assocparams.sasoc_cookie_life != 0) {
2914-
asoc->cookie_life.tv_sec =
2915-
assocparams.sasoc_cookie_life / 1000;
2916-
asoc->cookie_life.tv_usec =
2917-
(assocparams.sasoc_cookie_life % 1000)
2918-
* 1000;
2919-
}
2913+
if (assocparams.sasoc_cookie_life != 0)
2914+
asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
29202915
} else {
29212916
/* Set the values to the endpoint */
29222917
struct sctp_sock *sp = sctp_sk(sk);
@@ -5074,10 +5069,7 @@ static int sctp_getsockopt_associnfo(struct sock *sk, int len,
50745069
assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
50755070
assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
50765071
assocparams.sasoc_local_rwnd = asoc->a_rwnd;
5077-
assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
5078-
* 1000) +
5079-
(asoc->cookie_life.tv_usec
5080-
/ 1000);
5072+
assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
50815073

50825074
list_for_each(pos, &asoc->peer.transport_addr_list) {
50835075
cnt ++;

0 commit comments

Comments
 (0)