From patchwork Mon Aug 27 02:42:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousuk Seung X-Patchwork-Id: 962323 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="u8uFI+RR"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41zGQM4wNZz9s3Z for ; Mon, 27 Aug 2018 12:42:51 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727077AbeH0G1S (ORCPT ); Mon, 27 Aug 2018 02:27:18 -0400 Received: from mail-it0-f73.google.com ([209.85.214.73]:56827 "EHLO mail-it0-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726968AbeH0G1R (ORCPT ); Mon, 27 Aug 2018 02:27:17 -0400 Received: by mail-it0-f73.google.com with SMTP id 11-v6so2093646itk.6 for ; Sun, 26 Aug 2018 19:42:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=sWstpziTmvYfU9vNEkBjMIwFXxPSEqSl1opx6iA6H5o=; b=u8uFI+RRYhNKfgJGvVyQme3RzHbDcJuFYE74YbiPJ3FyzNprGleqDf/HQfJnIdShJH qXqgSPD+LqpDnTSWm4+2cJUI26oBM1nQK/EQEA6SbpjkSUQG0xQcJiV7KmyfKA5UCPZs s6pq8uYIXMz9ueGUGHmOE62iwqfcjCaQEpxd37i2g4OGG51cDV2mOWA2B9PqErdqwpte AqjpniCzW9AcAefHJfC5Bh6neR3gng9P9tG/Oc2/9SG1GubYLKWAi8OxDY1yPdvVb3Iw 2HsjNh51DifCovBJa+6JXp131gywZaZr7Xmqi4MSty3VjrrCV8JJedze1W744QPy55Q0 ftZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=sWstpziTmvYfU9vNEkBjMIwFXxPSEqSl1opx6iA6H5o=; b=R/qAgJH2KU21EBtgXuOIAlv7kmMh9909JN09bXUZJ2j5NW53aj+5JIY0vINEwTiUSp l2PWCavMCuTFziaoDCub0ZXoYXi/F5va2B0DleXKJi3zAJI8BJDm400HhLXzYLsKusfB X8MtAJ0TPJnAKgqzFXj9UCjbK01U9e6F+T9h7HudNxOkF11/tfP03CuhYFN4hbE0IKT8 +86UQvjJVSaf+i3/Co/hDGEeX0oEZg1LCIjxH7K/uOirhuL7JntBhPMZIEuPNlJB8v1G P+3gGdh0hiRVI9Hw/PbmrV985jt9VqlbGLO8zBrzOlYYYZm7rxQEAs1QzHwamY+dEitx FPBw== X-Gm-Message-State: APzg51AFGZ4S59ELHGzoKGfyi/FM0JFJUXKl4pxhLCESGRgMwWxTSE83 TKbYSWadTReJQA6/TnhmV9JAWW7hGa2nqI7muawElf5zXxxudNzihYMSgt9du0+SZwakmuxG1aG AK4qjZ4RM2wLhBA6J5DSeu911Hm2TUKx0e33Y3p1e9Xseg2xWZn85wesbQ/jTinC9 X-Google-Smtp-Source: ANB0VdbZ0E3EUp4/IXo8PwXVWMUl9mlajt/IFAU6UU2fo5w9ar00C/t2h6Hbab1yxKmRzJA+SitfzFYoGslc X-Received: by 2002:a24:4d89:: with SMTP id l131-v6mr3286216itb.38.1535337761315; Sun, 26 Aug 2018 19:42:41 -0700 (PDT) Date: Sun, 26 Aug 2018 19:42:28 -0700 In-Reply-To: <20180827024230.246445-1-ysseung@google.com> Message-Id: <20180827024230.246445-2-ysseung@google.com> Mime-Version: 1.0 References: <20180827024230.246445-1-ysseung@google.com> X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-goog Subject: [PATCH v2 iproute2-next 1/3] tc: support conversions to or from 64 bit nanosecond-based time From: Yousuk Seung To: netdev@vger.kernel.org Cc: Stephen Hemminger , David Ahern , Michael McLennan , Priyaranjan Jha , Dave Taht , Yousuk Seung , Neal Cardwell Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Dave Taht Using a 32 bit field to represent time in nanoseconds results in a maximum value of about 4.3 seconds, which is well below many observed delays in WiFi and LTE, and barely in the ballpark for a trip past the Earth's moon, Luna. Using 64 bit time fields in nanoseconds allows us to simulate network diameters of several hundred light-years. However, only conversions to and from ns, us, ms, and seconds are provided. The iproute2 64 bit api uses signed values for time. Being able to represent positive or negative time allows us to calculate +/- deltas between, for example, the CLOCK_TAI and CLOCK_REALTIME clocks. Time related utility functions in tc_util.c are moved to lib/utils.c. Signed-off-by: Yousuk Seung Signed-off-by: Dave Taht Signed-off-by: Neal Cardwell --- include/utils.h | 12 ++++++ lib/utils.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++ tc/tc_cbq.c | 1 + tc/tc_core.c | 1 + tc/tc_core.h | 2 - tc/tc_estimator.c | 1 + tc/tc_util.c | 46 -------------------- tc/tc_util.h | 3 -- 8 files changed, 119 insertions(+), 51 deletions(-) diff --git a/include/utils.h b/include/utils.h index 8cb4349e8a89..eba67b6ecf44 100644 --- a/include/utils.h +++ b/include/utils.h @@ -46,6 +46,11 @@ void incomplete_command(void) __attribute__((noreturn)); #define NEXT_ARG_FWD() do { argv++; argc--; } while(0) #define PREV_ARG() do { argv--; argc++; } while(0) +#define TIME_UNITS_PER_SEC 1000000 +#define NSEC_PER_USEC 1000 +#define NSEC_PER_MSEC 1000000 +#define NSEC_PER_SEC 1000000000LL + typedef struct { __u16 flags; @@ -310,4 +315,11 @@ size_t strlcat(char *dst, const char *src, size_t size); void drop_cap(void); +int get_time(unsigned int *time, const char *str); +int get_time64(__s64 *time, const char *str); +void print_time(char *buf, int len, __u32 time); +void print_time64(char *buf, int len, __s64 time); +char *sprint_time(__u32 time, char *buf); +char *sprint_time64(__s64 time, char *buf); + #endif /* __UTILS_H__ */ diff --git a/lib/utils.c b/lib/utils.c index 02ce67721915..34ec4ab12646 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -1633,3 +1633,107 @@ void drop_cap(void) } #endif } + +int get_time(unsigned int *time, const char *str) +{ + double t; + char *p; + + t = strtod(str, &p); + if (p == str) + return -1; + + if (*p) { + if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 || + strcasecmp(p, "secs") == 0) + t *= TIME_UNITS_PER_SEC; + else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 || + strcasecmp(p, "msecs") == 0) + t *= TIME_UNITS_PER_SEC/1000; + else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 || + strcasecmp(p, "usecs") == 0) + t *= TIME_UNITS_PER_SEC/1000000; + else + return -1; + } + + *time = t; + return 0; +} + + +void print_time(char *buf, int len, __u32 time) +{ + double tmp = time; + + if (tmp >= TIME_UNITS_PER_SEC) + snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); + else if (tmp >= TIME_UNITS_PER_SEC/1000) + snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); + else + snprintf(buf, len, "%uus", time); +} + +char *sprint_time(__u32 time, char *buf) +{ + print_time(buf, SPRINT_BSIZE-1, time); + return buf; +} + +/* 64 bit times are represented internally in nanoseconds */ +int get_time64(__s64 *time, const char *str) +{ + double nsec; + char *p; + + nsec = strtod(str, &p); + if (p == str) + return -1; + + if (*p) { + if (strcasecmp(p, "s") == 0 || + strcasecmp(p, "sec") == 0 || + strcasecmp(p, "secs") == 0) + nsec *= NSEC_PER_SEC; + else if (strcasecmp(p, "ms") == 0 || + strcasecmp(p, "msec") == 0 || + strcasecmp(p, "msecs") == 0) + nsec *= NSEC_PER_MSEC; + else if (strcasecmp(p, "us") == 0 || + strcasecmp(p, "usec") == 0 || + strcasecmp(p, "usecs") == 0) + nsec *= NSEC_PER_USEC; + else if (strcasecmp(p, "ns") == 0 || + strcasecmp(p, "nsec") == 0 || + strcasecmp(p, "nsecs") == 0) + nsec *= 1; + else + return -1; + } + + *time = nsec; + return 0; +} + +void print_time64(char *buf, int len, __s64 time) +{ + double nsec = time; + + if (time >= NSEC_PER_SEC) + snprintf(buf, len, "%.3fs", nsec/NSEC_PER_SEC); + else if (time >= NSEC_PER_MSEC) + snprintf(buf, len, "%.3fms", nsec/NSEC_PER_MSEC); + else if (time >= NSEC_PER_USEC) + snprintf(buf, len, "%.3fus", nsec/NSEC_PER_USEC); + else + snprintf(buf, len, "%lldns", time); +} + +char *sprint_time64(__s64 time, char *buf) +{ + print_time64(buf, SPRINT_BSIZE-1, time); + return buf; +} + + + diff --git a/tc/tc_cbq.c b/tc/tc_cbq.c index 4cd584a91a26..c811456b1627 100644 --- a/tc/tc_cbq.c +++ b/tc/tc_cbq.c @@ -20,6 +20,7 @@ #include #include +#include "utils.h" #include "tc_core.h" #include "tc_cbq.h" diff --git a/tc/tc_core.c b/tc/tc_core.c index 1bde4d51e5dc..8eb11223eb9d 100644 --- a/tc/tc_core.c +++ b/tc/tc_core.c @@ -21,6 +21,7 @@ #include #include +#include "utils.h" #include "tc_core.h" #include diff --git a/tc/tc_core.h b/tc/tc_core.h index 1dfa9a4f773b..bd4a99f0d8dd 100644 --- a/tc/tc_core.h +++ b/tc/tc_core.h @@ -5,8 +5,6 @@ #include #include -#define TIME_UNITS_PER_SEC 1000000 - enum link_layer { LINKLAYER_UNSPEC, LINKLAYER_ETHERNET, diff --git a/tc/tc_estimator.c b/tc/tc_estimator.c index e4edfc7e98d9..f494b7caa44e 100644 --- a/tc/tc_estimator.c +++ b/tc/tc_estimator.c @@ -20,6 +20,7 @@ #include #include +#include "utils.h" #include "tc_core.h" int tc_setup_estimator(unsigned int A, unsigned int time_const, struct tc_estimator *est) diff --git a/tc/tc_util.c b/tc/tc_util.c index d7578528a31b..cafbe49f3ec8 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -334,52 +334,6 @@ char *sprint_rate(__u64 rate, char *buf) return buf; } -int get_time(unsigned int *time, const char *str) -{ - double t; - char *p; - - t = strtod(str, &p); - if (p == str) - return -1; - - if (*p) { - if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 || - strcasecmp(p, "secs") == 0) - t *= TIME_UNITS_PER_SEC; - else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 || - strcasecmp(p, "msecs") == 0) - t *= TIME_UNITS_PER_SEC/1000; - else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 || - strcasecmp(p, "usecs") == 0) - t *= TIME_UNITS_PER_SEC/1000000; - else - return -1; - } - - *time = t; - return 0; -} - - -void print_time(char *buf, int len, __u32 time) -{ - double tmp = time; - - if (tmp >= TIME_UNITS_PER_SEC) - snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); - else if (tmp >= TIME_UNITS_PER_SEC/1000) - snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); - else - snprintf(buf, len, "%uus", time); -} - -char *sprint_time(__u32 time, char *buf) -{ - print_time(buf, SPRINT_BSIZE-1, time); - return buf; -} - char *sprint_ticks(__u32 ticks, char *buf) { return sprint_time(tc_core_tick2time(ticks), buf); diff --git a/tc/tc_util.h b/tc/tc_util.h index 6632c4f9c528..76fd986d6e4c 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -81,13 +81,11 @@ int get_rate64(__u64 *rate, const char *str); int get_percent_rate64(__u64 *rate, const char *str, const char *dev); int get_size(unsigned int *size, const char *str); int get_size_and_cell(unsigned int *size, int *cell_log, char *str); -int get_time(unsigned int *time, const char *str); int get_linklayer(unsigned int *val, const char *arg); void print_rate(char *buf, int len, __u64 rate); void print_size(char *buf, int len, __u32 size); void print_qdisc_handle(char *buf, int len, __u32 h); -void print_time(char *buf, int len, __u32 time); void print_linklayer(char *buf, int len, unsigned int linklayer); void print_devname(enum output_type type, int ifindex); @@ -95,7 +93,6 @@ char *sprint_rate(__u64 rate, char *buf); char *sprint_size(__u32 size, char *buf); char *sprint_qdisc_handle(__u32 h, char *buf); char *sprint_tc_classid(__u32 h, char *buf); -char *sprint_time(__u32 time, char *buf); char *sprint_ticks(__u32 ticks, char *buf); char *sprint_linklayer(unsigned int linklayer, char *buf);