Skip to content

Commit

Permalink
devtelemetry: slight restructure to make way for ipv6 (#52)
Browse files Browse the repository at this point in the history
MIN-2935
  • Loading branch information
Tom-Keddie authored Dec 23, 2021
1 parent f17be6f commit 0e0ec6e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
40 changes: 21 additions & 19 deletions src/unum/devtelemetry/dt_collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,10 @@ static void ip_pkt_rcv_cb(TPCAP_IF_t *tpif,

// Prepare header for the connection info table entry
DT_CONN_HDR_t hdr;
hdr.ipv4.i = peer_ipv4.i;
hdr.proto = iph->protocol;
hdr.rev = FALSE;
hdr.ip.ipv4.i = peer_ipv4.i;
hdr.ip_proto = iph->protocol;
hdr.flags.rev = FALSE;
hdr.flags.version = 4;
hdr.port = 0;
hdr.dev = dev;

Expand All @@ -487,28 +488,28 @@ static void ip_pkt_rcv_cb(TPCAP_IF_t *tpif,
// Work out tcp/udp and what port to report. If the device port is
// less than 1024 we report it, otherwise report peer's port.
// If the packet is corrupt override protocol w/ 0.
if(hdr.proto == 6 || hdr.proto == 17) {
if(hdr.ip_proto == IPPROTO_TCP || hdr.ip_proto == IPPROTO_UDP) {
struct tcphdr* tcph = ((void *)iph) + sizeof(struct iphdr);
struct udphdr* udph = ((void *)iph) + sizeof(struct iphdr);
int have_net_len = thdr->tp_snaplen - (thdr->tp_net - thdr->tp_mac);
int need_net_len = sizeof(struct iphdr);
if(hdr.proto == 6) {
if(hdr.ip_proto == IPPROTO_TCP) {
need_net_len += sizeof(*tcph);
} else if(hdr.proto == 17) {
} else if(hdr.ip_proto == IPPROTO_UDP) {
need_net_len += sizeof(*udph);
}
if(need_net_len <= have_net_len) {
// The ports are in the same place for TCP & UDP
uint16_t sp = ntohs(udph->source);
uint16_t dp = ntohs(udph->dest);
if(to_rtr) {
hdr.rev = (sp < 1024 && dp >= 1024);
hdr.port = hdr.rev ? sp : dp;
hdr.flags.rev = (sp < 1024 && dp >= 1024);
hdr.port = hdr.flags.rev ? sp : dp;
} else { // pkt from the router
hdr.rev = (dp < 1024 && sp >= 1024);
hdr.port = hdr.rev ? dp : sp;
hdr.flags.rev = (dp < 1024 && sp >= 1024);
hdr.port = hdr.flags.rev ? dp : sp;
}
if(hdr.proto == 6) {
if(hdr.ip_proto == IPPROTO_TCP) {
cur_tcp_win_size = ntohs(tcph->window);
// Only capture win size if SYN or SYN|ACK pkt from the device
if(tcph->syn && to_rtr) {
Expand All @@ -517,8 +518,8 @@ static void ip_pkt_rcv_cb(TPCAP_IF_t *tpif,
}
} else {
hdr.port = 0;
hdr.proto = 0;
hdr.rev = FALSE;
hdr.ip_proto = 0;
hdr.flags.rev = FALSE;
}
}

Expand Down Expand Up @@ -578,10 +579,11 @@ int dt_add_fe_conn(FE_CONN_t *fe_conn)

// Prepare the dev telemetry connection header from fe connection header
DT_CONN_HDR_t hdr;
hdr.ipv4.i = fe_conn->hdr.peer_ipv4.i;
hdr.proto = fe_conn->hdr.proto;
hdr.rev = fe_conn->hdr.rev;
hdr.port = (hdr.rev ? fe_conn->hdr.dev_port : fe_conn->hdr.peer_port);
hdr.ip.ipv4.i = fe_conn->hdr.peer_ipv4.i;
hdr.ip_proto = fe_conn->hdr.proto;
hdr.flags.rev = fe_conn->hdr.rev;
hdr.flags.version = 4;
hdr.port = (hdr.flags.rev ? fe_conn->hdr.dev_port : fe_conn->hdr.peer_port);
hdr.dev = dev;

// Adding/updating the connection common info (i.e. the info updated
Expand All @@ -604,7 +606,7 @@ int dt_add_fe_conn(FE_CONN_t *fe_conn)
DPRINTF("%s: fe%s connection p:%d "
IP_PRINTF_FMT_TPL ":%hu <-> " IP_PRINTF_FMT_TPL ":%hu "
"cur in/from:%u out/to:%u\n",
__func__, (fe_conn->hdr.rev ? " rev" : ""), fe_conn->hdr.proto,
__func__, (fe_conn->hdr.flags.rev ? " rev" : ""), fe_conn->hdr.proto,
IP_PRINTF_ARG_TPL(fe_conn->hdr.dev_ipv4.b),
fe_conn->hdr.dev_port,
IP_PRINTF_ARG_TPL(fe_conn->hdr.peer_ipv4.b),
Expand Down Expand Up @@ -929,7 +931,7 @@ static void print_test_conn_info(DT_CONN_t *conn)
conn->hdr.proto, proto_name(conn->hdr.proto),
IP_PRINTF_ARG_TPL(conn->hdr.ipv4.b));
if(conn->hdr.proto == 6 || conn->hdr.proto == 17) {
printf(" p:%u(%s)", conn->hdr.port, conn->hdr.rev ? "our" : "their");
printf(" p:%u(%s)", conn->hdr.port, conn->hdr.flags.rev ? "our" : "their");
}
if(conn->hdr.proto == 6) {
printf(" syn_ws:%d ws:%d",
Expand Down
14 changes: 11 additions & 3 deletions src/unum/devtelemetry/dt_data_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ struct _DT_COUNTERS;

// Connection information structure header
struct _DT_CONN_HDR {
IPV4_ADDR_t ipv4; // Peer's IPv4 address
unsigned char proto; // Connection IP protocol (0 - unrecognized)
unsigned char rev; // TRUE if reporting reverse TCP/UDP port
union {
IPV4_ADDR_t ipv4; // Peer's IPv4 address
#ifdef FEATURE_IPV6_TELEMETRY
IPV6_ADDR_t ipv6; // Peer's IPv6 address
#endif
} ip;
unsigned char ip_proto; // Connection IP protocol (0 - unrecognized)
struct {
unsigned char rev : 1; // TRUE if reporting reverse TCP/UDP port
unsigned char version : 4; // As per ip header, 4=V4, 6=V6
} flags;
uint16_t port; // Peer's TCP/UDP port (device port if rev is TRUE)
struct _DT_DEVICE *dev; // Ptr to the device owning the connection
} __attribute__((packed));
Expand Down
6 changes: 3 additions & 3 deletions src/unum/devtelemetry/dt_sender.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ static JSON_VAL_TPL_t *tpl_con_array_f(DT_DEVICE_t *dev, int idx)

// Prepare the template data
snprintf(con_ip, sizeof(con_ip), IP_PRINTF_FMT_TPL,
IP_PRINTF_ARG_TPL(p_con->hdr.ipv4.b));
proto = p_con->hdr.proto;
IP_PRINTF_ARG_TPL(p_con->hdr.ip.ipv4.b));
proto = p_con->hdr.ip_proto;
tpl_tbl_con_obj[0].val.pul = tpl_tbl_con_obj[1].val.pul = NULL;
tpl_tbl_con_obj[2].val.pul = NULL;
#ifdef REPORT_CURRENT_TCP_WIN_SIZE
tpl_tbl_con_obj[3].val.pul = NULL;
#endif // REPORT_CURRENT_TCP_WIN_SIZE
if(proto == 6 || proto == 17) {
port = p_con->hdr.port;
if(p_con->hdr.rev) { // device's port
if(p_con->hdr.flags.rev) { // device's port
tpl_tbl_con_obj[0].val.pul = &port;
} else { // peer's port
tpl_tbl_con_obj[1].val.pul = &port;
Expand Down
9 changes: 9 additions & 0 deletions src/unum/util/util_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ typedef union {
unsigned int i;
} IPV4_ADDR_t;

// IPv6 internal address data type
typedef union {
unsigned char b[16];
struct {
uint64_t h;
uint64_t l;
} __attribute__((packed)) i;
} IPV6_ADDR_t;

// IP configuration structure describing device IP settings
typedef struct _DEV_IP_CFG {
IPV4_ADDR_t ipv4; // byte order is the same as in struct sockaddr
Expand Down

0 comments on commit 0e0ec6e

Please sign in to comment.