Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Daniel Borkmann and committed by
David S. Miller
52db882f d36f82b2

+15 -50
-18
include/net/sctp/sctp.h
··· 560 560 /* Round an int up to the next multiple of 4. */ 561 561 #define WORD_ROUND(s) (((s)+3)&~3) 562 562 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 - 581 563 /* External references. */ 582 564 583 565 extern struct proto sctp_prot;
+3 -3
include/net/sctp/structs.h
··· 54 54 #ifndef __sctp_structs_h__ 55 55 #define __sctp_structs_h__ 56 56 57 - #include <linux/time.h> /* We get struct timespec. */ 57 + #include <linux/ktime.h> 58 58 #include <linux/socket.h> /* linux/in.h needs this!! */ 59 59 #include <linux/in.h> /* We get struct sockaddr_in. */ 60 60 #include <linux/in6.h> /* We get struct in6_addr */ ··· 284 284 __u32 peer_ttag; 285 285 286 286 /* When does this cookie expire? */ 287 - struct timeval expiration; 287 + ktime_t expiration; 288 288 289 289 /* Number of inbound/outbound streams which are set 290 290 * and negotiated during the INIT process. ··· 1537 1537 sctp_state_t state; 1538 1538 1539 1539 /* The cookie life I award for any cookie. */ 1540 - struct timeval cookie_life; 1540 + ktime_t cookie_life; 1541 1541 1542 1542 /* Overall : The overall association error count. 1543 1543 * Error Count : [Clear this any time I get something.]
+1 -7
net/sctp/associola.c
··· 102 102 sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port); 103 103 104 104 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); 112 106 asoc->frag_point = 0; 113 107 asoc->user_frag = sp->user_frag; 114 108
+8 -11
net/sctp/sm_make_chunk.c
··· 1630 1630 cookie->c.adaptation_ind = asoc->peer.adaptation_ind; 1631 1631 1632 1632 /* 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()); 1635 1635 1636 1636 /* Copy the peer's init packet. */ 1637 1637 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr, ··· 1680 1680 unsigned int len; 1681 1681 sctp_scope_t scope; 1682 1682 struct sk_buff *skb = chunk->skb; 1683 - struct timeval tv; 1683 + ktime_t kt; 1684 1684 struct hash_desc desc; 1685 1685 1686 1686 /* Header size is static data prior to the actual cookie, including ··· 1757 1757 * down the new association establishment instead of every packet. 1758 1758 */ 1759 1759 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP)) 1760 - skb_get_timestamp(skb, &tv); 1760 + kt = skb_get_ktime(skb); 1761 1761 else 1762 - do_gettimeofday(&tv); 1762 + kt = ktime_get(); 1763 1763 1764 - if (!asoc && tv_lt(bear_cookie->expiration, tv)) { 1764 + if (!asoc && ktime_compare(bear_cookie->expiration, kt) < 0) { 1765 1765 /* 1766 1766 * Section 3.3.10.3 Stale Cookie Error (3) 1767 1767 * ··· 1773 1773 len = ntohs(chunk->chunk_hdr->length); 1774 1774 *errp = sctp_make_op_error_space(asoc, chunk, len); 1775 1775 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)); 1779 1777 __be32 n = htonl(usecs); 1780 1778 1781 1779 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE, ··· 2512 2514 /* Suggested Cookie Life span increment's unit is msec, 2513 2515 * (1/1000sec). 2514 2516 */ 2515 - asoc->cookie_life.tv_sec += stale / 1000; 2516 - asoc->cookie_life.tv_usec += (stale % 1000) * 1000; 2517 + asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale); 2517 2518 break; 2518 2519 2519 2520 case SCTP_PARAM_HOST_NAME_ADDRESS:
+3 -11
net/sctp/socket.c
··· 2910 2910 asoc->max_retrans = assocparams.sasoc_asocmaxrxt; 2911 2911 } 2912 2912 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); 2920 2915 } else { 2921 2916 /* Set the values to the endpoint */ 2922 2917 struct sctp_sock *sp = sctp_sk(sk); ··· 5069 5074 assocparams.sasoc_asocmaxrxt = asoc->max_retrans; 5070 5075 assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; 5071 5076 assocparams.sasoc_local_rwnd = asoc->a_rwnd; 5072 - assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec 5073 - * 1000) + 5074 - (asoc->cookie_life.tv_usec 5075 - / 1000); 5077 + assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life); 5076 5078 5077 5079 list_for_each(pos, &asoc->peer.transport_addr_list) { 5078 5080 cnt ++;