From 6d78eeaa8f4b8aca1d058ba52b6305fe186372cc Mon Sep 17 00:00:00 2001 From: liangbotong Date: Fri, 23 Dec 2022 11:55:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E9=99=A4=20newip=20=E5=86=85=E6=A0=B8?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liangbotong --- examples/check_nip_enable.c | 8 +- examples/get_af_ninet.c | 4 +- examples/nip.h | 4 +- examples/nip_addr.c | 84 ++++---- examples/nip_addr_cfg_demo.c | 24 +-- examples/nip_lib.c | 6 +- examples/nip_lib.h | 9 +- examples/nip_route.c | 129 ++++++++----- examples/nip_route_cfg_demo.c | 14 +- examples/nip_tcp_client_demo.c | 100 ++++++---- examples/nip_tcp_server_demo.c | 2 +- examples/nip_uapi.h | 2 +- examples/nip_udp_client_demo.c | 98 ++++++---- examples/nip_udp_server_demo.c | 4 +- src/common/nip_addr.h | 2 +- src/common/nip_checksum.c | 3 +- src/linux/include/linux/nip.h | 2 +- src/linux/include/net/if_ninet.h | 6 +- src/linux/include/net/nip_addrconf.h | 6 +- src/linux/include/net/nip_fib.h | 2 +- src/linux/include/net/nip_route.h | 2 +- src/linux/include/net/nndisc.h | 4 +- src/linux/include/net/tcp_nip.h | 2 +- src/linux/include/uapi/linux/nip.h | 6 +- src/linux/net/newip/af_ninet.c | 2 +- src/linux/net/newip/ninet_hashtables.c | 63 +++--- src/linux/net/newip/nip_addrconf.c | 51 +++-- src/linux/net/newip/nip_fib.c | 4 +- src/linux/net/newip/nip_sockglue.c | 3 +- src/linux/net/newip/nndisc.c | 188 ++++++++---------- src/linux/net/newip/route.c | 41 ++-- src/linux/net/newip/tcp_nip.c | 192 ++++++++++--------- src/linux/net/newip/tcp_nip_input.c | 245 ++++++++++++------------ src/linux/net/newip/tcp_nip_output.c | 145 +++++++------- src/linux/net/newip/tcp_nip_parameter.c | 176 ++++++++++++++++- src/linux/net/newip/tcp_nip_parameter.h | 111 ++++------- src/linux/net/newip/tcp_nip_timer.c | 6 +- src/linux/net/newip/udp.c | 83 +++----- 38 files changed, 989 insertions(+), 844 deletions(-) diff --git a/examples/check_nip_enable.c b/examples/check_nip_enable.c index 5a4210f..5aaedf0 100644 --- a/examples/check_nip_enable.c +++ b/examples/check_nip_enable.c @@ -46,12 +46,12 @@ void _check_nip_enable(void) FILE *fn = fopen(NIP_DISABLE_PATH, "r"); if (!fn) { - printf("fail to open %s.\n\n", NIP_DISABLE_PATH); + printf("fail to open %s\n\n", NIP_DISABLE_PATH); return; } if (fgets(tmp, NIP_DISABLE_LENTH, fn) == NULL) { - printf("fail to gets %s.\n\n", NIP_DISABLE_PATH); + printf("fail to gets %s\n\n", NIP_DISABLE_PATH); fclose(fn); return; } @@ -75,9 +75,9 @@ int main(int argc, char **argv) int af_ninet = check_nip_enable(); if (af_ninet) - printf("Support NewIP.\n\n"); + printf("Support NewIP\n\n"); else - printf("Not support NewIP.\n\n"); + printf("Not support NewIP\n\n"); return 0; } diff --git a/examples/get_af_ninet.c b/examples/get_af_ninet.c index 83a3330..2dba239 100644 --- a/examples/get_af_ninet.c +++ b/examples/get_af_ninet.c @@ -45,12 +45,12 @@ void _get_af_ninet(void) FILE *fn = fopen(AF_NINET_PATH, "r"); if (!fn) { - printf("fail to open %s.\n\n", AF_NINET_PATH); + printf("fail to open %s\n\n", AF_NINET_PATH); return; } if (fgets(tmp, AF_NINET_LENTH, fn) == NULL) { - printf("fail to gets %s.\n\n", AF_NINET_PATH); + printf("fail to gets %s\n\n", AF_NINET_PATH); fclose(fn); return; } diff --git a/examples/nip.h b/examples/nip.h index 06d5da9..f1cf685 100644 --- a/examples/nip.h +++ b/examples/nip.h @@ -99,7 +99,7 @@ struct nip_addr_field { }; struct nip_addr { - unsigned char bitlen; /* The address length is in bit (not byte) */ + unsigned char bitlen; /* The address length is in bit (not byte) */ struct nip_addr_field v; }; #pragma pack() @@ -124,4 +124,4 @@ enum nip_index { INDEX_MAX, }; -#endif /*_NIP_H*/ +#endif /*_NIP_H */ diff --git a/examples/nip_addr.c b/examples/nip_addr.c index 30eecae..6a40567 100644 --- a/examples/nip_addr.c +++ b/examples/nip_addr.c @@ -45,7 +45,7 @@ * ioctl(fd, SIOGIFINDEX, &ifr); * ifr.ifr_ifindex; ===> ifindex */ -int32_t nip_add_addr(int32_t ifindex, const struct nip_addr *addr, int opt) +int nip_add_addr(int ifindex, const struct nip_addr *addr, int opt) { int fd, ret; struct nip_ifreq ifrn; @@ -76,66 +76,78 @@ void cmd_help(void) printf("[cmd example] nip_addr { add | del } \n"); } -int main(int argc, char **argv_input) +int parse_name(char **argv, int *ifindex, char *dev) { - int ret; - int opt; - unsigned int len; - int ifindex = 0; - char **argv = argv_input; - char cmd[ARRAY_LEN]; - char dev[ARRAY_LEN]; - struct nip_addr addr = {0}; - - if (argc != DEMO_INPUT_3) { - printf("unsupport addr cfg input, argc=%d.\n", argc); - cmd_help(); - return -1; - } + int len = strlen(*argv); - /* 配置参数1解析: */ - argv++; memset(dev, 0, ARRAY_LEN); - - len = strlen(*argv); if (!len || len >= (ARRAY_LEN - 1)) return -1; memcpy(dev, *argv, len); dev[len + 1] = '\0'; - if (strncmp(dev, "wlan", NAME_WLAN_LEN)) { - printf("unsupport addr cfg cmd-1, cmd=%s.\n", dev); + if (strncmp(dev, NIC_NAME_CHECK, strlen(NIC_NAME_CHECK))) { + printf("unsupport addr cfg cmd-1, cmd=%s\n", dev); cmd_help(); return -1; } - ret = nip_get_ifindex(dev, &ifindex); - if (ret != 0) - return -1; + return nip_get_ifindex(dev, ifindex); +} - /* 配置参数2解析: { add | del } */ - argv++; - memset(cmd, 0, ARRAY_LEN); +int parse_cmd(char **argv, int *opt) +{ + char cmd[ARRAY_LEN]; + int len = strlen(*argv); - len = strlen(*argv); + memset(cmd, 0, ARRAY_LEN); if (!len || len >= (ARRAY_LEN - 1)) return -1; memcpy(cmd, *argv, len); cmd[len + 1] = '\0'; - if (!strncmp(cmd, "add", NAME_WLAN_LEN)) { - opt = SIOCSIFADDR; - } else if (!strncmp(cmd, "del", NAME_WLAN_LEN)) { - opt = SIOCDIFADDR; + if (!strncmp(cmd, CMD_ADD, strlen(CMD_ADD))) { + *opt = SIOCSIFADDR; + } else if (!strncmp(cmd, CMD_DEL, strlen(CMD_DEL))) { + *opt = SIOCDIFADDR; } else { - printf("unsupport addr cfg cmd-2, cmd=%s.\n", cmd); + printf("unsupport addr cfg cmd-2, cmd=%s\n", cmd); cmd_help(); return -1; } + return 0; +} + +int main(int argc, char **argv_input) +{ + char dev[ARRAY_LEN]; + int ret; + int opt; + int ifindex = 0; + char **argv = argv_input; + struct nip_addr addr = {0}; + + if (argc != DEMO_INPUT_3) { + printf("unsupport addr cfg input, argc=%d\n", argc); + cmd_help(); + return -1; + } + + /* 配置参数1解析: */ + argv++; + ret = parse_name(argv, &ifindex, dev); + if (ret != 0) + return -1; + + /* 配置参数2解析: { add | del } */ + argv++; + ret = parse_cmd(argv, &opt); + if (ret != 0) + return -1; /* 配置参数3解析: */ argv++; if (nip_get_addr(argv, &addr)) { - printf("unsupport addr cfg cmd-3.\n"); + printf("unsupport addr cfg cmd-3\n"); cmd_help(); return 1; } @@ -144,7 +156,7 @@ int main(int argc, char **argv_input) if (ret != 0) return -1; - printf("%s (ifindex=%d) cfg addr success.\n", dev, ifindex); + printf("%s (ifindex=%d) cfg addr success\n", dev, ifindex); return 0; } diff --git a/examples/nip_addr_cfg_demo.c b/examples/nip_addr_cfg_demo.c index 9839027..994c605 100644 --- a/examples/nip_addr_cfg_demo.c +++ b/examples/nip_addr_cfg_demo.c @@ -45,7 +45,7 @@ * ioctl(fd, SIOGIFINDEX, &ifr); * ifr.ifr_ifindex; ===> ifindex */ -int32_t nip_add_addr(int32_t ifindex, const unsigned char *addr, uint8_t addr_len) +int nip_add_addr(int ifindex, const unsigned char *addr, unsigned char addr_len) { int fd, ret; struct nip_ifreq ifrn; @@ -61,7 +61,7 @@ int32_t nip_add_addr(int32_t ifindex, const unsigned char *addr, uint8_t addr_le ret = ioctl(fd, SIOCSIFADDR, &ifrn); if (ret < 0 && errno != EEXIST) { // ignore File Exists error - printf("cfg newip addr fail, ifindex=%d, ret=%d.\n", ifindex, ret); + printf("cfg newip addr fail, ifindex=%d, ret=%d\n", ifindex, ret); close(fd); return -1; } @@ -75,29 +75,29 @@ int32_t nip_add_addr(int32_t ifindex, const unsigned char *addr, uint8_t addr_le */ int main(int argc, char **argv) { - int ret; int ifindex = 0; - uint8_t client_addr[INDEX_1] = {0x50}; // 1-byte address of the client: 0x50 - uint8_t server_addr[INDEX_2] = {0xDE, 0x00}; // 2-byte address of the server: 0xDE00 - uint8_t *addr; - uint8_t addr_len; + int ret; + unsigned char client_addr[INDEX_1] = {0x50}; // 1-byte address of the client: 0x50 + unsigned char server_addr[INDEX_2] = {0xDE, 0x00}; // 2-byte address of the server: 0xDE00 + unsigned char *addr; + unsigned char addr_len; if (argc == DEMO_INPUT_1) { if (!strcmp(*(argv + 1), "server")) { - printf("server cfg addr=0x%02x%02x.\n", + printf("server cfg addr=0x%02x%02x\n", server_addr[INDEX_0], server_addr[INDEX_1]); addr = server_addr; addr_len = sizeof(server_addr); } else if (!strcmp(*(argv + 1), "client")) { - printf("client cfg addr=0x%x02x.\n", client_addr[INDEX_0]); + printf("client cfg addr=0x%02x\n", client_addr[INDEX_0]); addr = client_addr; addr_len = sizeof(client_addr); } else { - printf("invalid addr cfg input.\n"); + printf("invalid addr cfg input\n"); return -1; } } else { - printf("unsupport addr cfg input.\n"); + printf("unsupport addr cfg input\n"); return -1; } @@ -109,7 +109,7 @@ int main(int argc, char **argv) if (ret != 0) return -1; - printf("%s %s(ifindex=%d) cfg addr success.\n", *argv, NIC_NAME, ifindex); + printf("%s %s(ifindex=%d) cfg addr success\n", *argv, NIC_NAME, ifindex); return 0; } diff --git a/examples/nip_lib.c b/examples/nip_lib.c index d414968..b597b92 100644 --- a/examples/nip_lib.c +++ b/examples/nip_lib.c @@ -87,7 +87,7 @@ int nip_addr_fmt(char *addr_str, struct nip_addr *sap, int addrlen_input) } else if (addr_str[i] >= 'a' && addr_str[i] <= 'f') { addr_str[i] = addr_str[i] - STR_FMT_2; } else { - printf("Newip addr error: uaddr[%d]=%c.\n", i, addr_str[i]); + printf("Newip addr error: uaddr[%d]=%c\n", i, addr_str[i]); return 1; } } @@ -110,7 +110,7 @@ int nip_addr_fmt(char *addr_str, struct nip_addr *sap, int addrlen_input) addrlen = 0; if (addrlen_input != addrlen) { - printf("Newip addr error, first_byte=0x%x.\n", first_byte); + printf("Newip addr error, first_byte=0x%x\n", first_byte); return 1; } @@ -140,7 +140,7 @@ int nip_get_addr(char **args, struct nip_addr *addr) } if (addrlen_input % ADDR_STR_LEN != 0) { - printf("NewIP addr str-len invalid, addrlen_input=%d.\n", addrlen_input); + printf("NewIP addr str-len invalid, addrlen_input=%d\n", addrlen_input); return -1; } diff --git a/examples/nip_lib.h b/examples/nip_lib.h index 43c64f8..6255008 100644 --- a/examples/nip_lib.h +++ b/examples/nip_lib.h @@ -40,9 +40,12 @@ #define DEMO_INPUT_4 5 /* Change the value based on the actual interface */ -#define NIC_NAME "wlan0" +#define NIC_NAME "wlan0" +#define NIC_NAME_CHECK "wlan" +#define CMD_ADD "add" +#define CMD_DEL "del" -#define BUFLEN 2048 +#define BUFLEN 1024 #define LISTEN_MAX 3 #define PKTCNT 10 /* Number of sent packets */ #define PKTLEN 1024 /* Length of sent packet */ @@ -51,8 +54,6 @@ #define TCP_SERVER_PORT 5556 /* TCP Server Port */ #define UDP_SERVER_PORT 9090 /* UDP Server Port */ -#define NAME_WLAN_LEN 4 -#define NAME_ETH_LEN 3 #define ARRAY_LEN 255 int nip_get_ifindex(const char *ifname, int *ifindex); diff --git a/examples/nip_route.c b/examples/nip_route.c index 2fc0ea7..4df8e47 100644 --- a/examples/nip_route.c +++ b/examples/nip_route.c @@ -33,8 +33,8 @@ #include #include #include -#include #include +#include #include #include "nip_uapi.h" @@ -48,7 +48,7 @@ * ifr.ifr_ifindex; ===> ifindex */ int nip_route_add(int ifindex, const struct nip_addr *dst_addr, - const struct nip_addr *gateway_addr, uint8_t gateway_flag, int opt) + const struct nip_addr *gateway_addr, __u8 gateway_flag, int opt) { int fd, ret; struct nip_rtmsg rt; @@ -79,105 +79,132 @@ int nip_route_add(int ifindex, const struct nip_addr *dst_addr, void cmd_help(void) { - /* nip_route add 02 wlan0 (配置目的地址02设备路由,出口是wlan0) - * nip_route add 02 wlan0 03 (配置目的地址02设备路由,出口是wlan0,网关地址是03) - * nip_route add ff09 wlan0 03 (配置广播默认路由, 出口是wlan0,网关地址是03) + /* nip_route add 02 wlan0 + * (配置目的地址02设备路由,出口是wlan0) + * nip_route add 02 wlan0 03 + * (配置目的地址02设备路由,出口是wlan0,网关地址是03) + * nip_route add ff09 wlan0 03 + * (配置广播默认路由, 出口是wlan0,网关地址是03) */ printf("\n[cmd example]\n"); printf("nip_route { add | del } \n"); printf("nip_route { add | del } \n"); } -int main(int argc, char **argv_input) +int parse_name(char **argv, int *ifindex, char *dev) { - int ret; - int opt; - unsigned int len; - int ifindex = 0; - uint8_t gateway_flag = 0; - char **argv = argv_input; - char cmd[ARRAY_LEN]; - char dev[ARRAY_LEN]; - struct nip_addr dst_addr = {0}; - struct nip_addr gateway_addr = {0}; + int len = strlen(*argv); - if (argc != DEMO_INPUT_3 && argc != DEMO_INPUT_4) { - printf("unsupport route cfg input, argc=%d.\n", argc); + memset(dev, 0, ARRAY_LEN); + if (!len || len >= (ARRAY_LEN - 1)) + return -1; + memcpy(dev, *argv, len); + dev[len + 1] = '\0'; + + if (strncmp(dev, NIC_NAME_CHECK, strlen(NIC_NAME_CHECK))) { + printf("unsupport addr cfg cmd-3, cmd=%s\n", dev); cmd_help(); return -1; } + return nip_get_ifindex(dev, ifindex); +} - /* 配置参数1解析: { add | del } */ - argv++; - memset(cmd, 0, ARRAY_LEN); +int parse_cmd(char **argv, int *opt) +{ + char cmd[ARRAY_LEN]; + int len = strlen(*argv); - len = strlen(*argv); + memset(cmd, 0, ARRAY_LEN); if (!len || len >= (ARRAY_LEN - 1)) return -1; memcpy(cmd, *argv, len); cmd[len + 1] = '\0'; - if (!strncmp(cmd, "add", NAME_WLAN_LEN)) { - opt = SIOCADDRT; - } else if (!strncmp(cmd, "del", NAME_WLAN_LEN)) { - opt = SIOCDELRT; + if (!strncmp(cmd, CMD_ADD, strlen(CMD_ADD))) { + *opt = SIOCADDRT; + } else if (!strncmp(cmd, CMD_DEL, strlen(CMD_DEL))) { + *opt = SIOCDELRT; } else { - printf("unsupport route cfg cmd-1, cmd=%s.\n", cmd); + printf("unsupport route cfg cmd-1, cmd=%s\n", cmd); cmd_help(); return -1; } + return 0; +} - /* 配置参数2解析: */ - argv++; - if (nip_get_addr(argv, &dst_addr)) { - printf("unsupport route cfg cmd-2.\n"); - cmd_help(); - return 1; - } +int parse_args(char **argv, int *opt, __u8 *gateway_flag, int *ifindex, + struct nip_addr *dst_addr, struct nip_addr *gateway_addr, char *dev, int argc) +{ + /* 配置参数1解析: { add | del } */ + int ret; - /* 配置参数3解析: */ argv++; - memset(dev, 0, ARRAY_LEN); - - len = strlen(*argv); - if (!len || len >= (ARRAY_LEN - 1)) + ret = parse_cmd(argv, opt); + if (ret != 0) return -1; - memcpy(dev, *argv, len); - dev[len + 1] = '\0'; - if (strncmp(dev, "wlan", NAME_WLAN_LEN)) { - printf("unsupport route cfg cmd-3, cmd=%s.\n", dev); + /* 配置参数2解析: */ + argv++; + if (nip_get_addr(argv, dst_addr)) { + printf("unsupport route cfg cmd-2\n"); cmd_help(); return -1; } - ret = nip_get_ifindex(dev, &ifindex); + + /* 配置参数3解析: */ + argv++; + ret = parse_name(argv, ifindex, dev); if (ret != 0) return -1; /* 配置参数4解析: */ if (argc == DEMO_INPUT_4) { argv++; - if (nip_get_addr(argv, &gateway_addr)) { - printf("unsupport route cfg cmd-4.\n"); + if (nip_get_addr(argv, gateway_addr)) { + printf("unsupport route cfg cmd-4\n"); cmd_help(); - return 1; + return -1; } - gateway_flag = 1; + *gateway_flag = 1; } + return ret; +} + +int main(int argc, char **argv_input) +{ + int ret; + int opt; + int ifindex = 0; + __u8 gateway_flag = 0; + char **argv = argv_input; + char dev[ARRAY_LEN]; + struct nip_addr dst_addr = {0}; + struct nip_addr gateway_addr = {0}; + + if (argc != DEMO_INPUT_3 && argc != DEMO_INPUT_4) { + printf("unsupport route cfg input, argc=%d\n", argc); + cmd_help(); + return -1; + } + + ret = parse_args(argv, &opt, &gateway_flag, &ifindex, + &dst_addr, &gateway_addr, dev, argc); + if (ret != 0) + return -1; ret = nip_get_ifindex(dev, &ifindex); if (ret != 0) { - printf("get %s ifindex fail, ret=%d.\n", dev, ret); + printf("get %s ifindex fail, ret=%d\n", dev, ret); return -1; } ret = nip_route_add(ifindex, &dst_addr, &gateway_addr, gateway_flag, opt); if (ret != 0) { - printf("get %s ifindex fail, ret=%d.\n", dev, ret); + printf("get %s ifindex fail, ret=%d\n", dev, ret); return -1; } - printf("%s (ifindex=%d) cfg route success.\n", dev, ifindex); + printf("%s (ifindex=%d) cfg route success\n", dev, ifindex); return 0; } diff --git a/examples/nip_route_cfg_demo.c b/examples/nip_route_cfg_demo.c index f17cdfb..a5e9769 100644 --- a/examples/nip_route_cfg_demo.c +++ b/examples/nip_route_cfg_demo.c @@ -90,36 +90,36 @@ int main(int argc, char **argv) if (argc == DEMO_INPUT_1) { if (!strcmp(*(argv + 1), "server")) { - printf("server cfg route, dst-addr=0x%x02.\n", client_addr[INDEX_0]); + printf("server cfg route, dst-addr=0x%02x\n", client_addr[INDEX_0]); dst_addr = client_addr; dst_addr_len = 1; } else if (!strcmp(*(argv + 1), "client")) { - printf("client cfg route, dst-addr=0x%02x%02x.\n", + printf("client cfg route, dst-addr=0x%02x%02x\n", server_addr[INDEX_0], server_addr[INDEX_1]); dst_addr = server_addr; dst_addr_len = 2; } else { - printf("invalid route cfg input.\n"); + printf("invalid route cfg input\n"); return -1; } } else { - printf("unsupport route cfg input.\n"); + printf("unsupport route cfg input\n"); return -1; } ret = nip_get_ifindex(NIC_NAME, &ifindex); if (ret != 0) { - printf("get %s ifindex fail, ret=%d.\n", NIC_NAME, ret); + printf("get %s ifindex fail, ret=%d\n", NIC_NAME, ret); return -1; } ret = nip_route_add(ifindex, dst_addr, dst_addr_len, NULL, 0); if (ret != 0) { - printf("get %s ifindex fail, ret=%d.\n", NIC_NAME, ret); + printf("get %s ifindex fail, ret=%d\n", NIC_NAME, ret); return -1; } - printf("%s %s(ifindex=%d) cfg route success.\n", *argv, NIC_NAME, ifindex); + printf("%s %s(ifindex=%d) cfg route success\n", *argv, NIC_NAME, ifindex); return 0; } diff --git a/examples/nip_tcp_client_demo.c b/examples/nip_tcp_client_demo.c index fac89e6..771d3c1 100644 --- a/examples/nip_tcp_client_demo.c +++ b/examples/nip_tcp_client_demo.c @@ -46,57 +46,73 @@ #include #include +int _send(int cfd, int pkt_num) +{ + char buf[BUFLEN] = {0}; + struct timeval sys_time; + + gettimeofday(&sys_time, NULL); + sprintf(buf, "%ld %6ld NIP_TCP # %6d", sys_time.tv_sec, sys_time.tv_usec, pkt_num); + + if (send(cfd, buf, PKTLEN, 0) < 0) { + perror("sendto"); + return -1; + } + + return 0; +} + +int _recv(int cfd, int pkt_num, int *success) +{ + char buf[BUFLEN] = {0}; + fd_set readfds; + int tmp; + struct timeval tv; + + FD_ZERO(&readfds); + FD_SET(cfd, &readfds); + tv.tv_sec = 2; + tv.tv_usec = 0; + if (select(cfd + 1, &readfds, NULL, NULL, &tv) < 0) { + perror("select"); + return -1; + } + + if (FD_ISSET(cfd, &readfds)) { + int ret; + int no = 0; + + ret = recv(cfd, buf, PKTLEN, MSG_WAITALL); + if (ret > 0) { + *success += 1; + ret = sscanf(buf, "%d %d NIP_TCP # %d", &tmp, &tmp, &no); + if (ret <= 0) { + perror("sscanf"); + return -1; + } + printf("Received --%s sock %d success:%6d/%6d/no=%6d\n", + buf, cfd, *success, pkt_num + 1, no); + } else { + printf("recv fail, ret=%d\n", ret); + return -1; + } + } + + return 0; +} + void *send_recv(void *args) { - char buf[BUFLEN]; - int cfd, ret; - int sendtime_sec, sendtime_usec; + int cfd = ((struct thread_args *)args)->cfd; int success = 0; - int count = 0; - int no = 0; - fd_set readfds; - struct timeval tv, stTime; - struct thread_args *th_args = (struct thread_args *) args; - cfd = th_args->cfd; for (int i = 0; i < PKTCNT; i++) { - memset(buf, 0, BUFLEN); - (void)gettimeofday(&stTime, NULL); - sprintf(buf, "%ld %6ld NIP_TCP # %6d", stTime.tv_sec, stTime.tv_usec, count); - if (send(cfd, buf, PKTLEN, 0) < 0) { - perror("send"); + if (_send(cfd, i) != 0) goto END; - } - FD_ZERO(&readfds); - FD_SET(cfd, &readfds); - tv.tv_sec = SELECT_TIME; - tv.tv_usec = 0; - if (select(cfd + 1, &readfds, NULL, NULL, &tv) < 0) { - perror("select"); + if (_recv(cfd, i, &success) != 0) goto END; - } - if (FD_ISSET(cfd, &readfds)) { - memset(buf, 0, BUFLEN); - ret = recv(cfd, buf, PKTLEN, MSG_WAITALL); - if (ret > 0) { - success += 1; - (void)gettimeofday(&stTime, NULL); - ret = sscanf(buf, "%d %d NIP_TCP # %d", - &sendtime_sec, &sendtime_usec, &no); - if (ret <= 0) { - perror("sscanf"); - goto END; - } - printf("Received --%s sock %d success:%6d/%6d/no=%6d\n", - buf, cfd, success, count + 1, no); - } else { - printf("recv fail, ret=%d\n", ret); - goto END; - } - } - count += 1; usleep(SLEEP_US); } diff --git a/examples/nip_tcp_server_demo.c b/examples/nip_tcp_server_demo.c index 77433c8..bcedbea 100644 --- a/examples/nip_tcp_server_demo.c +++ b/examples/nip_tcp_server_demo.c @@ -44,8 +44,8 @@ void *recv_send(void *args) { - char buf[BUFLEN] = {0}; int cfd, ret; + char buf[BUFLEN] = {0}; memcpy(&cfd, args, sizeof(int)); for (int i = 0; i < PKTCNT; i++) { diff --git a/examples/nip_uapi.h b/examples/nip_uapi.h index 150c107..31e9789 100644 --- a/examples/nip_uapi.h +++ b/examples/nip_uapi.h @@ -28,4 +28,4 @@ struct thread_args { struct sockaddr_nin si_server; }; -#endif /*_NIP_UAPI_H*/ +#endif /*_NIP_UAPI_H */ diff --git a/examples/nip_udp_client_demo.c b/examples/nip_udp_client_demo.c index 74f1634..c644562 100644 --- a/examples/nip_udp_client_demo.c +++ b/examples/nip_udp_client_demo.c @@ -44,58 +44,72 @@ #include "nip_lib.h" #include "newip_route.h" -void *send_recv(void *args) +int _sendto(int cfd, struct sockaddr_nin *si_server, int pkt_num) { - char buf[BUFLEN]; - int cfd, ret; + char buf[BUFLEN] = {0}; + struct timeval sys_time; + socklen_t slen = sizeof(struct sockaddr_nin); + + gettimeofday(&sys_time, NULL); + sprintf(buf, "%ld %6ld NIP_UDP # %6d", sys_time.tv_sec, sys_time.tv_usec, pkt_num); + + if (sendto(cfd, buf, BUFLEN, 0, (struct sockaddr *)si_server, slen) < 0) { + printf("client sendto fail, pkt_num=%d", pkt_num); + return -1; + } + + return 0; +} + +int _recvfrom(int cfd, struct sockaddr_nin *si_server, int pkt_num, int *success) +{ + char buf[BUFLEN] = {0}; fd_set readfds; - int success = 0; - int count = 0; - int no = 0; - int sendtime_sec, sendtime_usec; + int tmp; struct timeval tv; - struct timeval stTime; - struct thread_args *th_args = (struct thread_args *)args; - struct sockaddr_nin si_server = th_args->si_server; + socklen_t slen = sizeof(struct sockaddr_nin); + + FD_ZERO(&readfds); + FD_SET(cfd, &readfds); + tv.tv_sec = 2; + tv.tv_usec = 0; + if (select(cfd + 1, &readfds, NULL, NULL, &tv) < 0) { + printf("client select fail, pkt_num=%d", pkt_num); + return -1; + } - cfd = th_args->cfd; - while (count < PKTCNT) { - socklen_t slen = sizeof(si_server); + if (FD_ISSET(cfd, &readfds)) { + int ret; + int no = 0; - memset(buf, 0, BUFLEN); - gettimeofday(&stTime, NULL); - sprintf(buf, "%ld %6ld NIP_UDP # %6d", stTime.tv_sec, stTime.tv_usec, count); + ret = recvfrom(cfd, buf, BUFLEN, 0, (struct sockaddr *)si_server, &slen); + if (ret > 0) { + *success += 1; + ret = sscanf(buf, "%d %d NIP_UDP # %d", &tmp, &tmp, &no); + printf("Received --%s sock %d success:%6d/%6d/no=%6d\n", + buf, cfd, *success, pkt_num + 1, no); + } else { + printf("client recvfrom fail, ret=%d\n", ret); + return -1; + } + } - if (sendto(cfd, buf, BUFLEN, 0, (struct sockaddr *)&si_server, slen) < 0) { - perror("sendto"); + return 0; +} + +void *send_recv(void *args) +{ + int success = 0; + int cfd = ((struct thread_args *)args)->cfd; + struct sockaddr_nin si_server = ((struct thread_args *)args)->si_server; + + for (int i = 0; i < PKTCNT; i++) { + if (_sendto(cfd, &si_server, i) != 0) goto END; - } - FD_ZERO(&readfds); - FD_SET(cfd, &readfds); - tv.tv_sec = 2; - tv.tv_usec = 0; - if (select(cfd + 1, &readfds, NULL, NULL, &tv) < 0) { - perror("select"); + if (_recvfrom(cfd, &si_server, i, &success) != 0) goto END; - } - if (FD_ISSET(cfd, &readfds)) { - memset(buf, 0, BUFLEN); - ret = recvfrom(cfd, buf, BUFLEN, 0, (struct sockaddr *)&si_server, &slen); - if (ret > 0) { - success += 1; - (void)gettimeofday(&stTime, NULL); - ret = sscanf(buf, "%d %d NIP_UDP # %d", &sendtime_sec, - &sendtime_usec, &no); - printf("Received --%s sock %d success:%6d/%6d/no=%6d\n", - buf, cfd, success, count + 1, no); - } else { - printf("recv fail, ret=%d\n", ret); - goto END; - } - } - count += 1; usleep(SLEEP_US); } diff --git a/examples/nip_udp_server_demo.c b/examples/nip_udp_server_demo.c index f84af5c..12f514f 100644 --- a/examples/nip_udp_server_demo.c +++ b/examples/nip_udp_server_demo.c @@ -59,7 +59,7 @@ void *recv_send(void *args) memset(&si_remote, 0, sizeof(si_remote)); recv_num = recvfrom(fd, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, &slen); if (recv_num < 0) { - perror("recvfrom"); + printf("server recvfrom fail, ret=%d\n", ret); goto END; } else if (recv_num == 0) { /* no data */ ; @@ -69,7 +69,7 @@ void *recv_send(void *args) slen = sizeof(si_remote); ret = sendto(fd, buf, BUFLEN, 0, (struct sockaddr *)&si_remote, slen); if (ret < 0) { - perror("sendto"); + printf("server sendto fail, ret=%d\n", ret); goto END; } printf("Sending -- %s -- to 0x%0x:%d\n", buf, diff --git a/src/common/nip_addr.h b/src/common/nip_addr.h index 905ff82..d913672 100644 --- a/src/common/nip_addr.h +++ b/src/common/nip_addr.h @@ -99,7 +99,7 @@ struct nip_addr_field { }; struct nip_addr { - unsigned char bitlen; /* The address length is in bit (not byte) */ + unsigned char bitlen; /* The address length is in bit (not byte) */ struct nip_addr_field v; }; diff --git a/src/common/nip_checksum.c b/src/common/nip_checksum.c index 98cadf8..da9ab00 100644 --- a/src/common/nip_checksum.c +++ b/src/common/nip_checksum.c @@ -35,7 +35,8 @@ #define NIP_CHECKSUM_UINT8_PAYLOAD 8 unsigned int _nip_check_sum(const unsigned char *data, unsigned short data_len) { - unsigned int i = 0, sum = 0; + unsigned int i = 0; + unsigned int sum = 0; while (i + 1 < data_len) { sum += (data[i] << NIP_CHECKSUM_UINT8_PAYLOAD) + data[i + 1]; diff --git a/src/linux/include/linux/nip.h b/src/linux/include/linux/nip.h index 524b929..65e9d9b 100644 --- a/src/linux/include/linux/nip.h +++ b/src/linux/include/linux/nip.h @@ -13,7 +13,7 @@ #include #include -#define ETH_P_NEWIP 0xEADD /* NIP */ +#define ETH_P_NEWIP 0xEADD /* NIP */ /* struct sock_common __sk_common */ #define sk_nip_daddr __sk_common.nip_daddr diff --git a/src/linux/include/net/if_ninet.h b/src/linux/include/net/if_ninet.h index 38fff47..347099c 100644 --- a/src/linux/include/net/if_ninet.h +++ b/src/linux/include/net/if_ninet.h @@ -32,8 +32,8 @@ struct ninet_ifaddr { __u32 flags; - unsigned long cstamp; /* created timestamp */ - unsigned long tstamp; /* updated timestamp */ + unsigned long cstamp; /* created timestamp */ + unsigned long tstamp; /* updated timestamp */ struct ninet_dev *idev; struct nip_rt_info *rt; @@ -57,7 +57,7 @@ struct ninet_dev { struct neigh_parms *nd_parms; struct nip_devconf cnf; - unsigned long tstamp; /* newip InterfaceTable update timestamp */ + unsigned long tstamp; /* newip InterfaceTable update timestamp */ struct rcu_head rcu; }; diff --git a/src/linux/include/net/nip_addrconf.h b/src/linux/include/net/nip_addrconf.h index 664a4fe..8455272 100644 --- a/src/linux/include/net/nip_addrconf.h +++ b/src/linux/include/net/nip_addrconf.h @@ -15,10 +15,10 @@ #include #define ADDRCONF_NOTIFY_PRIORITY 0 -#define NIN_ADDR_HSIZE_SHIFT (4) -#define NIN_ADDR_HSIZE (1 << NIN_ADDR_HSIZE_SHIFT) +#define NIN_ADDR_HSIZE_SHIFT (4) +#define NIN_ADDR_HSIZE (1 << NIN_ADDR_HSIZE_SHIFT) -#define DST_HOST 0x0001 /* NIP */ +#define DST_HOST 0x0001 /* NIP */ int nip_addrconf_add_ifaddr(struct net *net, void __user *arg); int nip_addrconf_del_ifaddr(struct net *net, void __user *arg); diff --git a/src/linux/include/net/nip_fib.h b/src/linux/include/net/nip_fib.h index d653f56..f2ce0b4 100644 --- a/src/linux/include/net/nip_fib.h +++ b/src/linux/include/net/nip_fib.h @@ -32,7 +32,7 @@ struct nip_fib_config { int fc_ifindex; u32 fc_flags; u32 fc_protocol; - u32 fc_type; /* only 8 bits are used */ + u32 fc_type; /* only 8 bits are used */ struct nip_addr fc_dst; struct nip_addr fc_src; diff --git a/src/linux/include/net/nip_route.h b/src/linux/include/net/nip_route.h index 0cc11e1..77e2f7e 100644 --- a/src/linux/include/net/nip_route.h +++ b/src/linux/include/net/nip_route.h @@ -63,4 +63,4 @@ int nip_route_init(void); void nip_route_cleanup(void); -#endif /*_NET_NIP_ROUTE_H*/ +#endif /*_NET_NIP_ROUTE_H */ diff --git a/src/linux/include/net/nndisc.h b/src/linux/include/net/nndisc.h index 5da370b..d8c776f 100644 --- a/src/linux/include/net/nndisc.h +++ b/src/linux/include/net/nndisc.h @@ -18,8 +18,8 @@ #define NEWIP_NEIGH_BUCKET_MAX 8 extern struct neigh_table nnd_tbl; -#define NIP_ARP_NS 0x01 /* ARP request */ -#define NIP_ARP_NA 0x02 /* ARP response */ +#define NIP_ARP_NS 0x01 /* ARP request */ +#define NIP_ARP_NA 0x02 /* ARP response */ struct nnd_msg { struct nip_icmp_hdr icmph; diff --git a/src/linux/include/net/tcp_nip.h b/src/linux/include/net/tcp_nip.h index 6b53eaa..0291b03 100644 --- a/src/linux/include/net/tcp_nip.h +++ b/src/linux/include/net/tcp_nip.h @@ -200,4 +200,4 @@ void tcp_nip_release_cb(struct sock *sk); void tcp_nip_keepalive_enable(struct sock *sk); void tcp_nip_keepalive_disable(struct sock *sk); -#endif /* _NIP_TCP_H */ +#endif /* _NIP_TCP_H */ diff --git a/src/linux/include/uapi/linux/nip.h b/src/linux/include/uapi/linux/nip.h index 477b806..bada548 100644 --- a/src/linux/include/uapi/linux/nip.h +++ b/src/linux/include/uapi/linux/nip.h @@ -41,7 +41,7 @@ struct nip_devreq { } devreq; }; -#define nip_dev_addr devreq.addr /* nip address */ -#define nip_dev_flags devreq.flags /* net device flags */ +#define nip_dev_addr devreq.addr /* nip address */ +#define nip_dev_flags devreq.flags /* net device flags */ -#endif /*_UAPI_NEWIP_H*/ +#endif /*_UAPI_NEWIP_H */ diff --git a/src/linux/net/newip/af_ninet.c b/src/linux/net/newip/af_ninet.c index 7a6c9d7..e21e121 100644 --- a/src/linux/net/newip/af_ninet.c +++ b/src/linux/net/newip/af_ninet.c @@ -22,7 +22,7 @@ #include #include #include -#include /* for signal_pending() */ +#include /* for signal_pending() */ #include #include diff --git a/src/linux/net/newip/ninet_hashtables.c b/src/linux/net/newip/ninet_hashtables.c index a55d0d4..3a8309d 100644 --- a/src/linux/net/newip/ninet_hashtables.c +++ b/src/linux/net/newip/ninet_hashtables.c @@ -36,14 +36,14 @@ static __always_inline void net_secret_init(void) #ifdef CONFIG_INET static u32 seq_scale(u32 seq) { - /* As close as possible to RFC 793, which - * suggests using a 250 kHz clock. - * Further reading shows this assumes 2 Mb/s networks. - * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate. - * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but - * we also need to limit the resolution so that the u32 seq - * overlaps less than one time per MSL (2 minutes). - * Choosing a clock of 64 ns period is OK. (period of 274 s) + /* As close as possible to RFC 793, which + * suggests using a 250 kHz clock. + * Further reading shows this assumes 2 Mb/s networks. + * or 10 Mb/s Ethernet, a 1 MHz clock is appropriate. + * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but + * we also need to limit the resolution so that the u32 seq + * overlaps less than one time per MSL (2 minutes). + * Choosing a clock of 64 ns period is OK. (period of 274 s) */ return seq + (ktime_get_real_ns() >> 6); } @@ -135,13 +135,13 @@ static void ninet_hash2(struct inet_hashinfo *h, struct sock *sk) } /* Function - * Returns the hash value based on the passed argument + * Returns the hash value based on the passed argument * Parameter - * net: The namespace - * laddr: The destination address - * lport: Destination port - * faddr: Source address - * fport: Source port + * net: The namespace + * laddr: The destination address + * lport: Destination port + * faddr: Source address + * fport: Source port */ u32 ninet_ehashfn(const struct net *net, const struct nip_addr *laddr, const u16 lport, @@ -164,11 +164,11 @@ u32 ninet_ehashfn(const struct net *net, } /* Function - * The socket is put into the Listen hash in case the server finds - the socket in the second handshake + * The socket is put into the Listen hash in case the server finds + * the socket in the second handshake * Parameter - * sk: Transmission control block - * osk: old socket + * sk: Transmission control block + * osk: old socket */ int __ninet_hash(struct sock *sk, struct sock *osk) { @@ -257,17 +257,17 @@ unlock: } /* Function - * Find transport control blocks based on address and port in the ehash table. - * If found, three handshakes have been made and a connection has been established, - * and normal communication can proceed. + * Find transport control blocks based on address and port in the ehash table. + * If found, three handshakes have been made and a connection has been established, + * and normal communication can proceed. * Parameter - * net: The namespace - * hashinfo: A global scalar of type tcp_hashinfo that stores tcp_SOCK(including ESTABLISHED, - * listen, and bind) for various states of the current system. - * saddr: Source address - * sport: Source port - * daddr: The destination address - * hnum: Destination port + * net: The namespace + * hashinfo: A global scalar of type tcp_hashinfo that stores tcp_SOCK(including ESTABLISHED, + * listen, and bind) for various states of the current system. + * saddr: Source address + * sport: Source port + * daddr: The destination address + * hnum: Destination port */ struct sock *__ninet_lookup_established(struct net *net, struct inet_hashinfo *hashinfo, @@ -346,8 +346,11 @@ static struct sock *ninet_lhash2_lookup(struct net *net, const int dif, const int sdif) { struct inet_connection_sock *icsk; - struct sock *sk, *result = NULL; - int hiscore = 0, matches = 0, reuseport = 0; + struct sock *sk; + struct sock *result = NULL; + int hiscore = 0; + int matches = 0; + int reuseport = 0; u32 phash = 0; inet_lhash2_for_each_icsk_rcu(icsk, &ilb2->head) { diff --git a/src/linux/net/newip/nip_addrconf.c b/src/linux/net/newip/nip_addrconf.c index f163ec7..079fa6d 100644 --- a/src/linux/net/newip/nip_addrconf.c +++ b/src/linux/net/newip/nip_addrconf.c @@ -265,7 +265,7 @@ static int ninet_addr_add(struct net *net, int ifindex, if (!dev) return -ENODEV; - idev = nip_addrconf_add_dev(dev); /* 挂接dev和idev */ + idev = nip_addrconf_add_dev(dev); if (IS_ERR(idev)) return PTR_ERR(idev); @@ -377,31 +377,40 @@ static int ninet_addr_del(struct net *net, int ifindex, u32 ifa_flags, return -EADDRNOTAVAIL; } -int nip_addrconf_add_ifaddr(struct net *net, void __user *arg) +int nip_addrconf_ifaddr_check(struct net *net, void __user *arg, struct nip_ifreq *ireq) { - struct nip_ifreq ireq; - - int err; - if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) { nip_dbg("%s: not admin can`t cfg", __func__); return -EPERM; } - if (copy_from_user(&ireq, arg, sizeof(struct nip_ifreq))) { + if (copy_from_user(ireq, arg, sizeof(struct nip_ifreq))) { nip_dbg("%s: fail to copy cfg data", __func__); return -EFAULT; } - if (nip_addr_invalid(&ireq.ifrn_addr)) { - nip_dbg("%s: nip addr invalid, bitlen=%u", __func__, ireq.ifrn_addr.bitlen); + if (nip_addr_invalid(&ireq->ifrn_addr)) { + nip_dbg("%s: nip addr invalid, bitlen=%u", __func__, ireq->ifrn_addr.bitlen); return -EFAULT; } - if (nip_addr_public(&ireq.ifrn_addr)) { + if (nip_addr_public(&ireq->ifrn_addr)) { nip_dbg("%s: The public address cannot be configured", __func__); return -EFAULT; } + return 0; +} + +int nip_addrconf_add_ifaddr(struct net *net, void __user *arg) +{ + struct nip_ifreq ireq; + int err; + + err = nip_addrconf_ifaddr_check(net, arg, &ireq); + if (err < 0) { + nip_dbg("%s: The ifaddr check failed", __func__); + return err; + } rtnl_lock(); err = ninet_addr_add(net, ireq.ifrn_ifindex, &ireq.ifrn_addr, @@ -416,24 +425,10 @@ int nip_addrconf_del_ifaddr(struct net *net, void __user *arg) struct nip_ifreq ireq; int err; - if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) { - nip_dbg("%s: not admin can`t cfg", __func__); - return -EPERM; - } - - if (copy_from_user(&ireq, arg, sizeof(struct nip_ifreq))) { - nip_dbg("%s: fail to copy cfg data", __func__); - return -EFAULT; - } - - if (nip_addr_invalid(&ireq.ifrn_addr)) { - nip_dbg("%s: nip addr invalid, bitlen=%u", __func__, ireq.ifrn_addr.bitlen); - return -EFAULT; - } - - if (nip_addr_public(&ireq.ifrn_addr)) { - nip_dbg("%s: Public addresses cannot be deleted", __func__); - return -EFAULT; + err = nip_addrconf_ifaddr_check(net, arg, &ireq); + if (err < 0) { + nip_dbg("%s: The ifaddr check failed", __func__); + return err; } rtnl_lock(); diff --git a/src/linux/net/newip/nip_fib.c b/src/linux/net/newip/nip_fib.c index b21dadb..262c5bb 100644 --- a/src/linux/net/newip/nip_fib.c +++ b/src/linux/net/newip/nip_fib.c @@ -162,7 +162,9 @@ int nip_fib_del(struct nip_rt_info *rt, struct nl_info *info) hlist_del_init_rcu(&fn->fib_hlist); - /* 当fib_node释放后,fib_node指向的route_info才可释放 */ + /* route_info directed by the fib_node can be released + * only after the fib_node is released + */ RCU_INIT_POINTER(rt->rt_node, NULL); call_rcu(&fn->rcu, nip_fib_destroy_rcu); diff --git a/src/linux/net/newip/nip_sockglue.c b/src/linux/net/newip/nip_sockglue.c index d33ac0b..c24b305 100644 --- a/src/linux/net/newip/nip_sockglue.c +++ b/src/linux/net/newip/nip_sockglue.c @@ -54,7 +54,8 @@ static int do_nip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, unsigned int optlen) { struct inet_sock *inet = inet_sk(sk); - int val = 0, err = 0; + int val = 0; + int err = 0; bool needs_rtnl = nip_setsockopt_needs_rtnl(optname); if (optlen >= sizeof(int)) { diff --git a/src/linux/net/newip/nndisc.c b/src/linux/net/newip/nndisc.c index 9dbd62e..c0ffe3e 100644 --- a/src/linux/net/newip/nndisc.c +++ b/src/linux/net/newip/nndisc.c @@ -265,77 +265,97 @@ static int get_ns_payload_len(const struct nip_addr *solicit) return sizeof(struct nip_icmp_hdr) + get_nip_addr_len(solicit); } -static void nndisc_send_ns(struct net_device *dev, - const struct nip_addr *solicit, - const struct nip_addr *daddr, - const struct nip_addr *saddr) +static int nndisc_send_skb(struct net_device *dev, + struct sk_buff *skb, struct nip_hdr_encap *head, + const int payload_len) { - int ret; - struct sk_buff *skb; - struct dst_entry *dst; - struct net *net; + int ret = 0; struct sock *sk = NULL; - int payload_len = get_ns_payload_len(solicit); - int len = NIP_ETH_HDR_LEN + NIP_HDR_MAX + payload_len; - struct nip_hdr_encap head = {0}; - unsigned short checksum; + struct dst_entry *dst = NULL; + u_short checksum = 0; - head.saddr = *saddr; - head.daddr = *daddr; - head.ttl = NIP_ARP_DEFAULT_TTL; - head.nexthdr = IPPROTO_NIP_ICMP; + /* skip transport hdr */ + skb_reserve(skb, payload_len); + + /* set skb->data to point network header */ + skb->data = skb_network_header(skb); + skb->len = head->hdr_buf_pos + payload_len; + + dst = nndisc_dst_alloc(dev); + if (!dst) { + kfree_skb(skb); + return -ENOMEM; + } + /* add check sum */ + checksum = nip_get_nndisc_send_checksum(skb, head, payload_len); + nip_insert_nndisc_send_checksum(skb, checksum); + + skb_dst_set(skb, dst); + ret = dst_output(dev_net(skb->dev), sk, skb); + return ret; +} + +static struct sk_buff *nndisc_alloc_skb(struct net_device *dev, + struct nip_hdr_encap *head, int payload_len) +{ + struct sk_buff *skb = NULL; + int len = NIP_ETH_HDR_LEN + NIP_HDR_MAX + payload_len; skb = alloc_skb(len, 0); if (!skb) /* If you add log here, there will be an alarm: * WARNING: Possible unnecessary 'out of memory' message */ - return; + return skb; skb->protocol = htons(ETH_P_NEWIP); - skb->dev = dev; skb->ip_summed = CHECKSUM_NONE; skb->csum = 0; + skb->dev = dev; memset(NIPCB(skb), 0, sizeof(struct ninet_skb_parm)); - NIPCB(skb)->dstaddr = head.daddr; - NIPCB(skb)->srcaddr = head.saddr; - NIPCB(skb)->nexthdr = head.nexthdr; - + NIPCB(skb)->dstaddr = head->daddr; + NIPCB(skb)->srcaddr = head->saddr; + NIPCB(skb)->nexthdr = head->nexthdr; /* reserve space for hardware header */ skb_reserve(skb, NIP_ETH_HDR_LEN); skb_reset_network_header(skb); /* build nwk header */ - head.hdr_buf = (unsigned char *)skb->data; - nip_hdr_comm_encap(&head); - head.total_len = head.hdr_buf_pos + payload_len; - nip_update_total_len(&head, htons(head.total_len)); - skb_reserve(skb, head.hdr_buf_pos); + head->hdr_buf = (unsigned char *)skb->data; + nip_hdr_comm_encap(head); + head->total_len = head->hdr_buf_pos + payload_len; + nip_update_total_len(head, htons(head->total_len)); + skb_reserve(skb, head->hdr_buf_pos); skb_reset_transport_header(skb); + return skb; +} - /* build transport header */ - nndisc_payload_ns_pack(solicit, skb); - skb_reserve(skb, payload_len); +static void nndisc_send_ns(struct net_device *dev, + const struct nip_addr *solicit, + const struct nip_addr *daddr, + const struct nip_addr *saddr) +{ + int ret; + struct sk_buff *skb; + int payload_len = get_ns_payload_len(solicit); + struct nip_hdr_encap head = {0}; - skb->data = skb_network_header(skb); - skb->len = head.hdr_buf_pos + payload_len; + head.saddr = *saddr; + head.daddr = *daddr; + head.ttl = NIP_ARP_DEFAULT_TTL; + head.nexthdr = IPPROTO_NIP_ICMP; - dst = nndisc_dst_alloc(dev); - if (!dst) { - kfree_skb(skb); + skb = nndisc_alloc_skb(dev, &head, payload_len); + if (!skb) + /* If you add log here, there will be an alarm: + * WARNING: Possible unnecessary 'out of memory' message + */ return; - } - - /* add check sum*/ - checksum = nip_get_nndisc_send_checksum(skb, &head, payload_len); - nip_insert_nndisc_send_checksum(skb, checksum); - - skb_dst_set(skb, dst); - net = dev_net(skb->dev); + /* build ns header */ + nndisc_payload_ns_pack(solicit, skb); - /* DST is set to SKB, and output is used to release SKB regardless of success or failure */ - ret = dst_output(net, sk, skb); + ret = nndisc_send_skb(dev, skb, &head, payload_len); if (ret) nip_dbg("%s: dst output fail", __func__); } @@ -399,71 +419,25 @@ static void nndisc_send_na(struct net_device *dev, { int ret; struct sk_buff *skb = NULL; - struct dst_entry *dst = NULL; - struct sock *sk = NULL; - int csummode = CHECKSUM_NONE; int payload_len = get_na_payload_len(dev); - int len = NIP_ETH_HDR_LEN + NIP_HDR_MAX + payload_len; u_char *smac = dev->dev_addr; struct nip_hdr_encap head = {0}; - u_short checksum = 0; head.saddr = *saddr; head.daddr = *daddr; head.ttl = NIP_ARP_DEFAULT_TTL; head.nexthdr = IPPROTO_NIP_ICMP; - skb = alloc_skb(len, 0); + skb = nndisc_alloc_skb(dev, &head, payload_len); if (!skb) /* If you add log here, there will be an alarm: * WARNING: Possible unnecessary 'out of memory' message */ return; - - skb->protocol = htons(ETH_P_NEWIP); - skb->ip_summed = csummode; - skb->csum = 0; - skb->dev = dev; - memset(NIPCB(skb), 0, sizeof(struct ninet_skb_parm)); - - NIPCB(skb)->dstaddr = head.daddr; - NIPCB(skb)->srcaddr = head.saddr; - NIPCB(skb)->nexthdr = head.nexthdr; - - /* reserve space for hardware header */ - skb_reserve(skb, NIP_ETH_HDR_LEN); - skb_reset_network_header(skb); - - /* build nwk header */ - head.hdr_buf = (unsigned char *)skb->data; - nip_hdr_comm_encap(&head); - head.total_len = head.hdr_buf_pos + payload_len; - nip_update_total_len(&head, htons(head.total_len)); - skb_reserve(skb, head.hdr_buf_pos); - skb_reset_transport_header(skb); - /* build na header */ build_na_hdr(smac, dev->addr_len, skb); - /* skip transport hdr */ - skb_reserve(skb, payload_len); - - /* set skb->data to point network header */ - skb->data = skb_network_header(skb); - skb->len = head.hdr_buf_pos + payload_len; - - dst = nndisc_dst_alloc(dev); - if (!dst) { - kfree_skb(skb); - return; - } - - /* add check sum*/ - checksum = nip_get_nndisc_send_checksum(skb, &head, payload_len); - nip_insert_nndisc_send_checksum(skb, checksum); - - skb_dst_set(skb, dst); - ret = dst_output(dev_net(skb->dev), sk, skb); + ret = nndisc_send_skb(dev, skb, &head, payload_len); if (ret) nip_dbg("%s: dst output fail", __func__); } @@ -475,22 +449,23 @@ bool nip_addr_local(struct net_device *dev, struct nip_addr *addr) rcu_read_lock(); idev = __nin_dev_get(dev); - if (idev) { - read_lock_bh(&idev->lock); - if (!list_empty(&idev->addr_list)) { - struct ninet_ifaddr *ifp; + if (!idev) + goto out; - list_for_each_entry(ifp, &idev->addr_list, if_list) { - if (nip_addr_eq(addr, &ifp->addr)) { - ret = true; - break; - } + read_lock_bh(&idev->lock); + if (!list_empty(&idev->addr_list)) { + struct ninet_ifaddr *ifp; + + list_for_each_entry(ifp, &idev->addr_list, if_list) { + if (nip_addr_eq(addr, &ifp->addr)) { + ret = true; + break; } } - read_unlock_bh(&idev->lock); } + read_unlock_bh(&idev->lock); +out: rcu_read_unlock(); - return ret; } @@ -526,15 +501,14 @@ int nndisc_rcv_ns(struct sk_buff *skb) eth = (struct ethhdr *)skb_mac_header(skb); lladdr = eth->h_source; - /* checksum parse*/ + /* checksum parse */ if (!nip_get_nndisc_rcv_checksum(skb, p)) { nip_dbg("%s:ns ICMP checksum failed, drop the packet", __func__); err = -EINVAL; goto out; } - neigh = __neigh_lookup(&nnd_tbl, &NIPCB(skb)->srcaddr, dev, lladdr || - !dev->addr_len); + neigh = __neigh_lookup(&nnd_tbl, &NIPCB(skb)->srcaddr, dev, lladdr || !dev->addr_len); if (neigh) { neigh_update(neigh, lladdr, NUD_STALE, NEIGH_UPDATE_F_OVERRIDE, 0); neigh_release(neigh); diff --git a/src/linux/net/newip/route.c b/src/linux/net/newip/route.c index 1c3c05f..5a89e1d 100644 --- a/src/linux/net/newip/route.c +++ b/src/linux/net/newip/route.c @@ -31,8 +31,8 @@ #include #include #include -#include /*copy_from_user()*/ -#include /*rtnl_lock()*/ +#include /* copy_from_user() */ +#include /* rtnl_lock() */ #include #include @@ -293,29 +293,28 @@ struct dst_entry *nip_route_output_flags(struct net *net, const struct sock *sk, dst = nip_fib_rule_lookup(net, fln, flags, nip_pol_route_output); rt = (struct nip_rt_info *)dst; - if (rt->rt_flags & RTF_LOCAL) { - rcu_read_lock(); - if (rt->rt_idev) { - read_lock_bh(&rt->rt_idev->lock); - /* search saddr in idev->addr */ - if (!list_empty(&rt->rt_idev->addr_list)) { - struct ninet_ifaddr *ifp; - - list_for_each_entry(ifp, &rt->rt_idev->addr_list, if_list) { - fln->saddr = ifp->addr; - break; - } + if (!(rt->rt_flags & RTF_LOCAL)) + return dst; + + rcu_read_lock(); + if (rt->rt_idev) { + read_lock_bh(&rt->rt_idev->lock); + /* search saddr in idev->addr */ + if (!list_empty(&rt->rt_idev->addr_list)) { + struct ninet_ifaddr *ifp; + + list_for_each_entry(ifp, &rt->rt_idev->addr_list, if_list) { + fln->saddr = ifp->addr; + break; } - read_unlock_bh(&rt->rt_idev->lock); } - rcu_read_unlock(); - - dst_release(dst); - dst_hold(&net->newip.nip_broadcast_entry->dst); - return &net->newip.nip_broadcast_entry->dst; + read_unlock_bh(&rt->rt_idev->lock); } + rcu_read_unlock(); - return dst; + dst_release(dst); + dst_hold(&net->newip.nip_broadcast_entry->dst); + return &net->newip.nip_broadcast_entry->dst; } struct nip_rt_info *nip_pol_route(struct net *net, struct nip_fib_table *table, diff --git a/src/linux/net/newip/tcp_nip.c b/src/linux/net/newip/tcp_nip.c index 5068e16..2a1bf02 100644 --- a/src/linux/net/newip/tcp_nip.c +++ b/src/linux/net/newip/tcp_nip.c @@ -18,37 +18,37 @@ * * Description of States: * - * TCP_SYN_SENT sent a connection request, waiting for ack + * TCP_SYN_SENT sent a connection request, waiting for ack * - * TCP_SYN_RECV received a connection request, sent ack, - * waiting for final ack in three-way handshake. + * TCP_SYN_RECV received a connection request, sent ack, + * waiting for final ack in three-way handshake. * - * TCP_ESTABLISHED connection established + * TCP_ESTABLISHED connection established * - * TCP_FIN_WAIT1 our side has shutdown, waiting to complete - * transmission of remaining buffered data + * TCP_FIN_WAIT1 our side has shutdown, waiting to complete + * transmission of remaining buffered data * - * TCP_FIN_WAIT2 all buffered data sent, waiting for remote - * to shutdown + * TCP_FIN_WAIT2 all buffered data sent, waiting for remote + * to shutdown * - * TCP_CLOSING both sides have shutdown but we still have - * data we have to finish sending + * TCP_CLOSING both sides have shutdown but we still have + * data we have to finish sending * - * TCP_TIME_WAIT timeout to catch resent junk before entering - * closed, can only be entered from FIN_WAIT2 - * or CLOSING. Required because the other end - * may not have gotten our last ACK causing it - * to retransmit the data packet (which we ignore) + * TCP_TIME_WAIT timeout to catch resent junk before entering + * closed, can only be entered from FIN_WAIT2 + * or CLOSING. Required because the other end + * may not have gotten our last ACK causing it + * to retransmit the data packet (which we ignore) * - * TCP_CLOSE_WAIT remote side has shutdown and is waiting for - * us to finish writing our data and to shutdown - * (we have to close() to move on to LAST_ACK) + * TCP_CLOSE_WAIT remote side has shutdown and is waiting for + * us to finish writing our data and to shutdown + * (we have to close() to move on to LAST_ACK) * - * TCP_LAST_ACK out side has shutdown after remote has - * shutdown. There may still be data in our - * buffer that we have to finish sending + * TCP_LAST_ACK out side has shutdown after remote has + * shutdown. There may still be data in our + * buffer that we have to finish sending * - * TCP_CLOSE socket is finished + * TCP_CLOSE socket is finished */ #define pr_fmt(fmt) "NIP-TCP: " fmt @@ -91,8 +91,8 @@ static void tcp_nip_push(struct sock *sk, int flags, int mss_now, } static const unsigned char new_state[16] = { - /* current state: new state: action: */ -[0 /* (Invalid) */] = TCP_CLOSE, + /* current state: new state: action: */ +[0] = TCP_CLOSE, [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, [TCP_SYN_SENT] = TCP_CLOSE, [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN, @@ -245,11 +245,11 @@ static inline bool tcp_nip_need_reset(int state) } /* Function - * Initialize some of the parameters in request_sock + * Initialize some of the parameters in request_sock * Parameter - * req: Request connection control block - * sk_listener: Transmission control block - * skb: Transfer control block buffer + * req: Request connection control block + * sk_listener: Transmission control block + * skb: Transfer control block buffer */ static void tcp_nip_init_req(struct request_sock *req, const struct sock *sk_listener, @@ -262,11 +262,11 @@ static void tcp_nip_init_req(struct request_sock *req, } /* Function - * Initialize The initialization number SEQ. Calculate the initial serial number of - * the server based on part of the source address source port, part of the destination - * address, and destination port + * Initialize The initialization number SEQ. Calculate the initial serial number of + * the server based on part of the source address source port, part of the destination + * address, and destination port * Parameter - * skb: Transfer control block buffer + * skb: Transfer control block buffer */ static __u32 tcp_nip_init_sequence(const struct sk_buff *skb) { @@ -290,12 +290,12 @@ static struct dst_entry *tcp_nip_route_req(const struct sock *sk, } /* Function - * Functions used by the client transport layer to connect requests - * This parameter is used to set the source address, destination address and interface + * Functions used by the client transport layer to connect requests + * This parameter is used to set the source address, destination address and interface * Parameter - * sk: Transmission control block - * uaddr:The destination address - * addr_len:Destination address Length + * sk: Transmission control block + * uaddr:The destination address + * addr_len:Destination address Length */ static int tcp_nip_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) @@ -407,7 +407,9 @@ failure: static void tcp_nip_send_reset(struct sock *sk, struct sk_buff *skb) { const struct tcphdr *th = tcp_hdr(skb); - u32 seq = 0, ack_seq = 0, priority = gfp_any(); + u32 seq = 0; + u32 ack_seq = 0; + u32 priority = gfp_any(); /* Never send a reset in response to a reset. */ if (th->rst) @@ -424,14 +426,14 @@ static void tcp_nip_send_reset(struct sock *sk, struct sk_buff *skb) } /* Function - * function used by the server to send SYN+ACK segments + * function used by the server to send SYN+ACK segments * Parameter - * sk: Transmission control block - * dst: routing。 - * flowi: Flow control block - * req: Request connection control block - * foc: Fast open options - * synack_type: Type of the SYN+ACK segment + * sk: Transmission control block + * dst: routing。 + * flowi: Flow control block + * req: Request connection control block + * foc: Fast open options + * synack_type: Type of the SYN+ACK segment */ static int tcp_nip_send_synack(const struct sock *sk, struct dst_entry *dst, struct flowi *fl, @@ -485,13 +487,13 @@ static const struct tcp_request_sock_ops tcp_request_sock_newip_ops = { }; /* Function - * The route cache saves the transport control block from the SKB + * The route cache saves the transport control block from the SKB * Parameter - * sk: Transmission control block - * skb: Transfer control block buffer - * req: Request connection control block - * dst: routing - * req_unhash: Request connection control block + * sk: Transmission control block + * skb: Transfer control block buffer + * req: Request connection control block + * dst: routing + * req_unhash: Request connection control block */ void ninet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb) { @@ -504,10 +506,10 @@ void ninet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb) } /* Function - * A function used by the server to process client connection requests + * A function used by the server to process client connection requests * Parameter - * sk: Transmission control block - * skb: Transfer control block buffer + * sk: Transmission control block + * skb: Transfer control block buffer */ static int tcp_nip_conn_request(struct sock *sk, struct sk_buff *skb) { @@ -516,13 +518,13 @@ static int tcp_nip_conn_request(struct sock *sk, struct sk_buff *skb) } /* Function - * Create child control blocks + * Create child control blocks * Parameter - * sk: Transmission control block - * skb: Transfer control block buffer - * req: Request connection control block - * dst: routing - * req_unhash: Request connection control block + * sk: Transmission control block + * skb: Transfer control block buffer + * req: Request connection control block + * dst: routing + * req_unhash: Request connection control block */ static struct sock *tcp_nip_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, struct request_sock *req, @@ -705,12 +707,12 @@ void tcp_nip_keepalive_enable(struct sock *sk) nip_dbg("%s HZ=%u, change time/probes/intvl [%u, %u, %u] to [%u, %u, %u]", __func__, HZ, tp->keepalive_time, tp->keepalive_probes, - tp->keepalive_intvl, g_nip_keepalive_time, NIP_KEEPALIVE_PROBES, - g_nip_keepalive_intvl); + tp->keepalive_intvl, get_nip_keepalive_time(), + NIP_KEEPALIVE_PROBES, get_nip_keepalive_intvl()); - tp->keepalive_time = g_nip_keepalive_time; + tp->keepalive_time = get_nip_keepalive_time(); tp->keepalive_probes = NIP_KEEPALIVE_PROBES; - tp->keepalive_intvl = g_nip_keepalive_intvl; + tp->keepalive_intvl = get_nip_keepalive_intvl(); inet_csk_reset_keepalive_timer(sk, tp->keepalive_time); } return; @@ -726,8 +728,8 @@ void tcp_nip_keepalive_enable(struct sock *sk) } /* change para to nip para */ - ret = tcp_nip_keepalive_para_update(sk, g_nip_keepalive_time, - g_nip_keepalive_intvl, + ret = tcp_nip_keepalive_para_update(sk, get_nip_keepalive_time(), + get_nip_keepalive_intvl(), NIP_KEEPALIVE_PROBES); if (ret != 0) { nip_dbg("%s fail, HZ=%u, time/probes/intvl [%u, %u, %u]", __func__, @@ -757,7 +759,7 @@ void tcp_nip_keepalive_disable(struct sock *sk) return; } - if (ntp->idle_ka_probes_out < g_nip_idle_ka_probes_out) + if (ntp->idle_ka_probes_out < get_nip_idle_ka_probes_out()) return; /* newip keepalive change to normal keepalive */ @@ -782,7 +784,7 @@ void tcp_nip_keepalive_disable(struct sock *sk) sk->sk_prot->keepalive(sk, 0); sock_valbool_flag(sk, SOCK_KEEPOPEN, 0); - nip_dbg("%s ok, HZ=%u, idle_ka_probes_out=%u", __func__, HZ, g_nip_idle_ka_probes_out); + nip_dbg("%s ok, HZ=%u, idle_ka_probes_out=%u", __func__, HZ, get_nip_idle_ka_probes_out()); ntp->nip_keepalive_enable = false; #endif } @@ -793,7 +795,7 @@ static void _tcp_sock_priv_init(struct sock *sk) struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common; memset(ntp, 0, sizeof(*ntp)); - ntp->nip_ssthresh = g_nip_ssthresh_default; + ntp->nip_ssthresh = get_nip_ssthresh_default(); tp->sacked_out = 0; tp->rcv_tstamp = 0; tp->selective_acks[0].start_seq = 0; @@ -809,9 +811,9 @@ static void tcp_sock_priv_init(struct sock *sk) } /* Function - * Example Initialize sock information in TCP + * Example Initialize sock information in TCP * Parameter - * sk: Sock to be initialized + * sk: Sock to be initialized * Note: Currently, this function does not initialize timer, pre-queue, and congestion control, * and does not allow fast retransmission. No function is set to adjust MSS */ @@ -826,7 +828,7 @@ static int tcp_nip_init_sock(struct sock *sk) tcp_nip_init_xmit_timers(sk); INIT_LIST_HEAD(&tp->tsq_node); - icsk->icsk_rto = g_nip_rto == 0 ? TCP_TIMEOUT_INIT : (unsigned int)(HZ / g_nip_rto); + icsk->icsk_rto = get_nip_rto() == 0 ? TCP_TIMEOUT_INIT : (HZ / get_nip_rto()); icsk->icsk_rto_min = TCP_RTO_MIN; icsk->icsk_delack_max = TCP_DELACK_MAX; tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT); @@ -846,8 +848,8 @@ static int tcp_nip_init_sock(struct sock *sk) icsk->icsk_sync_mss = tcp_nip_sync_mss; - WRITE_ONCE(sk->sk_sndbuf, g_nip_sndbuf); // sock_net(sk)->ipv4.sysctl_tcp_wmem[1] - WRITE_ONCE(sk->sk_rcvbuf, g_nip_rcvbuf); // sock_net(sk)->ipv4.sysctl_tcp_rmem[1] + WRITE_ONCE(sk->sk_sndbuf, get_nip_sndbuf()); // sock_net(sk)->ipv4.sysctl_tcp_wmem[1] + WRITE_ONCE(sk->sk_rcvbuf, get_nip_rcvbuf()); // sock_net(sk)->ipv4.sysctl_tcp_rmem[1] local_bh_disable(); sk_sockets_allocated_inc(sk); @@ -913,8 +915,11 @@ int tcp_nip_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) { struct tcp_sock *tp = tcp_sk(sk); struct sk_buff *skb; - int flags, err, copied = 0; - int mss_now = 0, size_goal; + int flags; + int err; + int copied = 0; + int mss_now = 0; + int size_goal; bool process_backlog = false; long timeo; @@ -1058,8 +1063,7 @@ void tcp_nip_cleanup_rbuf(struct sock *sk, int copied) if (inet_csk_ack_scheduled(sk)) { const struct inet_connection_sock *icsk = inet_csk(sk); - if (/* Once-per-two-segments ACK was not sent */ - tp->rcv_nxt - tp->rcv_wup > (g_ack_num * 20 * icsk->icsk_ack.rcv_mss) || + if (tp->rcv_nxt - tp->rcv_wup > (get_ack_num() * 20 * icsk->icsk_ack.rcv_mss) || /* If this read emptied read buffer, we send ACK, if * connection is not bidirectional, user drained * receive buffer and there was a small segment @@ -1288,10 +1292,10 @@ void tcp_nip_destroy_sock(struct sock *sk) } /* Function - * The sock handler for THE LISTEN and ESTABLISHED states is called by tcp_nip_rCV + * The sock handler for THE LISTEN and ESTABLISHED states is called by tcp_nip_rCV * Parameter - * skb: Packets received from the network layer - * sk: A SOCK instance needs to be processed + * skb: Packets received from the network layer + * sk: A SOCK instance needs to be processed */ static int tcp_nip_do_rcv(struct sock *sk, struct sk_buff *skb) { @@ -1325,13 +1329,13 @@ discard: } /* Function: - * Fill the TCP header field in SKB into the TCP private control block, - * because the TCP header field in SKB is the network byte order, - * in order to facilitate later call, need to convert the host byte order - * and store in the TCP control block. + * Fill the TCP header field in SKB into the TCP private control block, + * because the TCP header field in SKB is the network byte order, + * in order to facilitate later call, need to convert the host byte order + * and store in the TCP control block. * Parameter: - * skb:Packets delivered by the network layer - * th:TCP header field in a packet + * skb:Packets delivered by the network layer + * th:TCP header field in a packet */ static void tcp_nip_fill_cb(struct sk_buff *skb, const struct tcphdr *th) { @@ -1375,10 +1379,10 @@ static bool tcp_nip_add_backlog(struct sock *sk, struct sk_buff *skb) } /* Function - * TCP is the gateway from the network layer to the transport layer - * and receives data packets from the network layer + * TCP is the gateway from the network layer to the transport layer + * and receives data packets from the network layer * Parameter - * skb:Packets delivered by the network layer + * skb:Packets delivered by the network layer */ static int tcp_nip_rcv(struct sk_buff *skb) { @@ -1571,10 +1575,10 @@ void tcp_nip_done(struct sock *sk) } /* Function - * Disconnect the connection to the peer end, non-blocking - * Release read/write queue, send RST (not sent yet), clear timer + * Disconnect the connection to the peer end, non-blocking + * Release read/write queue, send RST (not sent yet), clear timer * Parameter - * sk: Transmission control block + * sk: Transmission control block */ int tcp_nip_disconnect(struct sock *sk, int flags) { @@ -1749,7 +1753,7 @@ void nip_dbg(const char *fmt, ...) va_list args; int len = 0; - if (!g_nip_debug) + if (!get_nip_debug()) return; memset(str, 0, sizeof(str)); diff --git a/src/linux/net/newip/tcp_nip_input.c b/src/linux/net/newip/tcp_nip_input.c index f662a9a..1073f38 100644 --- a/src/linux/net/newip/tcp_nip_input.c +++ b/src/linux/net/newip/tcp_nip_input.c @@ -25,35 +25,35 @@ #include #include "tcp_nip_parameter.h" -#define FLAG_DATA 0x01 /* Incoming frame contained data. */ -#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ -#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ -#define FLAG_RETRANS_DATA_ACKED 0x08 /* some of which was retransmitted. */ -#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ -#define FLAG_DATA_SACKED 0x20 /* New SACK. */ -#define FLAG_ECE 0x40 /* ECE in this ACK */ -#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */ -#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ -#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */ -#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */ -#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */ -#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */ -#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */ -#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */ - -#define FLAG_ACKED (FLAG_DATA_ACKED | FLAG_SYN_ACKED) -#define FLAG_NOT_DUP (FLAG_DATA | FLAG_WIN_UPDATE | FLAG_ACKED) -#define FLAG_CA_ALERT (FLAG_DATA_SACKED | FLAG_ECE) -#define FLAG_FORWARD_PROGRESS (FLAG_ACKED | FLAG_DATA_SACKED) +#define FLAG_DATA 0x01 /* Incoming frame contained data. */ +#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ +#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ +#define FLAG_RETRANS_DATA_ACKED 0x08 /* some of which was retransmitted. */ +#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ +#define FLAG_DATA_SACKED 0x20 /* New SACK. */ +#define FLAG_ECE 0x40 /* ECE in this ACK */ +#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */ +#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ +#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */ +#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */ +#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */ +#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */ +#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */ +#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */ + +#define FLAG_ACKED (FLAG_DATA_ACKED | FLAG_SYN_ACKED) +#define FLAG_NOT_DUP (FLAG_DATA | FLAG_WIN_UPDATE | FLAG_ACKED) +#define FLAG_CA_ALERT (FLAG_DATA_SACKED | FLAG_ECE) +#define FLAG_FORWARD_PROGRESS (FLAG_ACKED | FLAG_DATA_SACKED) #define TCP_REMNANT (TCP_FLAG_FIN | TCP_FLAG_URG | TCP_FLAG_SYN | TCP_FLAG_PSH) #define TCP_HP_BITS (~(TCP_RESERVED_BITS | TCP_FLAG_PSH)) -#define REXMIT_NONE 0 /* no loss recovery to do */ -#define REXMIT_LOST 1 /* retransmit packets marked lost */ -#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */ +#define REXMIT_NONE 0 /* no loss recovery to do */ +#define REXMIT_LOST 1 /* retransmit packets marked lost */ +#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */ -#define TCP_MAX_MSS 1460 +#define TCP_MAX_MSS 1460 void tcp_nip_fin(struct sock *sk) { @@ -315,8 +315,7 @@ static void tcp_nip_check_space(struct sock *sk) { /* Invoke memory barrier (annotated prior to checkpatch requirements) */ smp_mb(); - if (sk->sk_socket && - test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) + if (sk->sk_socket && test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) tcp_nip_new_space(sk); } @@ -330,7 +329,7 @@ static inline void tcp_nip_data_snd_check(struct sock *sk) void tcp_nip_send_delayed_ack(struct sock *sk) { struct inet_connection_sock *icsk = inet_csk(sk); - int ato = TCP_NIP_DELACK_MIN; // rtt + int ato = TCP_NIP_DELACK_MIN; unsigned long timeout; icsk->icsk_ack.ato = TCP_DELACK_MIN; @@ -364,7 +363,7 @@ static void __tcp_nip_ack_snd_check(struct sock *sk, int ofo_possible) inet_csk(sk)->icsk_ack.rcv_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS /* More than n full frame received... */ - if (((tp->rcv_nxt - tp->rcv_wup) > g_ack_num * inet_csk(sk)->icsk_ack.rcv_mss && + if (((tp->rcv_nxt - tp->rcv_wup) > get_ack_num() * inet_csk(sk)->icsk_ack.rcv_mss && __nip_tcp_select_window(sk) >= tp->rcv_wnd) || /* We have out of order data. */ (ofo_possible && tp->nip_out_of_order_queue)) { @@ -375,9 +374,9 @@ static void __tcp_nip_ack_snd_check(struct sock *sk, int ofo_possible) ntp->dup_ack_cnt = 0; ntp->last_rcv_nxt = tp->rcv_nxt; } - if (ntp->dup_ack_cnt < g_dup_ack_snd_max) + if (ntp->dup_ack_cnt < get_dup_ack_snd_max()) tcp_nip_send_ack(sk); - else if (ntp->dup_ack_cnt % g_dup_ack_snd_max == 0) + else if (ntp->dup_ack_cnt % get_dup_ack_snd_max() == 0) tcp_nip_send_ack(sk); } else { tcp_nip_send_ack(sk); @@ -456,21 +455,21 @@ static int tcp_nip_clean_rtx_queue(struct sock *sk, ktime_t *skb_snd_tstamp) sk_wmem_free_skb(sk, skb); } - icsk->icsk_rto = (unsigned int)(HZ / g_nip_rto); + icsk->icsk_rto = (unsigned int)(HZ / get_nip_rto()); if (flag & FLAG_ACKED) tcp_nip_rearm_rto(sk); return 0; } /* Function - * Allocate a connection request block that holds connection request information. - * At the same time, initialize the set of operations used to send ACK/RST segments - * during connection, so that these interfaces can be easily called during establishment. - * Set the socket state to TCP_NEW_SYN_RECV + * Allocate a connection request block that holds connection request information. + * At the same time, initialize the set of operations used to send ACK/RST segments + * during connection, so that these interfaces can be easily called during establishment. + * Set the socket state to TCP_NEW_SYN_RECV * Parameter - * ops: Request the functional interface of the control block - * sk_listener: Transmission control block - * attach_listener: Whether to set cookies + * ops: Request the functional interface of the control block + * sk_listener: Transmission control block + * attach_listener: Whether to set cookies */ struct request_sock *ninet_reqsk_alloc(const struct request_sock_ops *ops, struct sock *sk_listener, @@ -519,13 +518,13 @@ void tcp_nip_parse_mss(struct tcp_options_received *opt_rx, } /* Function - * Look for tcp options. Normally only called on SYN and SYNACK packets. - * Parsing of TCP options in SKB + * Look for tcp options. Normally only called on SYN and SYNACK packets. + * Parsing of TCP options in SKB * Parameter - * skb: Transfer control block buffer - * opt_rx: Saves the structure for TCP options - * estab: WANTCOOKIE - * foc: Len field + * skb: Transfer control block buffer + * opt_rx: Saves the structure for TCP options + * estab: WANTCOOKIE + * foc: Len field */ void tcp_nip_parse_options(const struct sk_buff *skb, struct tcp_options_received *opt_rx, int estab, @@ -555,7 +554,7 @@ void tcp_nip_parse_options(const struct sk_buff *skb, if (opsize < 2) /* "2 - silly options" */ return; if (opsize > length) - return; /* don't parse partial options */ + return; /* don't parse partial options */ switch (opcode) { case TCPOPT_MSS: tcp_nip_parse_mss(opt_rx, th, ptr, opsize, estab); @@ -575,17 +574,17 @@ static void tcp_nip_common_init(struct request_sock *req) struct tcp_nip_common *ntp = &niptreq->common; memset(ntp, 0, sizeof(*ntp)); - ntp->nip_ssthresh = g_nip_ssthresh_default; + ntp->nip_ssthresh = get_nip_ssthresh_default(); } /* Function - * Initializes the connection request block information based - * on the options and sequence number in the received SYN segment + * Initializes the connection request block information based + * on the options and sequence number in the received SYN segment * Parameter - * req: Request connection control block - * rx_opt: Saves the structure for TCP options - * skb: Transfer control block buffer. - * sk: transmission control block. + * req: Request connection control block + * rx_opt: Saves the structure for TCP options + * skb: Transfer control block buffer. + * sk: transmission control block. */ static void tcp_nip_openreq_init(struct request_sock *req, const struct tcp_options_received *rx_opt, @@ -605,10 +604,10 @@ static void tcp_nip_openreq_init(struct request_sock *req, ireq->tstamp_ok = rx_opt->tstamp_ok; ireq->snd_wscale = rx_opt->snd_wscale; - if (g_wscale_enable == 1) { + if (get_wscale_enable()) { ireq->wscale_ok = 1; - ireq->snd_wscale = g_wscale; // rx_opt->snd_wscale; - ireq->rcv_wscale = g_wscale; + ireq->snd_wscale = get_wscale(); // rx_opt->snd_wscale; + ireq->rcv_wscale = get_wscale(); } ireq->acked = 0; @@ -619,12 +618,12 @@ static void tcp_nip_openreq_init(struct request_sock *req, } /* Function - * Based on listening SOCK and REQ, create a transport control block - * for the new connection and initialize it. + * Based on listening SOCK and REQ, create a transport control block + * for the new connection and initialize it. * Parameter - * sk: the listening transmission control block. - * req: Request connection control block - * skb: Transfer control block buffer. + * sk: the listening transmission control block. + * req: Request connection control block + * skb: Transfer control block buffer. */ struct sock *tcp_nip_create_openreq_child(const struct sock *sk, struct request_sock *req, @@ -664,7 +663,7 @@ struct sock *tcp_nip_create_openreq_child(const struct sock *sk, /* Initialization of delay-related variables */ minmax_reset(&newtp->rtt_min, tcp_jiffies32, ~0U); - newicsk->icsk_rto = g_nip_rto == 0 ? TCP_TIMEOUT_INIT : (HZ / g_nip_rto); + newicsk->icsk_rto = get_nip_rto() == 0 ? TCP_TIMEOUT_INIT : (HZ / get_nip_rto()); newicsk->icsk_ack.lrcvtime = tcp_jiffies32; /* The congestion control-related variables are initialized */ @@ -766,16 +765,16 @@ void tcp_nip_openreq_init_rwin(struct request_sock *req, 0, &rcv_wscale, 0); - ireq->rcv_wscale = g_wscale_enable == 1 ? g_wscale : rcv_wscale; + ireq->rcv_wscale = get_wscale_enable() ? get_wscale() : rcv_wscale; } /* Function - * A function used by the server to process client connection requests. + * A function used by the server to process client connection requests. * Parameter - * rsk_ops: Functional interface to request control blocks. - * af_ops: The functional interface of the TCP request block. - * sk: transmission control block. - * skb: Transfer control block buffer. + * rsk_ops: Functional interface to request control blocks. + * af_ops: The functional interface of the TCP request block. + * sk: transmission control block. + * skb: Transfer control block buffer. */ int _tcp_nip_conn_request(struct request_sock_ops *rsk_ops, const struct tcp_request_sock_ops *af_ops, @@ -913,7 +912,7 @@ static void tcp_nip_ack_update_window(struct sock *sk, const struct sk_buff *skb } /* Check whether the ACK returned by the packet is detected - *and whether the peer window is opened + * and whether the peer window is opened */ static void tcp_nip_ack_probe(struct sock *sk) { @@ -1026,7 +1025,7 @@ static void tcp_nip_dup_ack_retrans(struct sock *sk, const struct sk_buff *skb, ntp->ack_retrans_seq = ack; ntp->ack_retrans_num = 0; - ntp->nip_ssthresh = g_ssthresh_low; + ntp->nip_ssthresh = get_ssthresh_low(); ssthresh_dbg("%s new dup ack, win %u to %u, discard_num=%u, seq=%u~%u", __func__, last_nip_ssthresh, ntp->nip_ssthresh, discard_num, tp->selective_acks[0].start_seq, @@ -1069,60 +1068,62 @@ static void tcp_nip_ack_calc_ssthresh(struct sock *sk, u32 ack, int icsk_rto_las struct tcp_sock *tp = tcp_sk(sk); struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common; struct inet_connection_sock *icsk = inet_csk(sk); - int ack_reset = ack / g_nip_ssthresh_reset; + int ack_reset = ack / get_nip_ssthresh_reset(); u32 nip_ssthresh; if (ntp->nip_ssthresh_reset != ack_reset) { ssthresh_dbg("%s ack reset win %u to %u, ack=%u", - __func__, ntp->nip_ssthresh, g_ssthresh_low, ack); + __func__, ntp->nip_ssthresh, get_ssthresh_low(), ack); ntp->nip_ssthresh_reset = ack_reset; - ntp->nip_ssthresh = g_ssthresh_low; + ntp->nip_ssthresh = get_ssthresh_low(); } else { if (skb_snd_tstamp) { u32 rtt_tstamp = tp->rcv_tstamp - skb_snd_tstamp; - if (rtt_tstamp >= g_rtt_tstamp_rto_up) { + if (rtt_tstamp >= get_rtt_tstamp_rto_up()) { ssthresh_dbg("%s rtt %u >= %u, win %u to %u, rto %u to %u, ack=%u", - __func__, rtt_tstamp, g_rtt_tstamp_rto_up, - ntp->nip_ssthresh, g_ssthresh_low_min, + __func__, rtt_tstamp, get_rtt_tstamp_rto_up(), + ntp->nip_ssthresh, get_ssthresh_low_min(), icsk_rto_last, icsk->icsk_rto, ack); - ntp->nip_ssthresh = g_ssthresh_low_min; - } else if (rtt_tstamp >= g_rtt_tstamp_high) { + ntp->nip_ssthresh = get_ssthresh_low_min(); + } else if (rtt_tstamp >= get_rtt_tstamp_high()) { ssthresh_dbg("%s rtt %u >= %u, win %u to %u, ack=%u", - __func__, rtt_tstamp, g_rtt_tstamp_high, - ntp->nip_ssthresh, g_ssthresh_low, ack); + __func__, rtt_tstamp, get_rtt_tstamp_high(), + ntp->nip_ssthresh, get_ssthresh_low(), ack); - ntp->nip_ssthresh = g_ssthresh_low; - } else if (rtt_tstamp >= g_rtt_tstamp_mid_high) { + ntp->nip_ssthresh = get_ssthresh_low(); + } else if (rtt_tstamp >= get_rtt_tstamp_mid_high()) { ssthresh_dbg("%s rtt %u >= %u, win %u to %u, ack=%u", - __func__, rtt_tstamp, g_rtt_tstamp_mid_high, - ntp->nip_ssthresh, g_ssthresh_mid_low, ack); + __func__, rtt_tstamp, get_rtt_tstamp_mid_high(), + ntp->nip_ssthresh, get_ssthresh_mid_low(), ack); - ntp->nip_ssthresh = g_ssthresh_mid_low; - } else if (rtt_tstamp >= g_rtt_tstamp_mid_low) { - u32 rtt_tstamp_scale = g_rtt_tstamp_mid_high - rtt_tstamp; - int half_mid_high = g_ssthresh_mid_high / 2; + ntp->nip_ssthresh = get_ssthresh_mid_low(); + } else if (rtt_tstamp >= get_rtt_tstamp_mid_low()) { + u32 rtt_tstamp_scale = get_rtt_tstamp_mid_high() - rtt_tstamp; + int half_mid_high = get_ssthresh_mid_high() / 2; nip_ssthresh = half_mid_high + rtt_tstamp_scale * half_mid_high / - (g_rtt_tstamp_mid_high - g_rtt_tstamp_mid_low); + (get_rtt_tstamp_mid_high() - + get_rtt_tstamp_mid_low()); - ntp->nip_ssthresh = ntp->nip_ssthresh > g_ssthresh_mid_high ? + ntp->nip_ssthresh = ntp->nip_ssthresh > get_ssthresh_mid_high() ? half_mid_high : ntp->nip_ssthresh; - nip_ssthresh = (ntp->nip_ssthresh * g_ssthresh_high_step + - nip_ssthresh) / (g_ssthresh_high_step + 1); + nip_ssthresh = (ntp->nip_ssthresh * get_ssthresh_high_step() + + nip_ssthresh) / (get_ssthresh_high_step() + 1); ssthresh_dbg("%s rtt %u >= %u, win %u to %u, ack=%u", - __func__, rtt_tstamp, g_rtt_tstamp_mid_low, + __func__, rtt_tstamp, get_rtt_tstamp_mid_low(), ntp->nip_ssthresh, nip_ssthresh, ack); ntp->nip_ssthresh = nip_ssthresh; } else if (rtt_tstamp != 0) { - nip_ssthresh = (ntp->nip_ssthresh * g_ssthresh_high_step + - g_ssthresh_high) / (g_ssthresh_high_step + 1); + nip_ssthresh = (ntp->nip_ssthresh * get_ssthresh_high_step() + + get_ssthresh_high()) / + (get_ssthresh_high_step() + 1); ssthresh_dbg("%s rtt %u < %u, win %u to %u, ack=%u", - __func__, rtt_tstamp, g_rtt_tstamp_mid_low, + __func__, rtt_tstamp, get_rtt_tstamp_mid_low(), ntp->nip_ssthresh, nip_ssthresh, ack); ntp->nip_ssthresh = nip_ssthresh; @@ -1173,12 +1174,12 @@ static int tcp_nip_ack(struct sock *sk, const struct sk_buff *skb) tcp_nip_clean_rtx_queue(sk, &skb_snd_tstamp); tcp_nip_ack_calc_ssthresh(sk, ack, icsk_rto_last, skb_snd_tstamp); - tcp_nip_nor_ack_retrans(sk, ack, g_ack_retrans_num); + tcp_nip_nor_ack_retrans(sk, ack, get_ack_retrans_num()); return 1; } // dup ack: ack == tp->snd_una - tcp_nip_dup_ack_retrans(sk, skb, ack, g_dup_ack_retrans_num); + tcp_nip_dup_ack_retrans(sk, skb, ack, get_dup_ack_retrans_num()); return 1; } @@ -1384,11 +1385,11 @@ void tcp_nip_finish_connect(struct sock *sk, struct sk_buff *skb) } /* Function: - * A function that handles the second handshake + * A function that handles the second handshake * Parameter: - * sk: transmission control block - * skb: Transfer control block buffer - * Th: TCP header field + * sk: transmission control block + * skb: Transfer control block buffer + * Th: TCP header field */ static int tcp_nip_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th) @@ -1410,7 +1411,7 @@ static int tcp_nip_rcv_synsent_state_process(struct sock *sk, struct sk_buff *sk if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) || after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) goto reset_and_undo; - /* Must be within the corresponding time*/ + /* Must be within the corresponding time */ if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp, tcp_time_stamp(tp))) { NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED); @@ -1435,10 +1436,10 @@ static int tcp_nip_rcv_synsent_state_process(struct sock *sk, struct sk_buff *sk tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; tp->snd_wnd = ntohs(th->window); - if (g_wscale_enable == 1) { + if (get_wscale_enable()) { tp->rx_opt.wscale_ok = 1; - tp->rx_opt.snd_wscale = g_wscale; - tp->rx_opt.rcv_wscale = g_wscale; + tp->rx_opt.snd_wscale = get_wscale(); + tp->rx_opt.rcv_wscale = get_wscale(); } if (!tp->rx_opt.wscale_ok) { @@ -1493,13 +1494,13 @@ reset_and_undo: } /* Function: - * TCP processing function that is differentiated according to - * different states after receiving data packets + * TCP processing function that is differentiated according to + * different states after receiving data packets * Parameter: - * sk: transmission control block - * skb: Transfer control block buffer + * sk: transmission control block + * skb: Transfer control block buffer * Note: Currently this function only has code for handling the first handshake packet - * Implementation of the third handshake ACK to handle the code + * Implementation of the third handshake ACK to handle the code */ int tcp_nip_rcv_state_process(struct sock *sk, struct sk_buff *skb) { @@ -1672,9 +1673,9 @@ discard: } /* Function - * Initialize RCV_MSS + * Initialize RCV_MSS * Parameter - * sk: transmission control block + * sk: transmission control block */ void tcp_nip_initialize_rcv_mss(struct sock *sk) { @@ -1689,17 +1690,17 @@ void tcp_nip_initialize_rcv_mss(struct sock *sk) } /* Function - * Handle the third handshake ACK and return the new control block successfully. - * Is the core process for handling ACKS. - * (1)Create a child control block. Note that the state of the child control - * block is TCP_SYN_RECV - * This is different from the TCP_NEW_SYN_RECV control block created when syn was received. - * (2)Remove the request control block from the incomplete connection queue - * and add it to the completed connection queue + * Handle the third handshake ACK and return the new control block successfully. + * Is the core process for handling ACKS. + * (1)Create a child control block. Note that the state of the child control + * block is TCP_SYN_RECV + * This is different from the TCP_NEW_SYN_RECV control block created when syn was received. + * (2)Remove the request control block from the incomplete connection queue + * and add it to the completed connection queue * Parameter - * sk: transmission control block - * skb: Transfer control block buffer - * req: Request connection control block + * sk: transmission control block + * skb: Transfer control block buffer + * req: Request connection control block */ struct sock *tcp_nip_check_req(struct sock *sk, struct sk_buff *skb, struct request_sock *req) diff --git a/src/linux/net/newip/tcp_nip_output.c b/src/linux/net/newip/tcp_nip_output.c index 23ba3d0..dbc515f 100644 --- a/src/linux/net/newip/tcp_nip_output.c +++ b/src/linux/net/newip/tcp_nip_output.c @@ -25,21 +25,19 @@ #include "nip_checksum.h" #include "tcp_nip_parameter.h" -#define OPTION_SACK_ADVERTISE BIT(0) -#define OPTION_TS BIT(1) -#define OPTION_MD5 BIT(2) -#define OPTION_WSCALE BIT(3) -#define OPTION_FAST_OPEN_COOKIE BIT(8) +#define OPTION_SACK_ADVERTISE BIT(0) +#define OPTION_TS BIT(1) +#define OPTION_MD5 BIT(2) +#define OPTION_WSCALE BIT(3) +#define OPTION_FAST_OPEN_COOKIE BIT(8) /* Store the options contained in TCP when sending TCP packets */ struct tcp_nip_out_options { - u16 options; /* bit field of OPTION_* */ - u16 mss; /* If it is zero, the MSS option is disabled */ + u16 options; /* bit field of OPTION_* */ + u16 mss; /* If it is zero, the MSS option is disabled */ - u8 ws; /* window scale, 0 to disable, If the window is enlarged, - * 0 indicates that the option is disabled - */ - __u32 tsval, tsecr; /* need to include OPTION_TS */ + u8 ws; /* window scale, 0 to disable */ + __u32 tsval, tsecr; /* need to include OPTION_TS */ }; static bool tcp_nip_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, @@ -146,14 +144,14 @@ u32 __nip_tcp_select_window(struct sock *sk) } } - if (g_nip_tcp_rcv_win_enable) { - if (g_ssthresh_enable == 1) + if (get_nip_tcp_rcv_win_enable()) { + if (get_ssthresh_enable()) free_space = free_space > ntp->nip_ssthresh ? ntp->nip_ssthresh : free_space; else free_space = free_space > tp->rcv_ssthresh ? tp->rcv_ssthresh : free_space; } else { - free_space = free_space > g_ssthresh_high ? g_ssthresh_high : free_space; + free_space = free_space > get_ssthresh_high() ? get_ssthresh_high() : free_space; } /* Don't do rounding if we are using window scaling, since the @@ -235,9 +233,9 @@ static u16 nip_tcp_select_window(struct sock *sk) } /* Function - * Initialize transport layer parameters. + * Initialize transport layer parameters. * Parameter - * sk: transmission control block. + * sk: transmission control block. */ static void tcp_nip_connect_init(struct sock *sk) { @@ -273,7 +271,7 @@ static void tcp_nip_connect_init(struct sock *sk) &rcv_wscale, 0); - tp->rx_opt.rcv_wscale = g_wscale_enable == 1 ? g_wscale : rcv_wscale; + tp->rx_opt.rcv_wscale = get_wscale_enable() ? get_wscale() : rcv_wscale; tp->rcv_ssthresh = tp->rcv_wnd; sk->sk_err = 0; @@ -290,7 +288,7 @@ static void tcp_nip_connect_init(struct sock *sk) tp->rcv_nxt = 0; tp->rcv_wup = tp->rcv_nxt; tp->copied_seq = tp->rcv_nxt; - inet_csk(sk)->icsk_rto = g_nip_rto == 0 ? TCP_TIMEOUT_INIT : (HZ / g_nip_rto); + inet_csk(sk)->icsk_rto = get_nip_rto() == 0 ? TCP_TIMEOUT_INIT : (HZ / get_nip_rto()); inet_csk(sk)->icsk_retransmits = 0; tcp_clear_retrans(tp); } @@ -311,8 +309,8 @@ static void tcp_nip_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags) TCP_SKB_CB(skb)->end_seq = seq; } -#define OPTION_TS BIT(1) -#define OPTION_WSCALE BIT(3) +#define OPTION_TS BIT(1) +#define OPTION_WSCALE BIT(3) static void tcp_nip_connect_queue_skb(struct sock *sk, struct sk_buff *skb) { @@ -395,12 +393,12 @@ static unsigned int tcp_nip_established_options(struct sock *sk, struct sk_buff } /* Function - * Put the parameters from the TCP option into SKB. - * Write previously computed TCP options to the packet. + * Put the parameters from the TCP option into SKB. + * Write previously computed TCP options to the packet. * Parameter - * ptr: pointer to TCP options in SKB. - * tp: transmission control block. - * opts: structure to be sent to temporarily load TCP options. + * ptr: pointer to TCP options in SKB. + * tp: transmission control block. + * opts: structure to be sent to temporarily load TCP options. */ static void tcp_nip_options_write(__be32 *ptr, struct tcp_sock *tp, struct tcp_nip_out_options *opts) @@ -496,11 +494,11 @@ static int __tcp_nip_transmit_skb(struct sock *sk, struct sk_buff *skb, refcount_add(skb->truesize, &sk->sk_wmem_alloc); /* Build TCP header and checksum it. */ - th = (struct tcphdr *)skb->data; - th->source = inet->inet_sport; - th->dest = inet->inet_dport; - th->seq = htonl(tcb->seq); - th->ack_seq = htonl(rcv_nxt); + th = (struct tcphdr *)skb->data; + th->source = inet->inet_sport; + th->dest = inet->inet_dport; + th->seq = htonl(tcb->seq); + th->ack_seq = htonl(rcv_nxt); /* TCP's header offset is measured in 4 bytes, so moving two to the right * means dividing by 4. In addition, according to the position of the offset * field in the packet, the offset field is at the beginning of a short type, @@ -546,11 +544,11 @@ static int __tcp_nip_transmit_skb(struct sock *sk, struct sk_buff *skb, } /* Function - * TCP's transport layer sends code that builds and initializes the TCP header - * Construct the SK_buff call transport layer to network layer interface + * TCP's transport layer sends code that builds and initializes the TCP header + * Construct the SK_buff call transport layer to network layer interface * Parameter - * sk: Transmission control block. - * skb: Structure stores all information about network datagrams + * sk: Transmission control block. + * skb: Structure stores all information about network datagrams */ int tcp_nip_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, gfp_t gfp_mask) @@ -571,9 +569,9 @@ static void tcp_nip_queue_skb(struct sock *sk, struct sk_buff *skb) } /* Function - * A function used by the client transport layer to connect requests. + * A function used by the client transport layer to connect requests. * Parameter - * sk: transmission control block. + * sk: transmission control block. */ int __tcp_nip_connect(struct sock *sk) { @@ -663,15 +661,15 @@ unsigned int tcp_nip_current_mss(struct sock *sk) } /* Function: - * Set up TCP options for SYN-ACKs. - * Initializes the TCP option for the SYN-ACK segment. Returns the SIZE of the TCP header. + * Set up TCP options for SYN-ACKs. + * Initializes the TCP option for the SYN-ACK segment. Returns the SIZE of the TCP header. * Parameter - * req: Request connection control block. - * mss: maximum segment length. - * skb: Transfer control block buffer. - * opts: stores the options contained in TCP packets when they are sent. - * foc: Fast Open option. - * synack_type: type of SYN+ACK segment. + * req: Request connection control block. + * mss: maximum segment length. + * skb: Transfer control block buffer. + * opts: stores the options contained in TCP packets when they are sent. + * foc: Fast Open option. + * synack_type: type of SYN+ACK segment. */ static unsigned int tcp_nip_synack_options(struct request_sock *req, unsigned int mss, struct sk_buff *skb, @@ -725,14 +723,14 @@ static int get_nip_mss(const struct sock *sk, struct dst_entry *dst, struct requ } /* Function - * The SYN + ACK segment is constructed based on the current transport control block, - * routing information, and request information. + * The SYN + ACK segment is constructed based on the current transport control block, + * routing information, and request information. * Parameter - * sk: transmission control block. - * dst: routing. - * req: Request connection control block. - * foc: Fast Open option. - * synack_type: type of SYN+ACK segment. + * sk: transmission control block. + * dst: routing. + * req: Request connection control block. + * foc: Fast Open option. + * synack_type: type of SYN+ACK segment. */ struct sk_buff *tcp_nip_make_synack(const struct sock *sk, struct dst_entry *dst, struct request_sock *req, @@ -815,10 +813,10 @@ struct sk_buff *tcp_nip_make_synack(const struct sock *sk, struct dst_entry *dst } /* Function - * Send SKB packets with SYN+ACK segments to the network layer. + * Send SKB packets with SYN+ACK segments to the network layer. * Parameter - * req: Request connection control block. - * skb: Transfer control block buffer. + * req: Request connection control block. + * skb: Transfer control block buffer. */ int __nip_send_synack(struct request_sock *req, struct sk_buff *skb) { @@ -867,11 +865,11 @@ int nip_send_synack(struct request_sock *req, struct sk_buff *skb) } /* Function: - * Creates a subtransport block to complete the establishment of the three-way handshake + * Creates a subtransport block to complete the establishment of the three-way handshake * Parameter: - * parent: indicates the parent transmission control block - * child: indicates the child transmission control block - * skb: Transfer control block buffer + * parent: indicates the parent transmission control block + * child: indicates the child transmission control block + * skb: Transfer control block buffer */ int tcp_nip_child_process(struct sock *parent, struct sock *child, struct sk_buff *skb) @@ -906,10 +904,10 @@ static inline __u32 tcp_nip_acceptable_seq(const struct sock *sk) } /* Function: - * The client sends an ACK + * The client sends an ACK * Parameter: - * sk: transmission control block - * rcv_nxt: serial number to be accepted + * sk: transmission control block + * rcv_nxt: serial number to be accepted */ void __tcp_nip_send_ack(struct sock *sk, u32 rcv_nxt) { @@ -940,7 +938,8 @@ void tcp_nip_send_ack(struct sock *sk) void tcp_nip_send_fin(struct sock *sk) { - struct sk_buff *skb, *tskb = tcp_write_queue_tail(sk); + struct sk_buff *skb; + struct sk_buff *tskb = tcp_write_queue_tail(sk); struct tcp_sock *tp = tcp_sk(sk); u32 cur_mss; @@ -1033,7 +1032,7 @@ static bool tcp_nip_write_xmit(struct sock *sk, unsigned int mss_now, int nonagl struct tcp_sock *tp = tcp_sk(sk); struct tcp_nip_common *ntp = &tcp_nip_sk(sk)->common; struct sk_buff *skb; - u32 snd_num = g_nip_tcp_snd_win_enable ? (ntp->nip_ssthresh / mss_now) : 0xFFFFFFFF; + u32 snd_num = get_nip_tcp_snd_win_enable() ? (ntp->nip_ssthresh / mss_now) : 0xFFFFFFFF; u32 last_nip_ssthresh = ntp->nip_ssthresh; static const char * const str[] = {"can`t send pkt because no window", "have window to send pkt"}; @@ -1046,11 +1045,11 @@ static bool tcp_nip_write_xmit(struct sock *sk, unsigned int mss_now, int nonagl if (tp->rcv_tstamp) { u32 tstamp = tcp_jiffies32 - tp->rcv_tstamp; - if (tstamp >= g_ack_to_nxt_snd_tstamp) { - ntp->nip_ssthresh = g_ssthresh_low_min; + if (tstamp >= get_ack_to_nxt_snd_tstamp()) { + ntp->nip_ssthresh = get_ssthresh_low_min(); snd_num = ntp->nip_ssthresh / mss_now; ssthresh_dbg("%s new snd tstamp %u >= %u, ssthresh %u to %u, snd_num=%u", - __func__, tstamp, g_ack_to_nxt_snd_tstamp, + __func__, tstamp, get_ack_to_nxt_snd_tstamp(), last_nip_ssthresh, ntp->nip_ssthresh, snd_num); } } @@ -1085,8 +1084,7 @@ int tcp_nip_rtx_synack(const struct sock *sk, struct request_sock *req) dst = af_ops->route_req(sk, NULL, req); tcp_rsk(req)->txhash = net_tx_rndhash(); - res = af_ops->send_synack(sk, dst, NULL, req, NULL, TCP_SYNACK_NORMAL, - NULL); + res = af_ops->send_synack(sk, dst, NULL, req, NULL, TCP_SYNACK_NORMAL, NULL); return res; } @@ -1113,7 +1111,7 @@ int __tcp_nip_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) return -ENOMEM; } - cur_mss = tcp_nip_current_mss(sk); // TCP_BASE_MSS + cur_mss = tcp_nip_current_mss(sk); if (!before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp)) && TCP_SKB_CB(skb)->seq != tp->snd_una) @@ -1121,8 +1119,7 @@ int __tcp_nip_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) len = cur_mss * segs; if (skb->len > len) { - if (tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, - skb, len, cur_mss, GFP_ATOMIC)) + if (tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, skb, len, cur_mss, GFP_ATOMIC)) return -ENOMEM; /* We'll try again later. */ } else { int diff = tcp_skb_pcount(skb); @@ -1161,9 +1158,9 @@ int tcp_nip_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) return err; } -#define TCP_NIP_DEFERRED_ALL ((1UL << TCP_TSQ_DEFERRED) | \ - (1UL << TCP_NIP_WRITE_TIMER_DEFERRED) | \ - (1UL << TCP_NIP_DELACK_TIMER_DEFERRED) | \ +#define TCP_NIP_DEFERRED_ALL ((1UL << TCP_TSQ_DEFERRED) | \ + (1UL << TCP_NIP_WRITE_TIMER_DEFERRED) | \ + (1UL << TCP_NIP_DELACK_TIMER_DEFERRED) | \ (1UL << TCP_MTU_REDUCED_DEFERRED)) void tcp_nip_release_cb(struct sock *sk) diff --git a/src/linux/net/newip/tcp_nip_parameter.c b/src/linux/net/newip/tcp_nip_parameter.c index 1badd91..7a12755 100644 --- a/src/linux/net/newip/tcp_nip_parameter.c +++ b/src/linux/net/newip/tcp_nip_parameter.c @@ -31,26 +31,51 @@ module_param_named(af_ninet, g_af_ninet, int, 0444); int g_nip_rto = 50; module_param_named(nip_rto, g_nip_rto, int, 0644); +int get_nip_rto(void) +{ + return g_nip_rto; +} + /*********************************************************************************************/ /* TCP sending and receiving buffer configuration */ /*********************************************************************************************/ int g_nip_sndbuf = 1050000; // 1M module_param_named(nip_sndbuf, g_nip_sndbuf, int, 0644); +int get_nip_sndbuf(void) +{ + return g_nip_sndbuf; +} + int g_nip_rcvbuf = 2000000; // 2M module_param_named(nip_rcvbuf, g_nip_rcvbuf, int, 0644); +int get_nip_rcvbuf(void) +{ + return g_nip_rcvbuf; +} + /*********************************************************************************************/ /* Window configuration */ /*********************************************************************************************/ /* Maximum receiving window */ -int g_wscale_enable = 1; -module_param_named(wscale_enable, g_wscale_enable, int, 0644); +bool g_wscale_enable = 1; +module_param_named(wscale_enable, g_wscale_enable, bool, 0644); + +bool get_wscale_enable(void) +{ + return g_wscale_enable; +} /* Window scale configuration, 2^n */ int g_wscale = 7; module_param_named(wscale, g_wscale, int, 0644); +int get_wscale(void) +{ + return g_wscale; +} + /*********************************************************************************************/ /* Enables the debugging of special scenarios */ /*********************************************************************************************/ @@ -58,10 +83,20 @@ module_param_named(wscale, g_wscale, int, 0644); int g_ack_num = 5; module_param_named(ack_num, g_ack_num, int, 0644); +int get_ack_num(void) +{ + return g_ack_num; +} + /* Reset the packet sending window threshold after receiving n ACK packets */ int g_nip_ssthresh_reset = 10000000; // 10M module_param_named(nip_ssthresh_reset, g_nip_ssthresh_reset, int, 0644); +int get_nip_ssthresh_reset(void) +{ + return g_nip_ssthresh_reset; +} + /*********************************************************************************************/ /* Retransmission parameters after ACK */ /*********************************************************************************************/ @@ -69,88 +104,198 @@ module_param_named(nip_ssthresh_reset, g_nip_ssthresh_reset, int, 0644); int g_dup_ack_retrans_num = 5; module_param_named(dup_ack_retrans_num, g_dup_ack_retrans_num, int, 0644); +int get_dup_ack_retrans_num(void) +{ + return g_dup_ack_retrans_num; +} + /* Common ACK Indicates the number of retransmissions */ int g_ack_retrans_num = 5; module_param_named(ack_retrans_num, g_ack_retrans_num, int, 0644); +int get_ack_retrans_num(void) +{ + return g_ack_retrans_num; +} + int g_dup_ack_snd_max = 6; module_param_named(dup_ack_snd_max, g_dup_ack_snd_max, int, 0644); +int get_dup_ack_snd_max(void) +{ + return g_dup_ack_snd_max; +} + /*********************************************************************************************/ /* RTT timestamp parameters */ /*********************************************************************************************/ int g_rtt_tstamp_rto_up = 100; // rtt_tstamp >= 100 ==> shorten rto module_param_named(rtt_tstamp_rto_up, g_rtt_tstamp_rto_up, int, 0644); +int get_rtt_tstamp_rto_up(void) +{ + return g_rtt_tstamp_rto_up; +} + int g_rtt_tstamp_high = 30; // rtt_tstamp >= 30 ==> ssthresh = 100K module_param_named(rtt_tstamp_high, g_rtt_tstamp_high, int, 0644); +int get_rtt_tstamp_high(void) +{ + return g_rtt_tstamp_high; +} + int g_rtt_tstamp_mid_high = 20; // rtt_tstamp >= 20 ==> ssthresh = 250K module_param_named(rtt_tstamp_mid_high, g_rtt_tstamp_mid_high, int, 0644); +int get_rtt_tstamp_mid_high(void) +{ + return g_rtt_tstamp_mid_high; +} + /* rtt_tstamp >= 10 ==> ssthresh = 1M (500K ~ 1M) * rtt_tstamp < 10 ==> ssthresh = 1.5M */ int g_rtt_tstamp_mid_low = 10; module_param_named(rtt_tstamp_mid_low, g_rtt_tstamp_mid_low, int, 0644); +int get_rtt_tstamp_mid_low(void) +{ + return g_rtt_tstamp_mid_low; +} + int g_ack_to_nxt_snd_tstamp = 500; module_param_named(ack_to_nxt_snd_tstamp, g_ack_to_nxt_snd_tstamp, int, 0644); +int get_ack_to_nxt_snd_tstamp(void) +{ + return g_ack_to_nxt_snd_tstamp; +} + /*********************************************************************************************/ /* Window threshold parameters */ /*********************************************************************************************/ -int g_ssthresh_enable = 1; -module_param_named(ssthresh_enable, g_ssthresh_enable, int, 0644); +bool g_ssthresh_enable = 1; +module_param_named(ssthresh_enable, g_ssthresh_enable, bool, 0644); + +bool get_ssthresh_enable(void) +{ + return g_ssthresh_enable; +} int g_nip_ssthresh_default = 300000; // 300K module_param_named(nip_ssthresh_default, g_nip_ssthresh_default, int, 0644); +int get_nip_ssthresh_default(void) +{ + return g_nip_ssthresh_default; +} + int g_ssthresh_high = 1500000; // rtt_tstamp < 10 ==> ssthresh = 1.5M module_param_named(ssthresh_high, g_ssthresh_high, int, 0644); +int get_ssthresh_high(void) +{ + return g_ssthresh_high; +} + int g_ssthresh_mid_high = 1000000; // rtt_tstamp >= 10 ==> ssthresh = 1M (500K ~ 1M) module_param_named(ssthresh_mid_high, g_ssthresh_mid_high, int, 0644); +int get_ssthresh_mid_high(void) +{ + return g_ssthresh_mid_high; +} + int g_ssthresh_mid_low = 250000; // rtt_tstamp >= 20 ==> ssthresh = 250K module_param_named(ssthresh_mid_low, g_ssthresh_mid_low, int, 0644); +int get_ssthresh_mid_low(void) +{ + return g_ssthresh_mid_low; +} + int g_ssthresh_low = 100000; // rtt_tstamp >= 30 ==> ssthresh = 100K module_param_named(ssthresh_low, g_ssthresh_low, int, 0644); +int get_ssthresh_low(void) +{ + return g_ssthresh_low; +} + int g_ssthresh_low_min = 10000; // rtt_tstamp >= 100 ==> ssthresh = 10K module_param_named(ssthresh_low_min, g_ssthresh_low_min, int, 0644); +int get_ssthresh_low_min(void) +{ + return g_ssthresh_low_min; +} + int g_ssthresh_high_step = 1; module_param_named(ssthresh_high_step, g_ssthresh_high_step, int, 0644); +int get_ssthresh_high_step(void) +{ + return g_ssthresh_high_step; +} + /*********************************************************************************************/ /* keepalive parameters */ /*********************************************************************************************/ int g_nip_idle_ka_probes_out = 20; module_param_named(nip_idle_ka_probes_out, g_nip_idle_ka_probes_out, int, 0644); +int get_nip_idle_ka_probes_out(void) +{ + return g_nip_idle_ka_probes_out; +} + int g_nip_keepalive_time = 25; module_param_named(nip_keepalive_time, g_nip_keepalive_time, int, 0644); +int get_nip_keepalive_time(void) +{ + return g_nip_keepalive_time; +} + int g_nip_keepalive_intvl = 25; module_param_named(nip_keepalive_intvl, g_nip_keepalive_intvl, int, 0644); +int get_nip_keepalive_intvl(void) +{ + return g_nip_keepalive_intvl; +} + /*********************************************************************************************/ /* probe parameters */ /*********************************************************************************************/ int g_nip_probe_max = 2000; module_param_named(nip_probe_max, g_nip_probe_max, int, 0644); +int get_nip_probe_max(void) +{ + return g_nip_probe_max; +} + /*********************************************************************************************/ /* window mode parameters */ /*********************************************************************************************/ bool g_nip_tcp_snd_win_enable; module_param_named(nip_tcp_snd_win_enable, g_nip_tcp_snd_win_enable, bool, 0644); +bool get_nip_tcp_snd_win_enable(void) +{ + return g_nip_tcp_snd_win_enable; +} + bool g_nip_tcp_rcv_win_enable = true; module_param_named(nip_tcp_rcv_win_enable, g_nip_tcp_rcv_win_enable, bool, 0644); +bool get_nip_tcp_rcv_win_enable(void) +{ + return g_nip_tcp_rcv_win_enable; +} + /*********************************************************************************************/ /* nip debug parameters */ /*********************************************************************************************/ @@ -158,11 +303,26 @@ module_param_named(nip_tcp_rcv_win_enable, g_nip_tcp_rcv_win_enable, bool, 0644) bool g_nip_debug; module_param_named(nip_debug, g_nip_debug, bool, 0644); +bool get_nip_debug(void) +{ + return g_nip_debug; +} + /* Debugging of threshold change */ -int g_rtt_ssthresh_debug; -module_param_named(rtt_ssthresh_debug, g_rtt_ssthresh_debug, int, 0644); +bool g_rtt_ssthresh_debug; +module_param_named(rtt_ssthresh_debug, g_rtt_ssthresh_debug, bool, 0644); + +bool get_rtt_ssthresh_debug(void) +{ + return g_rtt_ssthresh_debug; +} /* Debugging of packet retransmission after ACK */ -int g_ack_retrans_debug; -module_param_named(ack_retrans_debug, g_ack_retrans_debug, int, 0644); +bool g_ack_retrans_debug; +module_param_named(ack_retrans_debug, g_ack_retrans_debug, bool, 0644); + +bool get_ack_retrans_debug(void) +{ + return g_ack_retrans_debug; +} diff --git a/src/linux/net/newip/tcp_nip_parameter.h b/src/linux/net/newip/tcp_nip_parameter.h index bf7a602..b8409cb 100644 --- a/src/linux/net/newip/tcp_nip_parameter.h +++ b/src/linux/net/newip/tcp_nip_parameter.h @@ -12,95 +12,54 @@ #ifndef _TCP_NIP_PARAMETER_H #define _TCP_NIP_PARAMETER_H -/*********************************************************************************************/ -/* Rto timeout timer period (HZ/n) */ -/*********************************************************************************************/ -extern int g_nip_rto; - -/*********************************************************************************************/ -/* TCP sending and receiving buffer configuration */ -/*********************************************************************************************/ -extern int g_nip_sndbuf; -extern int g_nip_rcvbuf; - -/*********************************************************************************************/ -/* Window configuration */ -/*********************************************************************************************/ -extern int g_wscale_enable; -extern int g_wscale; - -/*********************************************************************************************/ -/* Enables the debugging of special scenarios */ -/*********************************************************************************************/ -extern int g_ack_num; -extern int g_nip_ssthresh_reset; - -/*********************************************************************************************/ -/* Retransmission parameters after ACK */ -/*********************************************************************************************/ -extern int g_dup_ack_retrans_num; -extern int g_ack_retrans_num; -extern int g_dup_ack_snd_max; - -/*********************************************************************************************/ -/* RTT timestamp parameters */ -/*********************************************************************************************/ -extern int g_rtt_tstamp_rto_up; -extern int g_rtt_tstamp_high; -extern int g_rtt_tstamp_mid_high; -extern int g_rtt_tstamp_mid_low; -extern int g_ack_to_nxt_snd_tstamp; - -/*********************************************************************************************/ -/* Window threshold parameters */ -/*********************************************************************************************/ -extern int g_ssthresh_enable; -extern int g_nip_ssthresh_default; -extern int g_ssthresh_high; -extern int g_ssthresh_mid_high; -extern int g_ssthresh_mid_low; -extern int g_ssthresh_low; -extern int g_ssthresh_low_min; -extern int g_ssthresh_high_step; - -/*********************************************************************************************/ -/* keepalive parameters */ -/*********************************************************************************************/ -extern int g_nip_idle_ka_probes_out; -extern int g_nip_keepalive_time; -extern int g_nip_keepalive_intvl; - -/*********************************************************************************************/ -/* window mode parameters */ -/*********************************************************************************************/ -extern bool g_nip_tcp_snd_win_enable; -extern bool g_nip_tcp_rcv_win_enable; - -/*********************************************************************************************/ -/* probe parameters */ -/*********************************************************************************************/ -extern int g_nip_probe_max; +int get_nip_rto(void); +int get_nip_sndbuf(void); +int get_nip_rcvbuf(void); +bool get_wscale_enable(void); +int get_wscale(void); +int get_ack_num(void); +int get_nip_ssthresh_reset(void); +int get_dup_ack_retrans_num(void); +int get_ack_retrans_num(void); +int get_dup_ack_snd_max(void); +int get_rtt_tstamp_rto_up(void); +int get_rtt_tstamp_high(void); +int get_rtt_tstamp_mid_high(void); +int get_rtt_tstamp_mid_low(void); +int get_ack_to_nxt_snd_tstamp(void); +bool get_ssthresh_enable(void); +int get_nip_ssthresh_default(void); +int get_ssthresh_high(void); +int get_ssthresh_mid_high(void); +int get_ssthresh_mid_low(void); +int get_ssthresh_low(void); +int get_ssthresh_low_min(void); +int get_ssthresh_high_step(void); +int get_nip_idle_ka_probes_out(void); +int get_nip_keepalive_time(void); +int get_nip_keepalive_intvl(void); +int get_nip_probe_max(void); +bool get_nip_tcp_snd_win_enable(void); +bool get_nip_tcp_rcv_win_enable(void); +bool get_nip_debug(void); +bool get_rtt_ssthresh_debug(void); +bool get_ack_retrans_debug(void); /*********************************************************************************************/ /* nip debug parameters */ /*********************************************************************************************/ -/* Debugging for control DEBUG */ -extern bool g_nip_debug; - /* Debugging of threshold change */ -extern int g_rtt_ssthresh_debug; #define ssthresh_dbg(fmt, ...) \ do { \ - if (g_rtt_ssthresh_debug) \ + if (get_rtt_ssthresh_debug()) \ pr_crit(fmt, ##__VA_ARGS__); \ } while (0) /* Debugging of packet retransmission after ACK */ -extern int g_ack_retrans_debug; #define retrans_dbg(fmt, ...) \ do { \ - if (g_ack_retrans_debug) \ + if (get_ack_retrans_debug()) \ pr_crit(fmt, ##__VA_ARGS__); \ } while (0) -#endif /* _TCP_NIP_PARAMETER_H */ +#endif /* _TCP_NIP_PARAMETER_H */ diff --git a/src/linux/net/newip/tcp_nip_timer.c b/src/linux/net/newip/tcp_nip_timer.c index cdd5500..5073550 100644 --- a/src/linux/net/newip/tcp_nip_timer.c +++ b/src/linux/net/newip/tcp_nip_timer.c @@ -173,10 +173,10 @@ void tcp_nip_retransmit_timer(struct sock *sk) icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX); ssthresh_dbg("%s seq %u, reset win %u to %u, rto %u to %u, pkt_out=%u", - __func__, scb->seq, ntp->nip_ssthresh, g_ssthresh_low, + __func__, scb->seq, ntp->nip_ssthresh, get_ssthresh_low(), icsk_rto_last, icsk->icsk_rto, tp->packets_out); - ntp->nip_ssthresh = g_ssthresh_low; + ntp->nip_ssthresh = get_ssthresh_low(); inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX); } @@ -198,7 +198,7 @@ void tcp_nip_probe_timer(struct sock *sk) } /* default: sock_net(sk)->ipv4.sysctl_tcp_retries2 */ - max_probes = g_nip_probe_max; /* fix session auto close */ + max_probes = get_nip_probe_max(); /* fix session auto close */ if (sock_flag(sk, SOCK_DEAD)) { const bool alive = inet_csk_rto_backoff(icsk, TCP_RTO_MAX) < TCP_RTO_MAX; diff --git a/src/linux/net/newip/udp.c b/src/linux/net/newip/udp.c index 143e10e..680c05d 100644 --- a/src/linux/net/newip/udp.c +++ b/src/linux/net/newip/udp.c @@ -57,13 +57,11 @@ int nip_udp_get_port(struct sock *sk, unsigned short snum) { unsigned int hash2_nulladdr, hash2_partial; - hash2_nulladdr = nip_udp_portaddr_hash(sock_net(sk), - &nip_any_addr, snum); + hash2_nulladdr = nip_udp_portaddr_hash(sock_net(sk), &nip_any_addr, snum); /* hash2_partial is the hash result of nip_addr only */ - hash2_partial = nip_udp_portaddr_hash(sock_net(sk), - &sk->sk_nip_rcv_saddr, 0); + hash2_partial = nip_udp_portaddr_hash(sock_net(sk), &sk->sk_nip_rcv_saddr, 0); - /* precompute partial secondary hash*/ + /* precompute partial secondary hash */ udp_sk(sk)->udp_portaddr_hash = hash2_partial; return udp_lib_get_port(sk, snum, hash2_nulladdr); } @@ -147,6 +145,7 @@ static struct sock *nip_udp_lib_lookup2(struct net *net, return result; } +/* rcu_read_lock() must be held */ struct sock *__nip_udp_lib_lookup(struct net *net, const struct nip_addr *saddr, __be16 sport, const struct nip_addr *daddr, __be16 dport, @@ -154,52 +153,32 @@ struct sock *__nip_udp_lib_lookup(struct net *net, struct sk_buff *skb) { unsigned short hnum = ntohs(dport); - unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); - unsigned int old_slot2; - int score, badness; - struct sock *sk, *result; - struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; - - if (hslot->count > NIP_UDP_HSLOT_COUNT) { - hash2 = nip_udp_portaddr_hash(net, daddr, hnum); - slot2 = hash2 & udptable->mask; - hslot2 = &udptable->hash2[slot2]; - if (hslot->count < hslot2->count) - goto begin; - - result = nip_udp_lib_lookup2(net, saddr, sport, - daddr, hnum, dif, sdif, - hslot2, skb); - if (!result) { - old_slot2 = slot2; - - hash2 = nip_udp_portaddr_hash(net, &nip_any_addr, hnum); - slot2 = hash2 & udptable->mask; - /* avoid searching the same slot again. */ - if (unlikely(slot2 == old_slot2)) - return result; - - hslot2 = &udptable->hash2[slot2]; - if (hslot->count < hslot2->count) - goto begin; - - result = nip_udp_lib_lookup2(net, saddr, sport, - daddr, hnum, dif, sdif, - hslot2, skb); - } - return result; - } -begin: - result = NULL; - badness = -1; - sk_for_each_rcu(sk, &hslot->head) { - score = nip_udp_compute_score(sk, net, saddr, sport, daddr, - hnum, dif, sdif); - if (score > badness) { - result = sk; - badness = score; - } - } + unsigned int hash2, slot2; + struct udp_hslot *hslot2; + struct sock *result; + + hash2 = nip_udp_portaddr_hash(net, daddr, hnum); + slot2 = hash2 & udptable->mask; + hslot2 = &udptable->hash2[slot2]; + + /* Lookup connected or non-wildcard sockets */ + result = nip_udp_lib_lookup2(net, saddr, sport, + daddr, hnum, dif, sdif, + hslot2, skb); + if (!IS_ERR_OR_NULL(result)) + goto done; + + /* Lookup wildcard sockets */ + hash2 = nip_udp_portaddr_hash(net, &nip_any_addr, hnum); + slot2 = hash2 & udptable->mask; + hslot2 = &udptable->hash2[slot2]; + + result = nip_udp_lib_lookup2(net, saddr, sport, + &nip_any_addr, hnum, dif, sdif, + hslot2, skb); +done: + if (IS_ERR(result)) + return NULL; return result; } @@ -227,7 +206,7 @@ int nip_udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int err; off = sk_peek_offset(sk, flags); - peeking = off; /* Fetch the SKB from the queue */ + peeking = off; /* Fetch the SKB from the queue */ skb = __skb_recv_udp(sk, flags, noblock, &off, &err); if (!skb) return err; -- Gitee