From patchwork Tue Nov 7 21:16:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Taht X-Patchwork-Id: 835471 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@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; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lmsqKYlz"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yWj1H2b03z9s7F for ; Wed, 8 Nov 2017 08:17:07 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933988AbdKGVRF (ORCPT ); Tue, 7 Nov 2017 16:17:05 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:43009 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933968AbdKGVRD (ORCPT ); Tue, 7 Nov 2017 16:17:03 -0500 Received: by mail-pf0-f196.google.com with SMTP id a8so431234pfc.0 for ; Tue, 07 Nov 2017 13:17:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/kTLKFfUrDfnu9Fn23O6pCikljjukBYMRptrXjVHRyI=; b=lmsqKYlzN59JCB44ib7j2S1X4CjY6TtCqb0BPaH34Z4X+a71UkKhC71kN6o5g2q/Jw RhDk61TwbVytt+DwUbC3osBT9/qukHn8jO14NEKT7srDDzyTBEZMBoSSwUuGO8LK0O+Z T19ApjRzbwiLr/XLc0PhwZY+3bZWLW6ic2Qa15F6gQmAwF7WZyQefP6szTETGqwqMCs5 tO9z0YdM75jT19MYTZ4iN7e7nhzMKMmFK+Ng5EeI+J5RC+zqolzHX2qiVH8Ho10EOWhn OwK9wlet3dTqi9C+0XUyLS52kALFsbkR42Xtx6ifoFWicLMseOdur6wrOCkrDTjj3ifL gcXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/kTLKFfUrDfnu9Fn23O6pCikljjukBYMRptrXjVHRyI=; b=lRZWNwbbKLshWOcUzp61LpWerH4cGk7k5RKgH+mn8lzwE7KVcaywuD2PNezL19P0Ox O6ap6YZPLyiBE1hpKrXJi1EM7FI38A6NDL+5U0w3SZcrC8aE49tCPOvOAzjEvEHddQg7 QaMFH/JuvfY9AB4EP9QKsaErln1B0Hzp30664YLWGDRHRpc5T2udhEbaoObA5dCyNRip cc2JaHrDQjVByNkqmLeSMSrT/2DtasMS8TgT72mPC6g6j8t8nepbWiNVDz1llEt2Cr6A x/55MARp1D/EDJxv2hfwACIGwqhvhZFm25lxS2IRzuACiKfpb/T4zK5oI/2Lx7/ssSen ZlGA== X-Gm-Message-State: AJaThX7HXE76ZbtOOvivPmi3BdiVguVXYj0TAjmnRcAiil/UuZWDS3sH ekInMN4WOlDPXtUpJwNNnsXqWg== X-Google-Smtp-Source: ABhQp+Sy9GWHq2IrFJn52YGt9tNFJU8br72V8/3PkOn3fK4NUyW6wt0eMh9DYj7Y7iJQGtrSaExDzQ== X-Received: by 10.101.81.197 with SMTP id i5mr98838pgq.260.1510089422748; Tue, 07 Nov 2017 13:17:02 -0800 (PST) Received: from nemesis.lab.teklibre.com ([2603:3024:1536:86f0:2e0:4cff:fec1:1206]) by smtp.gmail.com with ESMTPSA id h29sm4993197pfd.65.2017.11.07.13.17.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 07 Nov 2017 13:17:02 -0800 (PST) From: Dave Taht To: netdev@vger.kernel.org Cc: Dave Taht Subject: [PATCH iproute2 1/4] tc: support conversions to or from 64 bit nanosecond-based time Date: Tue, 7 Nov 2017 13:16:53 -0800 Message-Id: <1510089416-5945-2-git-send-email-dave.taht@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510089416-5945-1-git-send-email-dave.taht@gmail.com> References: <1510089416-5945-1-git-send-email-dave.taht@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. Signed-off-by: Dave Taht --- tc/tc_core.h | 3 +++ tc/tc_util.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tc/tc_util.h | 3 +++ 3 files changed, 62 insertions(+) diff --git a/tc/tc_core.h b/tc/tc_core.h index 8a63b79..e8bda8b 100644 --- a/tc/tc_core.h +++ b/tc/tc_core.h @@ -5,6 +5,9 @@ #include #define TIME_UNITS_PER_SEC 1000000 +#define USEC_PER_SEC 1000 +#define MSEC_PER_SEC (1000 * 1000) +#define NSEC_PER_SEC (1000 * 1000 * 1000) enum link_layer { LINKLAYER_UNSPEC, diff --git a/tc/tc_util.c b/tc/tc_util.c index b39e550..dafbbc3 100644 --- a/tc/tc_util.c +++ b/tc/tc_util.c @@ -323,6 +323,62 @@ char *sprint_ticks(__u32 ticks, char *buf) return sprint_time(tc_core_tick2time(ticks), buf); } +/* 64 bit times are represented internally in nanoseconds */ + +int get_time64(__s64 *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 *= NSEC_PER_SEC; + else if (strcasecmp(p, "ms") == 0 || + strcasecmp(p, "msec") == 0 || + strcasecmp(p, "msecs") == 0) + t *= MSEC_PER_SEC; + else if (strcasecmp(p, "us") == 0 || + strcasecmp(p, "usec") == 0 || + strcasecmp(p, "usecs") == 0) + t *= USEC_PER_SEC; + else if (strcasecmp(p, "ns") == 0 || + strcasecmp(p, "nsec") == 0 || + strcasecmp(p, "nsecs") == 0) + t *= 1; + else + return -1; + } + + *time = t; + return 0; +} + +void print_time64(char *buf, int len, __s64 time) +{ + double tmp = time; + + if (time >= NSEC_PER_SEC) + snprintf(buf, len, "%.3fs", tmp/NSEC_PER_SEC); + else if (time >= MSEC_PER_SEC) + snprintf(buf, len, "%.3fms", tmp/MSEC_PER_SEC); + else if (time >= USEC_PER_SEC) + snprintf(buf, len, "%.3fus", tmp/USEC_PER_SEC); + else + snprintf(buf, len, "%lldns", time); +} + +char *sprint_time64(__s64 time, char *buf) +{ + print_time64(buf, SPRINT_BSIZE-1, time); + return buf; +} + int get_size(unsigned int *size, const char *str) { double sz; diff --git a/tc/tc_util.h b/tc/tc_util.h index 583a21a..7a8559b 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -72,12 +72,14 @@ int get_rate64(__u64 *rate, const char *str); 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_time64(__s64 *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_time64(char *buf, int len, __s64 time); void print_linklayer(char *buf, int len, unsigned int linklayer); char *sprint_rate(__u64 rate, char *buf); @@ -85,6 +87,7 @@ 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_time64(__s64 time, char *buf); char *sprint_ticks(__u32 ticks, char *buf); char *sprint_linklayer(unsigned int linklayer, char *buf);