From 07cba30d6ad61d3a8df110789b8a5704c4405fad Mon Sep 17 00:00:00 2001 From: derekwin <1446103183@qq.com> Date: Fri, 19 Apr 2024 10:31:37 +0800 Subject: [PATCH] revert 34d5030d6e05dbf6b970caf839c6c323fc0801c3 --- README.md | 76 +----- bpf/CMakeLists.txt | 1 - bpf/bwm_tc.c | 103 +++++++- bpf/bwm_tc.h | 6 - bpf/bwm_tc_common.h | 110 --------- bpf/bwm_tc_i.c | 101 -------- bwmcli.c | 453 ++++++++++------------------------- bwmcli.h | 8 +- tools/README.md | 10 - tools/bwm_monitor_ingress.bt | 155 ------------ 10 files changed, 250 insertions(+), 773 deletions(-) delete mode 100644 bpf/bwm_tc_common.h delete mode 100644 bpf/bwm_tc_i.c delete mode 100755 tools/bwm_monitor_ingress.bt diff --git a/README.md b/README.md index 94c3764..172d4d3 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,8 @@ 具体提供了下列功能: ### 支持设置Pod网络优先级 -##### 出向 1. 以执行bwmcli命令的方式设置某个cgroup的网络优先级,执行bwmcli命令时需要指定的参数包括:cgrp_path、prio 2. 优先级默认值为0,0标识为在线业务,-1标识为离线业务。 -##### 入向 -1. 以执行bwmcli命令的方式设置某个目标ip数据包的网络优先级,执行bwmcli命令时需要指定的参数包括:ip -2. 未设置的ip默认被认为是在线业务,被设置ip被认为是离线业务。 ### 支持设置离线业务网络带宽限制 1. Pod网络带宽限制对所有离线业务生效,所有离线业务的总带宽不能超过设置的网络带宽限制,在线业务没有网络带宽限制。 @@ -36,7 +32,7 @@ oncn-bwm │ LICENSE │ README.md │ -├─bpf # 三个ebpf程序文件实现带宽管理逻辑 +├─bpf # 两个ebpf程序文件实现带宽管理逻辑 │ └─tools # 在离线带宽检测工具,基于bpftrace ``` @@ -48,10 +44,8 @@ oncn-bwm 3. mkdir -p /usr/share/bwmcli 4. install -Dpm 0500 bpf/CMakeFiles/bwm_prio_kern.dir/bwm_prio_kern.c.o /usr/share/bwmcli/bwm_prio_kern.o 5. install -Dpm 0500 bpf/CMakeFiles/bwm_tc.dir/bwm_tc.c.o /usr/share/bwmcli/bwm_tc.o -6. install -Dpm 0500 bpf/CMakeFiles/bwm_tc_i.dir/bwm_tc_i.c.o /usr/share/bwmcli/bwm_tc_i.o -7. install -Dpm 0500 bwmcli /usr/bin -8. install -Dpm 0500 tools/bwm_monitor.bt /usr/bin -9. install -Dpm 0500 tools/bwm_monitor_ingress.bt /usr/bin +6. install -Dpm 0500 bwmcli /usr/bin +7. install -Dpm 0500 tools/bwm_monitor.bt /usr/bin ## 使用说明 @@ -62,8 +56,6 @@ oncn-bwm ``` bwmcli –e/-d ethx 使能/除能某个网卡的Qos功能 bwmcli –e/-d 使能/除能所有网卡的Qos功能 -bwmcli -E/-D ethx 使能/除能某个网卡的入向Qos功能 -bwmcli –E/-D 使能/除能所有网卡的入向Qos功能 ``` 示例 ``` @@ -78,7 +70,7 @@ disable eth1 success **接口2** -说明(用于出向) +说明 ``` bwmcli –s path 设置某个cgroup的优先级 bwmcli –p path 查询某个cgroup的优先级 @@ -94,88 +86,53 @@ prio is 0 **接口3** -说明(用于入向) -``` -bwmcli –A 172.17.0.2 标识对应ip入向流为离线流量 -bwmcli –R 172.17.0.2 标识对应ip入向流为在线流量 -``` -示例 -``` -# bwmcli –A 172.17.0.2 -AddIp 172.17.0.2 success -# bwmcli –R 172.17.0.2 -RemoveIp 172.17.0.2 success -``` - -**接口4** - 说明 ``` bwmcli –s bandwidth 设置离线带宽 bwmcli –p bandwidth 查询离线带宽 -bwmcli –S bandwidth 设置入向离线带宽 -bwmcli –P bandwidth 查询入向离线带宽 ``` 示例 ``` # bwmcli -s bandwidth 30mb,100mb set bandwidth success -# bwmcli -S bandwidth 30mb,100mb -set bandwidth success # bwmcli -p bandwidth bandwidth is 31457280(B),104857600(B) -# bwmcli -P bandwidth -bandwidth is 31457280(B),104857600(B) ``` -**接口5** +**接口4** 说明 ``` bwmcli –s waterline 设置在线水线 bwmcli –p waterline 查询在线水线 -bwmcli –S waterline 设置入向在线水线 -bwmcli –P waterline 查询入向在线水线 ``` 示例 ``` # bwmcli -s waterline 20mb set waterline success -# bwmcli -S waterline 20mb -set waterline success # bwmcli -p waterline waterline is 20971520 (B) -# bwmcli -P waterline -waterline is 20971520 (B) ``` -**接口6** +**接口5** 说明 ``` -bwmcli –p stats 打印出向流量内部统计信息 -bwmcli –P stats 打印入向流量内部统计信息 +bwmcli –p stats 打印内部统计信息 ``` 示例 ``` # bwmcli -p stats -offline_target_bandwidth: 104857600 -online_pkts: 982 -offline_pkts: 0 -online_rate: 28190 -offline_rate: 0 - -# bwmcli –P stats -offline_target_bandwidth: 1073741824 -online_pkts: 1150 -offline_pkts: 0 -online_rate: 27306 -offline_rate: 0 +rate: 1073741824 +online_pkts: 79752 +offline_pkts: 69730 +online rate past: 0 +offline rate past: 916194823 ``` -**接口7** +**接口6** 说明 ``` @@ -186,10 +143,6 @@ bwmcli –p devs 描述系统上所有网卡的使能状态 # bwmcli –p devs lo : disabled enp2s2 : disabled - -# bwmcli –P devs -lo : disabled -enp2s2 : disabled ``` ### 典型使用案例 @@ -200,9 +153,6 @@ bwmcli -s /sys/fs/cgroup/net_cls/offline -1 bwmcli -e eth0 使能eth0的网卡 Qos功能 bwmcli -s bandwidth 20mb,1gb 配置离线业务带宽 bwmcli -s waterline 30mb 配置在线业务的水线 -bwmcli -E veth123456 使能veth123456的网卡(宿主侧) Qos功能(对应pod入向流量) -bwmcli –A 172.17.0.2 配置目标ip标识入向离线流 -bwmcli –R 172.17.0.2 删除目标ip标识入向离线流 ``` ## 参与贡献 diff --git a/bpf/CMakeLists.txt b/bpf/CMakeLists.txt index 6ac3a43..d081520 100644 --- a/bpf/CMakeLists.txt +++ b/bpf/CMakeLists.txt @@ -7,5 +7,4 @@ set(CMAKE_C_COMPILER "clang") set(CMAKE_C_FLAGS "-g -Wall -O2 -Werror --target=bpf -D__x86_64__ -fPIC -D_FORTIFY_SOURCE=2 -ftrapv") add_library(bwm_tc OBJECT bwm_tc) -add_library(bwm_tc_i OBJECT bwm_tc_i) add_library(bwm_prio_kern OBJECT bwm_prio_kern) diff --git a/bpf/bwm_tc.c b/bpf/bwm_tc.c index 9ecc1a2..3585c6d 100644 --- a/bpf/bwm_tc.c +++ b/bpf/bwm_tc.c @@ -10,9 +10,28 @@ #include #include #include +#include #include "bwm_tc.h" -#include "bwm_tc_common.h" + +#ifndef BWM_DEBUG // Define BWM_DEBUG to enable debugging +#undef bpf_printk +#define bpf_printk(fmt, ...) +#endif + +#define PIN_GLOBAL_NS 2 +#define OFFLINE_PRIO ((unsigned int)-1) + +struct bpf_elf_map_t { + __u32 type; + __u32 key_size; + __u32 value_size; + __u32 max_elem; + __u32 flags; + __u32 id; + __u32 pinning; +}; + struct bpf_elf_map_t SEC("maps") throttle_cfg = { .type = BPF_MAP_TYPE_ARRAY, @@ -34,6 +53,88 @@ struct bpf_elf_map_t SEC("maps") throttle_map = { .id = 0, }; +static void throttle_init(const struct edt_throttle_cfg *cfg, + struct edt_throttle *throttle) +{ + throttle->t_last = 0; + throttle->rate = cfg->low_rate; + + throttle->tx_bytes = 0; + throttle->t_start = 0; +} + +static void bwm_online(const struct __sk_buff *skb, struct edt_throttle *throttle) +{ + __sync_fetch_and_add(&throttle->online_tx_bytes, skb->len); + __sync_fetch_and_add(&throttle->stats.online_pkts, 1); +} + +static void bwm_offline(struct __sk_buff *skb, struct edt_throttle *throttle) +{ + unsigned long long t_cur; + unsigned long long t_send; + unsigned long long t_delay; + unsigned long long t_next; + + __sync_fetch_and_add(&throttle->tx_bytes, skb->len); + __sync_fetch_and_add(&throttle->stats.offline_pkts, 1); + + // 1. EDT schedule departure + t_cur = bpf_ktime_get_ns(); + t_send = skb->tstamp; + + if (t_send < t_cur) + t_send = t_cur; + + t_delay = skb->len * NSEC_PER_SEC / throttle->rate; + t_next = throttle->t_last + t_delay; + + if (t_next <= t_send) { + throttle->t_last = t_send; + return; + } + + skb->tstamp = t_next; + throttle->t_last = t_next; + return; +} + + +static void adjust_rate(const struct edt_throttle_cfg *cfg, struct edt_throttle *throttle) +{ + unsigned long long t_cur; + unsigned long long t_past; + unsigned long long rate_past; + unsigned long long offline_rate_past; + + // 2. check if need to adjust offline speed + t_cur = bpf_ktime_get_ns(); + t_past = t_cur - throttle->t_start; + if (t_past > cfg->interval) { + throttle->t_start = t_cur; + rate_past = throttle->online_tx_bytes * NSEC_PER_SEC / t_past; + offline_rate_past = throttle->tx_bytes * NSEC_PER_SEC / t_past; + + if (rate_past >= cfg->water_line) { + throttle->rate = cfg->low_rate; + __sync_fetch_and_add(&throttle->stats.low_times, 1); + } else { + throttle->rate = cfg->high_rate; + __sync_fetch_and_add(&throttle->stats.high_times, 1); + } + + /* we can safety update without lock */ + if (throttle->t_start == t_cur) { + throttle->online_tx_bytes = 0; + throttle->tx_bytes = 0; + } + + throttle->stats.rate_past = rate_past; + throttle->stats.offline_rate_past = offline_rate_past; + __sync_fetch_and_add(&throttle->stats.check_times, 1); + } +} + SEC("tc") int bwm_tc(struct __sk_buff *skb) { diff --git a/bpf/bwm_tc.h b/bpf/bwm_tc.h index 7e50b75..ff60f66 100644 --- a/bpf/bwm_tc.h +++ b/bpf/bwm_tc.h @@ -8,10 +8,6 @@ #define THROTTLE_MAP_PATH "/sys/fs/bpf/tc/globals/throttle_map" #define THROTTLE_CFG_PATH "/sys/fs/bpf/tc/globals/throttle_cfg" -#define THROTTLE_I_MAP_PATH "/sys/fs/bpf/tc/globals/throttle_i_map" -#define THROTTLE_I_CFG_PATH "/sys/fs/bpf/tc/globals/throttle_i_cfg" -#define IPS_I_MAP_PATH "/sys/fs/bpf/tc/globals/ips_i_map" - #undef NSEC_PER_SEC #undef NSEC_PER_MSEC @@ -24,8 +20,6 @@ #define LOWEST_BANDWIDTH (1LL * 1024 * 1024) #define HIGHEST_BANDWIDTH (9999LL * 1024 * 1024 * 1024) -#define IPS_MAX_NUM 4096 - struct throttle_stats { unsigned long long check_times; unsigned long long high_times; diff --git a/bpf/bwm_tc_common.h b/bpf/bwm_tc_common.h deleted file mode 100644 index 41dde34..0000000 --- a/bpf/bwm_tc_common.h +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved. - * Description: Network bandwidth management tool - */ -#ifndef __BWM_TC_COMMON_H__ -#define __BWM_TC_COMMON_H__ - -#include - -#ifndef BWM_DEBUG // Define BWM_DEBUG to enable debugging -#undef bpf_printk -#define bpf_printk(fmt, ...) -#endif - -#define PIN_GLOBAL_NS 2 -#define OFFLINE_PRIO ((unsigned int)-1) - -struct bpf_elf_map_t { - __u32 type; - __u32 key_size; - __u32 value_size; - __u32 max_elem; - __u32 flags; - __u32 id; - __u32 pinning; -}; - -static inline void bwm_online(const struct __sk_buff *skb, struct edt_throttle *throttle) -{ - __sync_fetch_and_add(&throttle->online_tx_bytes, skb->len); - __sync_fetch_and_add(&throttle->stats.online_pkts, 1); -} - -static inline void bwm_offline(struct __sk_buff *skb, struct edt_throttle *throttle) -{ - unsigned long long t_cur; - unsigned long long t_send; - unsigned long long t_delay; - unsigned long long t_next; - - __sync_fetch_and_add(&throttle->tx_bytes, skb->len); - __sync_fetch_and_add(&throttle->stats.offline_pkts, 1); - - // 1. EDT schedule departure - t_cur = bpf_ktime_get_ns(); - t_send = skb->tstamp; - - if (t_send < t_cur) - t_send = t_cur; - - t_delay = skb->len * NSEC_PER_SEC / throttle->rate; - t_next = throttle->t_last + t_delay; - - if (t_next <= t_send) { - throttle->t_last = t_send; - return; - } - - skb->tstamp = t_next; - throttle->t_last = t_next; - return; -} - - -static inline void adjust_rate(const struct edt_throttle_cfg *cfg, struct edt_throttle *throttle) -{ - unsigned long long t_cur; - unsigned long long t_past; - unsigned long long rate_past; - unsigned long long offline_rate_past; - - // 2. check if need to adjust offline speed - t_cur = bpf_ktime_get_ns(); - t_past = t_cur - throttle->t_start; - if (t_past > cfg->interval) { - throttle->t_start = t_cur; - rate_past = throttle->online_tx_bytes * NSEC_PER_SEC / t_past; - offline_rate_past = throttle->tx_bytes * NSEC_PER_SEC / t_past; - - if (rate_past >= cfg->water_line) { - throttle->rate = cfg->low_rate; - __sync_fetch_and_add(&throttle->stats.low_times, 1); - } else { - throttle->rate = cfg->high_rate; - __sync_fetch_and_add(&throttle->stats.high_times, 1); - } - - /* we can safety update without lock */ - if (throttle->t_start == t_cur) { - throttle->online_tx_bytes = 0; - throttle->tx_bytes = 0; - } - - throttle->stats.rate_past = rate_past; - throttle->stats.offline_rate_past = offline_rate_past; - __sync_fetch_and_add(&throttle->stats.check_times, 1); - } -} - -static inline void throttle_init(const struct edt_throttle_cfg *cfg, - struct edt_throttle *throttle) -{ - throttle->t_last = 0; - throttle->rate = cfg->low_rate; - - throttle->tx_bytes = 0; - throttle->t_start = 0; -} - -#endif diff --git a/bpf/bwm_tc_i.c b/bpf/bwm_tc_i.c deleted file mode 100644 index 75c6741..0000000 --- a/bpf/bwm_tc_i.c +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved. - * Description: Network bandwidth management tool - */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bwm_tc.h" -#include "bwm_tc_common.h" - -struct bpf_elf_map_t SEC("maps") throttle_i_cfg = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(unsigned int), - .value_size = sizeof(struct edt_throttle_cfg), - .pinning = PIN_GLOBAL_NS, - .max_elem = 1, - .flags = 0, - .id = 0, -}; - -struct bpf_elf_map_t SEC("maps") throttle_i_map = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(unsigned int), - .value_size = sizeof(struct edt_throttle), - .pinning = PIN_GLOBAL_NS, - .max_elem = 1, - .flags = 0, - .id = 0, -}; - -struct bpf_elf_map_t SEC("maps") ips_i_map = { - .type = BPF_MAP_TYPE_LRU_HASH, - .key_size = sizeof(__u32), // ipv4 - .value_size = sizeof(int), - .pinning = PIN_GLOBAL_NS, - .max_elem = IPS_MAX_NUM, - .flags = 0, - .id = 0, -}; - -SEC("tc") -int bwm_tc(struct __sk_buff *skb) -{ - struct edt_throttle *throttle = NULL; - struct edt_throttle_cfg * cfg = NULL; - unsigned int map_index = 0; - unsigned long ip = 0; - int *prio = NULL; - - cfg = bpf_map_lookup_elem(&throttle_i_cfg, &map_index); - if (cfg == NULL) - return TC_ACT_OK; - - throttle = bpf_map_lookup_elem(&throttle_i_map, &map_index); - if (throttle == NULL) - return TC_ACT_OK; - const struct edt_throttle_cfg * cfg_con = cfg; - if (throttle->rate == 0) - throttle_init(cfg_con, throttle); - - // skb->remote_ip4 is not visiable to tc, we need parse dest_ip from skb handly - // https://github1s.com/libbpf/libbpf-bootstrap/blob/HEAD/examples/c/tc.bpf.c#L12 - void *data_end = (void *)(__u64)skb->data_end; - void *data = (void *)(__u64)skb->data; - struct ethhdr *l2; - struct iphdr *l3; - - if (skb->protocol != bpf_htons(ETH_P_IP)) - return TC_ACT_OK; - l2 = data; - if ((void *)(l2 + 1) > data_end) - return TC_ACT_OK; - - l3 = (struct iphdr *)(l2 + 1); - if ((void *)(l3 + 1) > data_end) - return TC_ACT_OK; - - ip = l3->daddr; - - const struct __sk_buff *skb_con = skb; - prio = bpf_map_lookup_elem(&ips_i_map, &ip); - if (prio != NULL) { - // matched: offline flow - bwm_offline(skb, throttle); - } else { - // dismatched: online flow - bwm_online(skb_con, throttle); - } - adjust_rate(cfg_con, throttle); - - bpf_printk("[tc.c]dest_ip=%u\n", ip); - return TC_ACT_OK; -} - -char _license[] SEC("license") = "GPL"; diff --git a/bwmcli.c b/bwmcli.c index c912c37..f087ea6 100644 --- a/bwmcli.c +++ b/bwmcli.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -49,29 +48,6 @@ static struct TcCmd g_enableSeq[] = { } }; -static struct TcCmd g_enableSeqIngress[] = { - { - .cmdStr = "tc qdisc del dev %s root >/dev/null 2>&1", - .verifyRet = false, - }, - { - .cmdStr = "tc qdisc del dev %s clsact >/dev/null 2>&1", - .verifyRet = false, - }, - { - .cmdStr = "tc qdisc add dev %s root fq", - .verifyRet = true, - }, - { - .cmdStr = "tc qdisc add dev %s clsact", - .verifyRet = true, - }, - { - .cmdStr = "tc filter add dev %s egress bpf direct-action obj " TC_PROG_I " sec tc >/dev/null 2>&1", - .verifyRet = true, - } -}; - static struct TcCmd g_disableSeq[] = { { .cmdStr = "tc filter del dev %s egress", @@ -89,44 +65,36 @@ static struct TcCmd g_disableSeq[] = { static const struct option g_helps[] = { {"Display this information", no_argument, NULL, 'h' }, - {"Set egress configuration", required_argument, NULL, 's' }, - {"Set ingress configuration", required_argument, NULL, 'S' }, - {"Display egress configuration", required_argument, NULL, 'p' }, - {"Display ingress configuration", required_argument, NULL, 'P' }, - {"Enable egress bandwidth management of the network device ", required_argument, NULL, 'e' }, - {"Enable ingress bandwidth management of the network device ", required_argument, NULL, 'E' }, - {"Disable egress bandwidth management of the network device ", required_argument, NULL, 'd' }, - {"Disable ingress bandwidth management of the network device ", required_argument, NULL, 'D' }, + {"Set configuration", required_argument, NULL, 's' }, + {"Display configuration", required_argument, NULL, 'p' }, + {"Enable bandwidth management of the network device ", required_argument, NULL, 'e' }, + {"Disable bandwidth management of the network device ", required_argument, NULL, 'd' }, {"Display the version number of bwmcli", no_argument, NULL, 'v' }, {0, 0, NULL, 0 } }; static const struct option g_longOptions[] = { {"help", no_argument, NULL, 'h' }, - {"set=< |bandwidth |waterline > | egress", required_argument, NULL, 's' }, - {"set=|waterline > | ingress", required_argument, NULL, 'S' }, - {"print=<|bandwidth|waterline|stats|devs> | egress", required_argument, NULL, 'p' }, - {"print= | ingress", required_argument, NULL, 'P' }, - {"enable[=] | egress\t", optional_argument, NULL, 'e' }, - {"enable[=] | ingress\t", optional_argument, NULL, 'E' }, - {"disable[=] | egress\t", optional_argument, NULL, 'd' }, - {"disable[=] | ingress\t", optional_argument, NULL, 'D' }, + {"set=< |bandwidth |waterline >", required_argument, NULL, 's' }, + {"print=<|bandwidth|waterline|stats|devs>", required_argument, NULL, 'p' }, + {"enable[=]", optional_argument, NULL, 'e' }, + {"disable[=]", optional_argument, NULL, 'd' }, {"version", no_argument, NULL, 'v' }, {0, 0, NULL, 0 } }; static char g_cmdBuf[MAX_CMD_LEN]; -static int GetCgroupPrio(void *cgrpPath, int isIngress); // don't need isIngress -static int SetCgrpPrio(void *cgrpPath, void *args, int isIngress); // don't need isIngress -static int GetBandwidth(void *unused, int isIngress); -static int SetBandwidth(void *cgrpPath, void *args, int isIngress); -static int GetWaterline(void *unused, int isIngress); -static int SetWaterline(void *cgrpPath, void *args, int isIngress); -static int GetStats(void *unused, int isIngress); -static int GetDevs(void *unused, int isIngress); -static int ForeachEthdev(NetdevCallback fn, const void *arg, int isIngress); -static bool InitCfgMap(int isIngress); +static int GetCgroupPrio(void *cgrpPath); +static int SetCgrpPrio(void *cgrpPath, void *args); +static int GetBandwidth(void *unused); +static int SetBandwidth(void *cgrpPath, void *args); +static int GetWaterline(void *unused); +static int SetWaterline(void *cgrpPath, void *args); +static int GetStats(void *unused); +static int GetDevs(void *unused); +static int ForeachEthdev(NetdevCallback fn, const void *arg); +static bool InitCfgMap(); static struct CfgOption g_defaultOption = { .name = "", @@ -533,26 +501,30 @@ static int NetdevEnabledSub(const char *format, const char *ethdev) } // return: 1 is enabled. 0 is disabled. -static int NetdevEnabled(const char *ethdev, int isIngress) +static int NetdevEnabled(const char *ethdev) { - const char *format = isIngress ? "tc filter show dev %s egress|grep bwm_tc_i.o >/dev/null 2>&1" : "tc filter show dev %s egress|grep bwm_tc.o >/dev/null 2>&1" ; - - if (NetdevEnabledSub(format, ethdev) != 0) { - return 1; + const char *format = "tc filter show dev %s egress|grep bwm_tc.o >/dev/null 2>&1"; + if (NetdevEnabledSub(format, ethdev) == 0) { + return 0; + } + + const char *format1 = "tc qdisc ls dev %s|grep \"qdisc fq \" >/dev/null 2>&1"; + if (NetdevEnabledSub(format1, ethdev) == 0) { + return 0; } - return 0; + return 1; } -static int DisableSpecificNetdevice(const char *ethdev, const void *unused, int isIngress) +static int DisableSpecificNetdevice(const char *ethdev, const void *unused) { size_t i; int ret; BWM_LOG_DEBUG("DisableSpecificNetdevice: %s\n", ethdev); - if (NetdevEnabled(ethdev, isIngress) == 0) { - BWM_LOG_INFO("%s %s has already disabled\n", ethdev, (isIngress ? "ingress" : "egress")); + if (NetdevEnabled(ethdev) == 0) { + BWM_LOG_INFO("%s has already disabled\n", ethdev); return EXIT_OK; } @@ -644,78 +616,44 @@ qdisc: return ret == 0 ? 1 : 0; } -static int EnableSpecificNetdevice(const char *ethdev, const void *unused, int isIngress) +static int EnableSpecificNetdevice(const char *ethdev, const void *unused) { unsigned long i; int ret; - if (isIngress) { - BWM_LOG_DEBUG("EnableSpecificNetdeviceIngress: %s\n", ethdev); + BWM_LOG_DEBUG("EnableSpecificNetdevice: %s\n", ethdev); - if (NetdevEnabled(ethdev, isIngress) == 1) { - BWM_LOG_INFO("%s %s has already enabled\n", ethdev, (isIngress ? "ingress" : "egress")); - return EXIT_OK; - } - - if (!DefaultTc(ethdev)) { - BWM_LOG_INFO("%s has already enabled other TC\n", ethdev); - return EXIT_OK; - } - - for (i = 0; i < sizeof(g_enableSeqIngress) / sizeof(struct TcCmd); i++) { - ret = snprintf(g_cmdBuf, MAX_CMD_LEN, g_enableSeqIngress[i].cmdStr, ethdev); - if (ret < 0 || g_cmdBuf[MAX_CMD_LEN - 1] != '\0') { - BWM_LOG_ERR("Invalid net device: %s\n", ethdev); - return EXIT_FAIL_OPTION; - } - - ret = system(g_cmdBuf); - if (ret < 0) { - BWM_LOG_ERR("execute cmd[%s] error: %d\n", g_cmdBuf, ret); - goto clear; - } + if (NetdevEnabled(ethdev) == 1) { + BWM_LOG_INFO("%s has already enabled\n", ethdev); + return EXIT_OK; + } - ret = WEXITSTATUS(ret); - if (ret && g_enableSeqIngress[i].verifyRet) { - BWM_LOG_ERR("execute cmd ret wrong: %s\n", g_enableSeqIngress[i].cmdStr); - goto clear; - } - } - } else { - BWM_LOG_DEBUG("EnableSpecificNetdevice: %s\n", ethdev); + if (!DefaultTc(ethdev)) { + BWM_LOG_INFO("%s has already enabled other TC\n", ethdev); + return EXIT_OK; + } - if (NetdevEnabled(ethdev, isIngress) == 1) { - BWM_LOG_INFO("%s has already enabled %s\n", ethdev, isIngress ? "ingress" : "egress"); - return EXIT_OK; + for (i = 0; i < sizeof(g_enableSeq) / sizeof(struct TcCmd); i++) { + ret = snprintf(g_cmdBuf, MAX_CMD_LEN, g_enableSeq[i].cmdStr, ethdev); + if (ret < 0 || g_cmdBuf[MAX_CMD_LEN - 1] != '\0') { + BWM_LOG_ERR("Invalid net device: %s\n", ethdev); + return EXIT_FAIL_OPTION; } - if (!DefaultTc(ethdev)) { - BWM_LOG_INFO("%s has already enabled other TC\n", ethdev); - return EXIT_OK; + ret = system(g_cmdBuf); + if (ret < 0) { + BWM_LOG_ERR("execute cmd[%s] error: %d\n", g_cmdBuf, ret); + goto clear; } - for (i = 0; i < sizeof(g_enableSeq) / sizeof(struct TcCmd); i++) { - ret = snprintf(g_cmdBuf, MAX_CMD_LEN, g_enableSeq[i].cmdStr, ethdev); - if (ret < 0 || g_cmdBuf[MAX_CMD_LEN - 1] != '\0') { - BWM_LOG_ERR("Invalid net device: %s\n", ethdev); - return EXIT_FAIL_OPTION; - } - - ret = system(g_cmdBuf); - if (ret < 0) { - BWM_LOG_ERR("execute cmd[%s] error: %d\n", g_cmdBuf, ret); - goto clear; - } - - ret = WEXITSTATUS(ret); - if (ret && g_enableSeq[i].verifyRet) { - BWM_LOG_ERR("execute cmd ret wrong: %s\n", g_enableSeq[i].cmdStr); - goto clear; - } + ret = WEXITSTATUS(ret); + if (ret && g_enableSeq[i].verifyRet) { + BWM_LOG_ERR("execute cmd ret wrong: %s\n", g_enableSeq[i].cmdStr); + goto clear; } } - - if (!InitCfgMap(isIngress)) { + + if (!InitCfgMap()) { goto clear; } @@ -723,7 +661,7 @@ static int EnableSpecificNetdevice(const char *ethdev, const void *unused, int i return EXIT_OK; clear: - (void)DisableSpecificNetdevice(ethdev, NULL, isIngress); + (void)DisableSpecificNetdevice(ethdev, NULL); return EXIT_FAIL; } @@ -737,7 +675,7 @@ static bool CheckDevNameIsLegalChar(const char c) return false; } -static int ForeachEthdev(NetdevCallback fn, const void *arg, int isIngress) +static int ForeachEthdev(NetdevCallback fn, const void *arg) { int ret = EXIT_OK; int i; @@ -785,7 +723,7 @@ static int ForeachEthdev(NetdevCallback fn, const void *arg, int isIngress) continue; } - ret = fn(start, arg, isIngress); + ret = fn(start, arg); if (ret != EXIT_OK) { (void)fclose(fstream); return ret; @@ -796,28 +734,25 @@ static int ForeachEthdev(NetdevCallback fn, const void *arg, int isIngress) return ret; } -static int DisableAllNetdevice(int isIngress) +static int DisableAllNetdevice(void) { int ret; BWM_LOG_DEBUG("DisableAllNetdevice\n"); - ret = ForeachEthdev(DisableSpecificNetdevice, NULL, isIngress); + ret = ForeachEthdev(DisableSpecificNetdevice, NULL); return ret; } -static int EnableAllNetdevice(int isIngress) +static int EnableAllNetdevice(void) { int ret; - if (isIngress) - BWM_LOG_DEBUG("EnableAllNetdevice\n"); - else - BWM_LOG_DEBUG("EnableAllNetdeviceIngress\n"); + BWM_LOG_DEBUG("EnableAllNetdevice\n"); - ret = ForeachEthdev(EnableSpecificNetdevice, NULL, isIngress); + ret = ForeachEthdev(EnableSpecificNetdevice, NULL); if (ret != EXIT_OK) { - (void)DisableAllNetdevice(isIngress); + (void)DisableAllNetdevice(); } return ret; @@ -832,19 +767,19 @@ static void PrintThrottle(const struct edt_throttle *throttle) BWM_LOG_INFO("offline_rate: %llu\n", throttle->stats.offline_rate_past); } -static int DevStatShow(const char *ethdev, const void *arg, int isIngress) +static int DevStatShow(const char *ethdev, const void *arg) { int ret; - ret = NetdevEnabled(ethdev, isIngress); - BWM_LOG_INFO("%-16s: %s\n", ethdev, (ret == 0) ? "disabled" : (isIngress ? "ingress enabled" : "egress enabled")); + ret = NetdevEnabled(ethdev); + BWM_LOG_INFO("%-16s: %s\n", ethdev, (ret == 0) ? "disabled" : "enabled"); return EXIT_OK; } -static void PrintDevsStats(int isIngress) +static void PrintDevsStats(void) { - (void)ForeachEthdev(DevStatShow, NULL, isIngress); + (void)ForeachEthdev(DevStatShow, NULL); } static struct CfgOption *FindOptions(const char *cfg) @@ -860,20 +795,20 @@ static struct CfgOption *FindOptions(const char *cfg) return &g_defaultOption; } -static int GetCfgsInfo(char *cfg, int cfgLen, int isIngress) +static int GetCfgsInfo(char *cfg, int cfgLen) { struct CfgOption *option; option = FindOptions(cfg); - return option->op.getCfg(cfg, isIngress); + return option->op.getCfg(cfg); } -static int SetCfgsInfo(char *cfg, int cfgLen, char *args, int argsLen, int isIngress) +static int SetCfgsInfo(char *cfg, int cfgLen, char *args, int argsLen) { struct CfgOption *option; option = FindOptions(cfg); - return option->op.setCfg(cfg, args, isIngress); + return option->op.setCfg(cfg, args); } static int BreakMultiArgs(char *args, char arg1[], char arg2[], int mutilArgs) @@ -900,6 +835,7 @@ static int BreakMultiArgs(char *args, char arg1[], char arg2[], int mutilArgs) return EXIT_OK; } + static int ParseUnit(char arg[], int setPrio, long long *val) { int unit; @@ -959,7 +895,7 @@ static int ParseArgs(char *args, long long *val1, long long *val2, int mutilArgs return EXIT_OK; } -static int CfgsInfo(int argc, char **argv, int isSet, int isIngress) +static int CfgsInfo(int argc, char **argv, int isSet) { int ret; char option[PATH_MAX + 1] = {0}; @@ -976,7 +912,7 @@ static int CfgsInfo(int argc, char **argv, int isSet, int isIngress) (void)fprintf(stderr, "invalid option, extra parameter: %s\n", optarg); return EXIT_FAIL_OPTION; } - ret = GetCfgsInfo(option, PATH_MAX + 1, isIngress); + ret = GetCfgsInfo(option, PATH_MAX + 1); return ret; } @@ -986,7 +922,7 @@ static int CfgsInfo(int argc, char **argv, int isSet, int isIngress) return EXIT_FAIL_OPTION; } - ret = SetCfgsInfo(option, PATH_MAX + 1, args, PRIO_LEN, isIngress); + ret = SetCfgsInfo(option, PATH_MAX + 1, args, PRIO_LEN); if (ret != EXIT_OK) { return ret; } @@ -997,24 +933,24 @@ static int CfgsInfo(int argc, char **argv, int isSet, int isIngress) return EXIT_OK; } -static int NetdevCmp(const char *ethdev, const void *arg, int isIngress) +static int NetdevCmp(const char *ethdev, const void *arg) { return (strcmp(ethdev, arg) == 0) ? 1 : 0; } inline static int IsValidEthdev(const char *dev) { - return ForeachEthdev(NetdevCmp, dev, 0); + return ForeachEthdev(NetdevCmp, dev); } -static int ChangeNetdeviceStatus(int argc, char **argv, int enable, int isIngress) +static int ChangeNetdeviceStatus(int argc, char **argv, int enable) { int ret; char ethdev[NAME_MAX + 1] = {0}; if (optarg == NULL && (optind >= argc || argv[optind][0] == '-')) { /* enable all eth device */ - ret = (enable == 0) ? DisableAllNetdevice(isIngress) : EnableAllNetdevice(isIngress); + ret = (enable == 0) ? DisableAllNetdevice() : EnableAllNetdevice(); return ret; } @@ -1035,12 +971,12 @@ static int ChangeNetdeviceStatus(int argc, char **argv, int enable, int isIngres return EXIT_FAIL_OPTION; } - ret = (enable == 1) ? EnableSpecificNetdevice(ethdev, NULL, isIngress) : DisableSpecificNetdevice(ethdev, NULL, isIngress); - + ret = (enable == 1) ? EnableSpecificNetdevice(ethdev, NULL) : DisableSpecificNetdevice(ethdev, NULL); + return ret; } -static int GetCgroupPrio(void *cgrpPath, int isIngress) +static int GetCgroupPrio(void *cgrpPath) { int ret; @@ -1056,7 +992,7 @@ static int GetCgroupPrio(void *cgrpPath, int isIngress) return CgrpV2PrioGet(cgrpPath); } -static int SetCgrpPrio(void *cgrpPath, void *args, int isIngress) +static int SetCgrpPrio(void *cgrpPath, void *args) { int ret; long long prio, tmp; @@ -1082,48 +1018,47 @@ static int SetCgrpPrio(void *cgrpPath, void *args, int isIngress) return CgrpV2PrioSet(cgrpPath, (int)prio); } -static int ReadCfgByPath(char *path, struct edt_throttle_cfg *cfg) +static int ReadStats(struct edt_throttle *stats) { int ret; int mapIndex = 0; int fd; - fd = bpf_obj_get(path); + fd = bpf_obj_get(THROTTLE_MAP_PATH); if (fd < 0) { - BWM_LOG_ERR("ERROR: ReadCfg bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", - path, fd, errno); + BWM_LOG_ERR("ERROR: ReadStats bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", + THROTTLE_MAP_PATH, fd, errno); return EXIT_FAIL_BPF; } - - ret = bpf_map_lookup_elem(fd, &mapIndex, cfg); + + ret = bpf_map_lookup_elem(fd, &mapIndex, stats); if (ret != 0) { - BWM_LOG_ERR("ERROR: ReadCfg can't find map. %s ret:%d errno:%d\n", - path, ret, errno); + BWM_LOG_ERR("ERROR: ReadStats can't find map. %s ret:%d errno:%d\n", + THROTTLE_MAP_PATH, ret, errno); (void)close(fd); return EXIT_FAIL_BPF; } - (void)close(fd); return EXIT_OK; } -static int UpdateCfgByPath(char *path, struct edt_throttle_cfg *cfg) +static int ReadCfg(struct edt_throttle_cfg *cfg) { int ret; int mapIndex = 0; int fd; - fd = bpf_obj_get(path); + fd = bpf_obj_get(THROTTLE_CFG_PATH); if (fd < 0) { - BWM_LOG_ERR("ERROR: UpdateCfg bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", - path, fd, errno); + BWM_LOG_ERR("ERROR: ReadCfg bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", + THROTTLE_CFG_PATH, fd, errno); return EXIT_FAIL_BPF; } - - ret = bpf_map_update_elem(fd, &mapIndex, cfg, BPF_ANY); + + ret = bpf_map_lookup_elem(fd, &mapIndex, cfg); if (ret != 0) { - BWM_LOG_ERR("ERROR: UpdateCfg can't update map. %s ret:%d errno:%d\n", - path, ret, errno); + BWM_LOG_ERR("ERROR: ReadCfg can't find map. %s ret:%d errno:%d\n", + THROTTLE_CFG_PATH, ret, errno); (void)close(fd); return EXIT_FAIL_BPF; } @@ -1132,76 +1067,36 @@ static int UpdateCfgByPath(char *path, struct edt_throttle_cfg *cfg) return EXIT_OK; } -static int ReadStatsByPath(char *path, struct edt_throttle *stats) +static int UpdateCfg(const struct edt_throttle_cfg *cfg) { int ret; int mapIndex = 0; int fd; - fd = bpf_obj_get(path); + fd = bpf_obj_get(THROTTLE_CFG_PATH); if (fd < 0) { - BWM_LOG_ERR("ERROR: ReadStats bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", - path, fd, errno); + BWM_LOG_ERR("ERROR: UpdateCfg bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", + THROTTLE_CFG_PATH, fd, errno); return EXIT_FAIL_BPF; } - - ret = bpf_map_lookup_elem(fd, &mapIndex, stats); + + ret = bpf_map_update_elem(fd, &mapIndex, cfg, BPF_ANY); if (ret != 0) { - BWM_LOG_ERR("ERROR: ReadStats can't find map. %s ret:%d errno:%d\n", - path, ret, errno); + BWM_LOG_ERR("ERROR: UpdateCfg update map fail. %s ret:%d errno:%d\n", + THROTTLE_CFG_PATH, ret, errno); (void)close(fd); return EXIT_FAIL_BPF; } - (void)close(fd); return EXIT_OK; } -static int ReadStats(struct edt_throttle *stats, int isIngress) -{ - int ret; - - if (isIngress) { - ret = ReadStatsByPath(THROTTLE_I_MAP_PATH, stats); - } else { - ret = ReadStatsByPath(THROTTLE_MAP_PATH, stats); - } - - return ret; -} - -static int ReadCfg(struct edt_throttle_cfg *cfg, int isIngress) -{ - int ret; - - if (isIngress) { - ret = ReadCfgByPath(THROTTLE_I_CFG_PATH, cfg); - } else { - ret = ReadCfgByPath(THROTTLE_CFG_PATH, cfg); - } - - return ret; -} - -static int UpdateCfg(struct edt_throttle_cfg *cfg, int isIngress) -{ - int ret; - - if (isIngress) { - ret = UpdateCfgByPath(THROTTLE_I_CFG_PATH, cfg); - } else { - ret = UpdateCfgByPath(THROTTLE_CFG_PATH, cfg); - } - - return ret; -} - -static bool InitCfgMap(int isIngress) +static bool InitCfgMap() { struct edt_throttle_cfg cfg = {0}; int ret; - ret = ReadCfg(&cfg, isIngress); + ret = ReadCfg(&cfg); if (ret != EXIT_OK) { BWM_LOG_ERR("InitCfgMap ReadCfg err: %d\n", ret); return false; @@ -1212,7 +1107,7 @@ static bool InitCfgMap(int isIngress) cfg.low_rate = DEFAULT_LOW_BANDWIDTH; cfg.high_rate = DEFAULT_HIGH_BANDWIDTH; - ret = UpdateCfg(&cfg, isIngress); + ret = UpdateCfg(&cfg); if (ret != EXIT_OK) { BWM_LOG_ERR("InitCfgMap UpdateCfg err: %d\n", ret); return false; @@ -1222,59 +1117,12 @@ static bool InitCfgMap(int isIngress) return true; } -static int UpdateIp(int isDelete) -{ - int ret; - int fd; - int priority = 1; - struct in_addr ip; - __u32 ipUint; - - char ipStr[IP_LEN + 1] = {0}; - - (void)strncpy(ipStr, optarg, IP_LEN); - if (ipStr[IP_LEN] != '\0') { - (void)fprintf(stderr, "invalid ip, too long: %s\n", optarg); - return EXIT_FAIL_OPTION; - } - - // ip char* -> u32, convert result is network byte order - ret = inet_pton(AF_INET, ipStr, &ip); - if (ret == 0 || errno == EAFNOSUPPORT) { - (void)fprintf(stderr, "invalid ip, transfer ip to u32 error\n"); - return EXIT_FAIL_OPTION; - } - - ipUint = ip.s_addr; // network byte order - - fd = bpf_obj_get(IPS_I_MAP_PATH); - if (fd < 0) { - BWM_LOG_ERR("ERROR: %s bpf_obj_get failed, Not enabled? %s fd:%d errno:%d\n", - isDelete ? "RemoveIp" : "AddIp", IPS_I_MAP_PATH, fd, errno); - return EXIT_FAIL_BPF; - } - - ret = isDelete ? bpf_map_delete_elem(fd, &ipUint) : bpf_map_update_elem(fd, &ipUint, &priority, BPF_ANY); - if (ret != 0) { - BWM_LOG_ERR("ERROR: %s map fail. %s ret:%d errno:%d\n", - isDelete ? "RemoveIp" : "AddIp", IPS_I_MAP_PATH, ret, errno); - (void)close(fd); - return EXIT_FAIL_BPF; - } - - if (!isDelete) - BWM_LOG_INFO("ip(network byte order): %u\n", ipUint); - BWM_LOG_INFO("%s %s success\n", isDelete ? "RemoveIp" : "AddIp", ipStr); - (void)close(fd); - return EXIT_OK; -} - -static int GetBandwidth(void *unused, int isIngress) +static int GetBandwidth(void *unused) { int ret; struct edt_throttle_cfg cfg = {0}; - ret = ReadCfg(&cfg, isIngress); + ret = ReadCfg(&cfg); if (ret != EXIT_OK) { return ret; } @@ -1283,15 +1131,14 @@ static int GetBandwidth(void *unused, int isIngress) (cfg.high_rate == 0) ? DEFAULT_HIGH_BANDWIDTH : cfg.high_rate); return EXIT_OK; } - -static int SetBandwidth(void *cgrpPath, void *args, int isIngress) +static int SetBandwidth(void *cgrpPath, void *args) { int ret; long long low, high; unsigned long long lowtemp, hightemp; struct edt_throttle_cfg cfg = {0}; - ret = ReadCfg(&cfg, isIngress); + ret = ReadCfg(&cfg); if (ret != EXIT_OK) { return ret; } @@ -1313,7 +1160,7 @@ static int SetBandwidth(void *cgrpPath, void *args, int isIngress) cfg.low_rate = lowtemp; cfg.high_rate = hightemp; - ret = UpdateCfg(&cfg, isIngress); + ret = UpdateCfg(&cfg); if (ret != EXIT_OK) { return ret; } @@ -1321,13 +1168,12 @@ static int SetBandwidth(void *cgrpPath, void *args, int isIngress) BWM_LOG_INFO("set bandwidth success\n"); return EXIT_OK; } - -static int GetWaterline(void *unused, int isIngress) +static int GetWaterline(void *unused) { int ret; struct edt_throttle_cfg cfg = {0}; - ret = ReadCfg(&cfg, isIngress); + ret = ReadCfg(&cfg); if (ret != EXIT_OK) { return ret; } @@ -1335,14 +1181,13 @@ static int GetWaterline(void *unused, int isIngress) BWM_LOG_INFO("waterline is %llu(B)\n", wl); return EXIT_OK; } - -static int SetWaterline(void *cgrpPath, void *args, int isIngress) +static int SetWaterline(void *cgrpPath, void *args) { int ret; long long val, tmp; struct edt_throttle_cfg cfg = {0}; - ret = ReadCfg(&cfg, isIngress); + ret = ReadCfg(&cfg); if (ret != EXIT_OK) { return ret; } @@ -1362,7 +1207,7 @@ static int SetWaterline(void *cgrpPath, void *args, int isIngress) if ((unsigned long long)val != cfg.water_line) { cfg.water_line = (unsigned long long)val; - ret = UpdateCfg(&cfg, isIngress); + ret = UpdateCfg(&cfg); if (ret != EXIT_OK) { return ret; } @@ -1370,13 +1215,12 @@ static int SetWaterline(void *cgrpPath, void *args, int isIngress) BWM_LOG_INFO("set waterline success\n"); return EXIT_OK; } - -static int GetStats(void *unused, int isIngress) +static int GetStats(void *unused) { int ret; struct edt_throttle stats = {0}; - ret = ReadStats(&stats, isIngress); + ret = ReadStats(&stats); if (ret != EXIT_OK) { return ret; } @@ -1386,9 +1230,9 @@ static int GetStats(void *unused, int isIngress) return EXIT_OK; } -static int GetDevs(void *unused, int isIngress) +static int GetDevs(void *unused) { - PrintDevsStats(isIngress); + PrintDevsStats(); return EXIT_OK; } @@ -1396,18 +1240,17 @@ int main(int argc, char **argv) { int hasOptions = 0; int ret; - int opt, isSet, enable, isIngress; + int opt, isSet, enable; int longindex = 0; if (argc < MINUM_ARGS) { Usage(argv); return EXIT_FAIL_OPTION; } - while ((opt = getopt_long(argc, argv, "vVhe::E::d::D::p:P:s:S:A:R:", g_longOptions, &longindex)) != -1) { + while ((opt = getopt_long(argc, argv, "vVhe::d::p:s:", g_longOptions, &longindex)) != -1) { hasOptions = 1; isSet = 1; enable = 1; - isIngress = 0; // 0 -> egress; !0 -> ingress ret = EXIT_FAIL_OPTION; switch (opt) { @@ -1419,48 +1262,16 @@ int main(int argc, char **argv) isSet = 0; //lint -fallthrough case 's': - ret = CfgsInfo(argc, argv, isSet, isIngress); + ret = CfgsInfo(argc, argv, isSet); if (ret != EXIT_OK) { return ret; } break; - case 'P': - isSet = 0; - //lint -fallthrough - case 'S': - isIngress = 1; - ret = CfgsInfo(argc, argv, isSet, isIngress); - if (ret != EXIT_OK) { - return ret; - } - break; - case 'd': // egress + case 'd': enable = 0; //lint -fallthrough case 'e': - ret = ChangeNetdeviceStatus(argc, argv, enable, isIngress); - if (ret != EXIT_OK) { - return ret; - } - break; - case 'D': // ingress - enable = 0; - //lint -fallthrough - case 'E': - isIngress = 1; - ret = ChangeNetdeviceStatus(argc, argv, enable, isIngress); - if (ret != EXIT_OK) { - return ret; - } - break; - case 'A': - ret = UpdateIp(0); - if (ret != EXIT_OK) { - return ret; - } - break; - case 'R': - ret = UpdateIp(1); + ret = ChangeNetdeviceStatus(argc, argv, enable); if (ret != EXIT_OK) { return ret; } diff --git a/bwmcli.h b/bwmcli.h index f64d66a..f7d4c62 100644 --- a/bwmcli.h +++ b/bwmcli.h @@ -13,13 +13,11 @@ #define EXIT_FAIL_MEM 4 #define PRIO_LEN 32 -#define IP_LEN 16 // ipv4 #define ARG_LEN 16 #define BPF_PROG_PATH "/usr/share/bwmcli/" #define CGRP_PRIO_PROG BPF_PROG_PATH"bwm_prio_kern.o" #define TC_PROG BPF_PROG_PATH"bwm_tc.o" -#define TC_PROG_I BPF_PROG_PATH"bwm_tc_i.o" #define MAX_PROG_CNT 10 #define MAX_CMD_LEN (256 + NAME_MAX) @@ -43,11 +41,11 @@ enum { GB_UNIT = GB, }; -typedef int NetdevCallback(const char *ethdev, const void *arg, int isIngress); +typedef int NetdevCallback(const char *ethdev, const void *arg); struct CfgOperations { - int (*setCfg)(void *, void *, int); - int (*getCfg)(void *, int); + int (*setCfg)(void *, void *); + int (*getCfg)(void *); }; struct CfgOption { diff --git a/tools/README.md b/tools/README.md index d4913af..52d18fb 100644 --- a/tools/README.md +++ b/tools/README.md @@ -33,13 +33,3 @@ monitor_interval 输出带宽的最小时间间隔(ms) high_threshold 离线流量低于此门限即打印[limiting] bandwidth_low, bandwidth_high 离线流量在此范围即打印[offline_exists] - -# 入向脚本 -./bwm_monitor_ingress.bt为入向流量脚本,其用法与前述用法一致。 - -运行入向脚本,需要首先安装linux header -yum install -y kernel-devel-$(uname -r) - -目前需要通过手动匹配的ip的方式进行离线流量标识,即在脚本130行通过判断条件对数据包IP与目标IP进行比对。 -(注意比较值使用的是网络字节序的IP,bwmcli工具在添加IP的时候会打印IP对应的网络字节序值)。 -完成值修改后,通过`install -Dpm 0500 tools/bwm_monitor_ingress.bt /usr/bin`重新安装脚本。 \ No newline at end of file diff --git a/tools/bwm_monitor_ingress.bt b/tools/bwm_monitor_ingress.bt deleted file mode 100755 index 8bbda23..0000000 --- a/tools/bwm_monitor_ingress.bt +++ /dev/null @@ -1,155 +0,0 @@ -#!/usr/bin/env bpftrace - -#include -#include -#include - -BEGIN -{ - @start = nsecs; - @start_monitor = nsecs; - @rate_online = (uint64)0; - @rate_offline = (uint64)0; - @online_bytes = (uint64)0; - @offline_bytes = (uint64)0; - @avg_cnt = (uint64)0; - - /* if qos is enabled on a virtual net device, then - * @net_dev should be set. but we suggest that only - * enabling physical network card to use this tool. - */ - //@net_dev = "virbr0"; - @net_dev = ""; - - /* adjustable parameters */ - @sample_interval = (uint64)10; - @monitor_interval = (uint64)25; - // The offline bandwidth is lower than high_threshold, indicating that the limit is successful. - // Default value is 5% error from the bandwidth_low - @high_threshold = (uint64)21971520; // 20mB * 1.05 - @bandwidth_low = (uint64)20971520; // 20mB - @bandwidth_high = (uint64)1073741824; // 1GB - if ($1 > 0) { - @bandwidth_low = (uint64)($1); - @high_threshold = (uint64)(@bandwidth_low + @bandwidth_low/20); - } - if ($2 > 0) { - @bandwidth_high = (uint64)($2); - } - if ($3 > 10) { // 10ms is the minimum sampling interval - @sample_interval = (uint64)($3); - } - if ($4 > 0) { - if ($4 < @sample_interval) { - @monitor_interval = @sample_interval; - } else { - @monitor_interval = (uint64)($4); - } - } -} - -tracepoint:net:net_dev_start_xmit -{ - $skb = ((struct sk_buff *)args->skbaddr); - - $data_end = $skb->end; - $data = $skb->data; - - $l2 = (struct ethhdr *)($data); - /* : Segmentation fault - * if (($l2 + 1) > $data_end) { - * return; - *} - */ - $l3 = (struct iphdr *)($l2+1); - /* - * if (($l3 + 1) > $data_end) { - * return; - * } - */ - $ip = $l3->daddr; - - // printf("dip: %u\n", $ip); - - if (@net_dev != "" && strncmp($skb->dev->name, @net_dev, 16) != 0) { - /* For the virtual network card, in order to make accurate - * statistics, we only count the traffic on the virtual - * network card - */ - - /* at bpftrace higher version, we can use "return" to - * Optimization branch. Now, we do nothing here. - */ - - } - else { - - $interval = (nsecs - @start)/1000000; - - if ($interval >= @sample_interval) { - @rate_online = @online_bytes*(1000/$interval); - @rate_offline = @offline_bytes*(1000/$interval); - - @avg_online = @avg_online + @rate_online; - @avg_offline = @avg_offline + @rate_offline; - - @avg_cnt = @avg_cnt + 1; - - /* reinit counters */ - @online_bytes = 0; - @offline_bytes = 0; - @start = nsecs; - } - - $interval = (nsecs - @start_monitor)/1000000; - - if ($interval >= @monitor_interval) { - /* offline bandwidth has reached the specific threshold */ - if (@avg_offline/@avg_cnt >= @bandwidth_low && - @avg_offline/@avg_cnt <= @bandwidth_high) - { - printf("[offline_exists]"); - } - if ((@high_threshold != 0 && - @avg_offline/@avg_cnt <= @high_threshold)) - { - printf("[limiting]"); - } - - printf("online/offline{%d}(ms): %u/%u(Bytes)\n", - $interval, - @avg_online/@avg_cnt, - @avg_offline/@avg_cnt); - @avg_online = 0; - @avg_offline = 0; - @start_monitor = nsecs; - @avg_cnt = 0; - } - - /* 172.17.0.2 33558956 ; 172.17.0.3 50336172*/ - if ($ip == 33558956 && $ip == 50336172) { - @offline_bytes = @offline_bytes + $skb->len; - } else { - @online_bytes = @online_bytes + $skb->len; - } - } -} - -END -{ - clear(@start); - clear(@start_monitor); - clear(@sample_interval); - clear(@monitor_interval); - clear(@online_bytes); - clear(@offline_bytes); - clear(@avg_online); - clear(@avg_offline); - clear(@rate_online); - clear(@rate_offline); - clear(@avg_cnt); - clear(@net_dev); - clear(@high_threshold); - clear(@bandwidth_low); - clear(@bandwidth_high); -} -- Gitee