Commit ef056b21 authored by Stephen Hemminger's avatar Stephen Hemminger

Merge branch 'master' into net-next-for-3.13

parents a4c51eb3 4de8d885
......@@ -18,8 +18,7 @@
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# To get the latest version, check on Freshmeat for actual location:
#
......
......@@ -22,13 +22,22 @@ struct rtnl_handle
extern int rcvbuf;
extern int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions);
extern int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions, int protocol);
extern int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
__attribute__((warn_unused_result));
extern int rtnl_open_byproto(struct rtnl_handle *rth, unsigned subscriptions,
int protocol)
__attribute__((warn_unused_result));
extern void rtnl_close(struct rtnl_handle *rth);
extern int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type);
extern int rtnl_wilddump_request(struct rtnl_handle *rth, int fam, int type)
__attribute__((warn_unused_result));
extern int rtnl_wilddump_req_filter(struct rtnl_handle *rth, int fam, int type,
__u32 filt_mask);
extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req, int len);
__u32 filt_mask)
__attribute__((warn_unused_result));
extern int rtnl_dump_request(struct rtnl_handle *rth, int type, void *req,
int len)
__attribute__((warn_unused_result));
typedef int (*rtnl_filter_t)(const struct sockaddr_nl *,
struct nlmsghdr *n, void *);
......@@ -44,9 +53,12 @@ extern int rtnl_dump_filter_l(struct rtnl_handle *rth,
extern int rtnl_dump_filter(struct rtnl_handle *rth, rtnl_filter_t filter,
void *arg);
extern int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
unsigned groups, struct nlmsghdr *answer);
extern int rtnl_send(struct rtnl_handle *rth, const void *buf, int);
extern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int);
unsigned groups, struct nlmsghdr *answer)
__attribute__((warn_unused_result));
extern int rtnl_send(struct rtnl_handle *rth, const void *buf, int)
__attribute__((warn_unused_result));
extern int rtnl_send_check(struct rtnl_handle *rth, const void *buf, int)
__attribute__((warn_unused_result));
extern int addattr(struct nlmsghdr *n, int maxlen, int type);
extern int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data);
......
......@@ -156,5 +156,5 @@ extern int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6);
struct iplink_req;
int iplink_parse(int argc, char **argv, struct iplink_req *req,
char **name, char **type, char **link, char **dev,
int *group);
int *group, int *index);
#endif /* __UTILS_H__ */
......@@ -5,7 +5,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
iplink_vlan.o link_veth.o link_gre.o iplink_can.o \
iplink_macvlan.o iplink_macvtap.o ipl2tp.o link_vti.o \
iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \
link_iptnl.o link_gre6.o iplink_bond.o
link_iptnl.o link_gre6.o iplink_bond.o iplink_hsr.o
RTMONOBJ=rtmon.o
......
......@@ -12,8 +12,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* Author:
......
......@@ -14,8 +14,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*
*
* Based on iprule.c.
......
......@@ -47,7 +47,7 @@ void iplink_usage(void)
fprintf(stderr, " [ txqueuelen PACKETS ]\n");
fprintf(stderr, " [ address LLADDR ]\n");
fprintf(stderr, " [ broadcast LLADDR ]\n");
fprintf(stderr, " [ mtu MTU ]\n");
fprintf(stderr, " [ mtu MTU ] [index IDX ]\n");
fprintf(stderr, " [ numtxqueues QUEUE_COUNT ]\n");
fprintf(stderr, " [ numrxqueues QUEUE_COUNT ]\n");
fprintf(stderr, " type TYPE [ ARGS ]\n");
......@@ -178,7 +178,10 @@ static int iplink_have_newlink(void)
req.n.nlmsg_type = RTM_NEWLINK;
req.i.ifi_family = AF_UNSPEC;
rtnl_send(&rth, &req.n, req.n.nlmsg_len);
if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
perror("request send failed");
exit(1);
}
rtnl_listen(&rth, accept_msg, NULL);
}
return have_rtnl_newlink;
......@@ -288,7 +291,7 @@ static int iplink_parse_vf(int vf, int *argcp, char ***argvp,
}
int iplink_parse(int argc, char **argv, struct iplink_req *req,
char **name, char **type, char **link, char **dev, int *group)
char **name, char **type, char **link, char **dev, int *group, int *index)
{
int ret, len;
char abuf[32];
......@@ -312,6 +315,9 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
} else if (strcmp(*argv, "name") == 0) {
NEXT_ARG();
*name = *argv;
} else if (strcmp(*argv, "index") == 0) {
NEXT_ARG();
*index = atoi(*argv);
} else if (matches(*argv, "link") == 0) {
NEXT_ARG();
*link = *argv;
......@@ -503,6 +509,7 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
char *name = NULL;
char *link = NULL;
char *type = NULL;
int index = 0;
int group;
struct link_util *lu = NULL;
struct iplink_req req;
......@@ -515,7 +522,7 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
req.n.nlmsg_type = cmd;
req.i.ifi_family = preferred_family;
ret = iplink_parse(argc, argv, &req, &name, &type, &link, &dev, &group);
ret = iplink_parse(argc, argv, &req, &name, &type, &link, &dev, &group, &index);
if (ret < 0)
return ret;
......@@ -575,6 +582,8 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
}
addattr_l(&req.n, sizeof(req), IFLA_LINK, &ifindex, 4);
}
req.i.ifi_index = index;
}
if (name) {
......
/*
* iplink_hsr.c HSR device support
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Arvid Brodin <arvid.brodin@alten.se>
*
* Based on iplink_vlan.c by Patrick McHardy <kaber@trash.net>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> /* Needed by linux/if.h for some reason */
#include <linux/if.h>
#include <linux/if_arp.h>
#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"
static void usage(void)
{
fprintf(stderr,
"Usage:\tip link add name NAME type hsr slave1 SLAVE1-IF slave2 SLAVE2-IF\n"
"\t[ supervision ADDR-BYTE ]\n"
"\n"
"NAME\n"
" name of new hsr device (e.g. hsr0)\n"
"SLAVE1-IF, SLAVE2-IF\n"
" the two slave devices bound to the HSR device\n"
"ADDR-BYTE\n"
" 0-255; the last byte of the multicast address used for HSR supervision\n"
" frames (default = 0)\n");
}
static int hsr_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *n)
{
int ifindex;
unsigned char multicast_spec;
while (argc > 0) {
if (matches(*argv, "supervision") == 0) {
NEXT_ARG();
if (get_u8(&multicast_spec, *argv, 0))
invarg("ADDR-BYTE is invalid", *argv);
addattr_l(n, 1024, IFLA_HSR_MULTICAST_SPEC,
&multicast_spec, 1);
} else if (matches(*argv, "slave1") == 0) {
NEXT_ARG();
ifindex = ll_name_to_index(*argv);
if (ifindex == 0)
invarg("No such interface", *argv);
addattr_l(n, 1024, IFLA_HSR_SLAVE1, &ifindex, 4);
} else if (matches(*argv, "slave2") == 0) {
NEXT_ARG();
ifindex = ll_name_to_index(*argv);
if (ifindex == 0)
invarg("No such interface", *argv);
addattr_l(n, 1024, IFLA_HSR_SLAVE2, &ifindex, 4);
} else if (matches(*argv, "help") == 0) {
usage();
return -1;
} else {
fprintf(stderr, "hsr: what is \"%s\"?\n", *argv);
usage();
return -1;
}
argc--, argv++;
}
return 0;
}
static void hsr_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
SPRINT_BUF(b1);
if (!tb)
return;
if (tb[IFLA_HSR_SLAVE1] &&
RTA_PAYLOAD(tb[IFLA_HSR_SLAVE1]) < sizeof(__u32))
return;
if (tb[IFLA_HSR_SLAVE2] &&
RTA_PAYLOAD(tb[IFLA_HSR_SLAVE2]) < sizeof(__u32))
return;
if (tb[IFLA_HSR_SEQ_NR] &&
RTA_PAYLOAD(tb[IFLA_HSR_SEQ_NR]) < sizeof(__u16))
return;
if (tb[IFLA_HSR_SUPERVISION_ADDR] &&
RTA_PAYLOAD(tb[IFLA_HSR_SUPERVISION_ADDR]) < ETH_ALEN)
return;
fprintf(f, "slave1 ");
if (tb[IFLA_HSR_SLAVE1])
fprintf(f, "%s ",
ll_index_to_name(rta_getattr_u32(tb[IFLA_HSR_SLAVE1])));
else
fprintf(f, "<none> ");
fprintf(f, "slave2 ");
if (tb[IFLA_HSR_SLAVE2])
fprintf(f, "%s ",
ll_index_to_name(rta_getattr_u32(tb[IFLA_HSR_SLAVE2])));
else
fprintf(f, "<none> ");
if (tb[IFLA_HSR_SEQ_NR])
fprintf(f, "sequence %d ",
rta_getattr_u16(tb[IFLA_HSR_SEQ_NR]));
if (tb[IFLA_HSR_SUPERVISION_ADDR])
fprintf(f, "supervision %s ",
ll_addr_n2a(RTA_DATA(tb[IFLA_HSR_SUPERVISION_ADDR]),
RTA_PAYLOAD(tb[IFLA_HSR_SUPERVISION_ADDR]),
ARPHRD_VOID,
b1, sizeof(b1)));
}
struct link_util hsr_link_util = {
.id = "hsr",
.maxattr = IFLA_VLAN_MAX,
.parse_opt = hsr_parse_opt,
.print_opt = hsr_print_opt,
};
......@@ -165,7 +165,10 @@ static int do_show(int argc, char **argv)
addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX,
&filter.ifindex, sizeof(filter.ifindex));
rtnl_send(&rth, &req.n, req.n.nlmsg_len);
if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
perror("Can not send request");
exit(1);
}
rtnl_listen(&rth, print_netconf, stdout);
} else {
dump:
......
......@@ -12,8 +12,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on ipneigh.c
......
......@@ -12,8 +12,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on ip.c, iproute.c
......
......@@ -240,8 +240,9 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
}
}
if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
if (!(p->i_flags & VTI_ISVTI) &&
(p->iph.protocol != IPPROTO_GRE)) {
fprintf(stderr, "Keys are not allowed with ipip and sit tunnels\n");
return -1;
}
......
......@@ -14,8 +14,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on ip.c, iproute.c
......
......@@ -26,7 +26,11 @@ static void usage(void)
static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *hdr)
{
char *name, *type, *link, *dev;
char *dev = NULL;
char *name = NULL;
char *link = NULL;
char *type = NULL;
int index = 0;
int err, len;
struct rtattr * data;
int group;
......@@ -42,7 +46,7 @@ static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
hdr->nlmsg_len += sizeof(struct ifinfomsg);
err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)hdr,
&name, &type, &link, &dev, &group);
&name, &type, &link, &dev, &group, &index);
if (err < 0)
return err;
......@@ -53,6 +57,14 @@ static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
addattr_l(hdr, 1024, IFLA_IFNAME, name, len);
}
if (index) {
struct ifinfomsg *ifi = (struct ifinfomsg *)(data + 1);
ifi->ifi_index = index;
}
if (group != -1)
addattr32(hdr, 1024, IFLA_GROUP, group);
data->rta_len = (void *)NLMSG_TAIL(hdr) - (void *)data;
return argc - 1 - err;
}
......
......@@ -12,8 +12,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* split from ip_tunnel.c
......
......@@ -12,8 +12,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* Author:
......
......@@ -14,8 +14,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* Authors:
......
......@@ -14,8 +14,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on ipmonitor.c
......
......@@ -14,8 +14,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on iproute.c
......
......@@ -14,8 +14,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on iproute.c
......
......@@ -39,6 +39,8 @@ ip-link \- network device configuration
.br
.RB "[ " mtu
.IR MTU " ]"
.RB "[ " index
.IR IDX " ]"
.br
.RB "[ " numtxqueues
.IR QUEUE_COUNT " ]"
......@@ -54,6 +56,7 @@ ip-link \- network device configuration
.BR bond " ]"
.BR can " | "
.BR dummy " | "
.BR hsr " | "
.BR ifb " | "
.BR ipoib " |"
.BR macvlan " | "
......@@ -216,6 +219,10 @@ specifies the number of transmit queues for new device.
.BI numrxqueues " QUEUE_COUNT "
specifies the number of receive queues for new device.
.TP
.BI index " IDX "
specifies the desired index of the new virtual device. The link creation fails, if the index is busy.
.TP
VXLAN Type Support
For a link of type
......
......@@ -428,7 +428,11 @@ static int do_one_request(struct nlmsghdr *n)
static void load_initial_table(void)
{
rtnl_wilddump_request(&rth, AF_INET, RTM_GETNEIGH);
if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETNEIGH) < 0) {
perror("dump request failed");
exit(1);
}
}
static void get_kern_msg(void)
......
......@@ -996,7 +996,9 @@ static int xll_initted = 0;
static void xll_init(void)
{
struct rtnl_handle rth;
rtnl_open(&rth, 0);
if (rtnl_open(&rth, 0) < 0)
exit(1);
ll_init_map(&rth);
rtnl_close(&rth);
xll_initted = 1;
......
......@@ -32,7 +32,6 @@ static struct action_util * action_list;
#ifdef CONFIG_GACT
int gact_ld = 0 ; //fuckin backward compatibility
#endif
int batch_c = 0;
int tab_flush = 0;
static void act_usage(void)
......@@ -144,6 +143,7 @@ new_cmd(char **argv)
if ((matches(*argv, "change") == 0) ||
(matches(*argv, "replace") == 0)||
(matches(*argv, "delete") == 0)||
(matches(*argv, "get") == 0)||
(matches(*argv, "add") == 0))
return 1;
......@@ -303,7 +303,7 @@ tc_print_action(FILE * f, const struct rtattr *arg)
for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
if (tb[i]) {
fprintf(f, "\n\taction order %d: ", i + batch_c);
fprintf(f, "\n\taction order %d: ", i);
if (0 > tc_print_one_action(f, tb[i])) {
fprintf(f, "Error printing action\n");
}
......@@ -311,7 +311,6 @@ tc_print_action(FILE * f, const struct rtattr *arg)
}
batch_c+=TCA_ACT_MAX_PRIO ;
return 0;
}
......
......@@ -38,7 +38,6 @@ static void usage(void)
fprintf(stderr, " [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n");
fprintf(stderr, " [ linklayer TYPE ] [ ACTIONTERM ]\n");
fprintf(stderr, "Old Syntax ACTIONTERM := action <EXCEEDACT>[/NOTEXCEEDACT] \n");
fprintf(stderr, "New Syntax ACTIONTERM := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT] \n");
fprintf(stderr, "Where: *EXCEEDACT := pipe | ok | reclassify | drop | continue \n");
fprintf(stderr, "Where: pipe is only valid for new syntax \n");
......@@ -150,6 +149,7 @@ int act_parse_police(struct action_util *a,int *argc_p, char ***argv_p, int tca_
while (argc > 0) {
fprintf(stderr,"police argc %d argv %s\n", argc, *argv);
if (matches(*argv, "index") == 0) {
NEXT_ARG();
if (get_u32(&p.index, *argv, 10)) {
......@@ -230,8 +230,7 @@ int act_parse_police(struct action_util *a,int *argc_p, char ***argv_p, int tca_
p.action = TC_POLICE_OK;
} else if (matches(*argv, "pipe") == 0) {
p.action = TC_POLICE_PIPE;
} else if (strcmp(*argv, "action") == 0 ||
strcmp(*argv, "conform-exceed") == 0) {
} else if (strcmp(*argv, "conform-exceed") == 0) {
NEXT_ARG();
if (get_police_result(&p.action, &presult, *argv)) {
fprintf(stderr, "Illegal \"action\"\n");
......
......@@ -188,9 +188,9 @@ static int print_simple(struct action_util *au, FILE * f, struct rtattr *arg)
if (tb[TCA_DEF_TM]) {
struct tcf_t *tm = RTA_DATA(tb[TCA_DEF_TM]);
print_tm(f, tm);
fprintf(f, "\n");
}
}
fprintf(f, "\n");
return 0;
}
......
......@@ -12,9 +12,8 @@
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307 USA.
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>.
*
* Authors: Alexander Duyck <alexander.h.duyck@intel.com>
*
......@@ -100,6 +99,7 @@ parse_skbedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
argv++;
}
sel.action = TC_ACT_PIPE;
if (argc) {
if (matches(*argv, "reclassify") == 0) {
sel.action = TC_ACT_RECLASSIFY;
......@@ -165,6 +165,7 @@ static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
__u32 *priority;
__u32 *mark;
__u16 *queue_mapping;
struct tc_skbedit *p = NULL;
if (arg == NULL)
return -1;
......@@ -175,6 +176,7 @@ static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
fprintf(f, "[NULL skbedit parameters]");
return -1;
}
p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]);
fprintf(f, " skbedit");
......@@ -191,6 +193,8 @@ static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
fprintf(f, " mark %d", *mark);
}
fprintf(f, "\n\t index %d ref %d bind %d", p->index, p->refcnt, p->bindcnt);
if (show_stats) {
if (tb[TCA_SKBEDIT_TM]) {
struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]);
......@@ -198,6 +202,8 @@ static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg)
}
}
fprintf(f, "\n ");
return 0;
}
......
......@@ -12,9 +12,8 @@
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307 USA.
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses>.
*
* Author: Alexander Duyck <alexander.h.duyck@intel.com>
*
......
......@@ -198,28 +198,22 @@ int get_rate64(__u64 *rate, const char *str)
void print_rate(char *buf, int len, __u64 rate)
{
double tmp = (double)rate*8;
extern int use_iec;
unsigned long kilo = use_iec ? 1024 : 1000;
const char *str = use_iec ? "i" : "";
int i = 0;
static char *units[5] = {"", "K", "M", "G", "T"};
if (use_iec) {
if (tmp >= 1000.0*1024.0*1024.0*1024.0)
snprintf(buf, len, "%.0fGibit", tmp/(1024.0*1024.0*1024.0));
else if (tmp >= 1000.0*1024.0*1024.0)
snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0));
else if (tmp >= 1000.0*1024)
snprintf(buf, len, "%.0fKibit", tmp/1024);
else
snprintf(buf, len, "%.0fbit", tmp);
} else {
if (tmp >= 1000.0*1000000000.0)
snprintf(buf, len, "%.0fGbit", tmp/1000000000.0);
else if (tmp >= 1000.0*1000000.0)
snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
else if (tmp >= 1000.0 * 1000.0)
snprintf(buf, len, "%.0fKbit", tmp/1000.0);
else
snprintf(buf, len, "%.0fbit", tmp);
rate <<= 3; /* bytes/sec -> bits/sec */
for (i = 0; i < ARRAY_SIZE(units); i++) {
if (rate < kilo)
break;
if (((rate % kilo) != 0) && rate < 1000*kilo)
break;
rate /= kilo;
}
snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
}
char * sprint_rate(__u64 rate, char *buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment