Skip to content

Commit 67adb98

Browse files
Integral-Techmnixry
authored andcommitted
refactor: improve code readability of struct initialization (daeuniverse#633)
1 parent dcd184a commit 67adb98

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

control/kern/tproxy.c

+55-56
Original file line numberDiff line numberDiff line change
@@ -962,10 +962,9 @@ static __always_inline void prep_redirect_to_control_plane(
962962
__builtin_memcpy(&redirect_tuple.dip, &tuples->five.dip,
963963
IPV6_BYTE_LENGTH);
964964
}
965-
struct redirect_entry redirect_entry = {};
965+
struct redirect_entry redirect_entry = { .ifindex = skb->ifindex,
966+
.from_wan = from_wan };
966967

967-
redirect_entry.ifindex = skb->ifindex;
968-
redirect_entry.from_wan = from_wan;
969968
__builtin_memcpy(redirect_entry.smac, ethh->h_source,
970969
sizeof(ethh->h_source));
971970
__builtin_memcpy(redirect_entry.dmac, ethh->h_dest,
@@ -1124,12 +1123,11 @@ int tproxy_lan_ingress(struct __sk_buff *skb)
11241123
bpf_printk("shot routing: %d", s64_ret);
11251124
return TC_ACT_SHOT;
11261125
}
1127-
struct routing_result routing_result = { 0 };
1126+
struct routing_result routing_result = { .outbound = s64_ret,
1127+
.mark = s64_ret >> 8,
1128+
.must = (s64_ret >> 40) & 1,
1129+
.dscp = tuples.dscp };
11281130

1129-
routing_result.outbound = s64_ret;
1130-
routing_result.mark = s64_ret >> 8;
1131-
routing_result.must = (s64_ret >> 40) & 1;
1132-
routing_result.dscp = tuples.dscp;
11331131
__builtin_memcpy(routing_result.mac, ethh.h_source,
11341132
sizeof(routing_result.mac));
11351133
/// NOTICE: No pid pname info for LAN packet.
@@ -1170,11 +1168,12 @@ int tproxy_lan_ingress(struct __sk_buff *skb)
11701168
}
11711169

11721170
// Check outbound connectivity in specific ipversion and l4proto.
1173-
struct outbound_connectivity_query q = { 0 };
1171+
struct outbound_connectivity_query q = {
1172+
.outbound = routing_result.outbound,
1173+
.ipversion = skb->protocol == bpf_htons(ETH_P_IP) ? 4 : 6,
1174+
.l4proto = l4proto
1175+
};
11741176

1175-
q.outbound = routing_result.outbound;
1176-
q.ipversion = skb->protocol == bpf_htons(ETH_P_IP) ? 4 : 6;
1177-
q.l4proto = l4proto;
11781177
__u32 *alive;
11791178

11801179
alive = bpf_map_lookup_elem(&outbound_connectivity_map, &q);
@@ -1272,13 +1271,11 @@ refresh_udp_conn_state_timer(struct tuples_key *key, bool is_egress)
12721271
{
12731272
struct udp_conn_state *old_conn_state =
12741273
bpf_map_lookup_elem(&udp_conn_state_map, key);
1275-
struct udp_conn_state new_conn_state = { 0 };
1274+
struct udp_conn_state new_conn_state = {
1275+
.is_egress = old_conn_state ? old_conn_state->is_egress :
1276+
is_egress
1277+
};
12761278

1277-
if (old_conn_state)
1278-
new_conn_state.is_egress =
1279-
old_conn_state->is_egress; // Keep the value.
1280-
else
1281-
new_conn_state.is_egress = is_egress;
12821279
long ret = bpf_map_update_elem(&udp_conn_state_map, key,
12831280
&new_conn_state, BPF_ANY);
12841281
if (unlikely(ret))
@@ -1463,11 +1460,13 @@ int tproxy_wan_egress(struct __sk_buff *skb)
14631460
// Rewrite to control plane.
14641461

14651462
// Check outbound connectivity in specific ipversion and l4proto.
1466-
struct outbound_connectivity_query q = { 0 };
1463+
struct outbound_connectivity_query q = {
1464+
.outbound = outbound,
1465+
.ipversion = skb->protocol == bpf_htons(ETH_P_IP) ? 4 :
1466+
6,
1467+
.l4proto = l4proto
1468+
};
14671469

1468-
q.outbound = outbound;
1469-
q.ipversion = skb->protocol == bpf_htons(ETH_P_IP) ? 4 : 6;
1470-
q.l4proto = l4proto;
14711470
__u32 *alive;
14721471

14731472
alive = bpf_map_lookup_elem(&outbound_connectivity_map, &q);
@@ -1479,12 +1478,13 @@ int tproxy_wan_egress(struct __sk_buff *skb)
14791478
}
14801479

14811480
if (unlikely(tcp_state_syn)) {
1482-
struct routing_result routing_result = {};
1481+
struct routing_result routing_result = {
1482+
.outbound = outbound,
1483+
.mark = mark,
1484+
.must = must,
1485+
.dscp = tuples.dscp
1486+
};
14831487

1484-
routing_result.outbound = outbound;
1485-
routing_result.mark = mark;
1486-
routing_result.must = must;
1487-
routing_result.dscp = tuples.dscp;
14881488
__builtin_memcpy(routing_result.mac, ethh.h_source,
14891489
sizeof(ethh.h_source));
14901490
if (pid_pname) {
@@ -1546,12 +1546,13 @@ int tproxy_wan_egress(struct __sk_buff *skb)
15461546
return TC_ACT_SHOT;
15471547
}
15481548
// Construct new hdr to encap.
1549-
struct routing_result routing_result = {};
1549+
struct routing_result routing_result = {
1550+
.outbound = s64_ret,
1551+
.mark = s64_ret >> 8,
1552+
.must = (s64_ret >> 40) & 1,
1553+
.dscp = tuples.dscp
1554+
};
15501555

1551-
routing_result.outbound = s64_ret;
1552-
routing_result.mark = s64_ret >> 8;
1553-
routing_result.must = (s64_ret >> 40) & 1;
1554-
routing_result.dscp = tuples.dscp;
15551556
__builtin_memcpy(routing_result.mac, ethh.h_source,
15561557
sizeof(ethh.h_source));
15571558
if (pid_pname) {
@@ -1586,11 +1587,13 @@ int tproxy_wan_egress(struct __sk_buff *skb)
15861587
// Rewrite to control plane.
15871588

15881589
// Check outbound connectivity in specific ipversion and l4proto.
1589-
struct outbound_connectivity_query q = { 0 };
1590+
struct outbound_connectivity_query q = {
1591+
.outbound = routing_result.outbound,
1592+
.ipversion = skb->protocol == bpf_htons(ETH_P_IP) ? 4 :
1593+
6,
1594+
.l4proto = l4proto
1595+
};
15901596

1591-
q.outbound = routing_result.outbound;
1592-
q.ipversion = skb->protocol == bpf_htons(ETH_P_IP) ? 4 : 6;
1593-
q.l4proto = l4proto;
15941597
__u32 *alive;
15951598

15961599
alive = bpf_map_lookup_elem(&outbound_connectivity_map, &q);
@@ -1766,9 +1769,9 @@ static __always_inline int update_map_elem_by_cookie(const __u64 cookie)
17661769
ret = _update_map_elem_by_cookie(cookie);
17671770
if (ret) {
17681771
// Fallback to only write pid to avoid loop due to packets sent by dae.
1769-
struct pid_pname val = { 0 };
1772+
struct pid_pname val = { .pid = bpf_get_current_pid_tgid() >>
1773+
32 };
17701774

1771-
val.pid = bpf_get_current_pid_tgid() >> 32;
17721775
__u32(*pname)[TASK_COMM_LEN] =
17731776
bpf_map_lookup_elem(&tgid_pname_map, &val.pid);
17741777
if (pname) {
@@ -1844,11 +1847,12 @@ int local_tcp_sockops(struct bpf_sock_ops *skops)
18441847
if (pid == 0)
18451848
return 0;
18461849

1847-
struct tuples_key tuple = {};
1850+
struct tuples_key tuple = {
1851+
.l4proto = IPPROTO_TCP,
1852+
.sport = bpf_htonl(skops->local_port) >> 16,
1853+
.dport = skops->remote_port >> 16,
1854+
};
18481855

1849-
tuple.l4proto = IPPROTO_TCP;
1850-
tuple.sport = bpf_htonl(skops->local_port) >> 16;
1851-
tuple.dport = skops->remote_port >> 16;
18521856
if (skops->family == AF_INET) {
18531857
tuple.sip.u6_addr32[2] = bpf_htonl(0x0000ffff);
18541858
tuple.sip.u6_addr32[3] = skops->local_ip4;
@@ -1870,17 +1874,14 @@ int local_tcp_sockops(struct bpf_sock_ops *skops)
18701874
switch (skops->op) {
18711875
case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: // dae sockets
18721876
{
1873-
struct tuples_key rev_tuple = {};
1877+
struct tuples_key rev_tuple = { .l4proto = IPPROTO_TCP,
1878+
.sport = tuple.dport,
1879+
.dport = tuple.sport };
18741880

1875-
rev_tuple.l4proto = IPPROTO_TCP;
1876-
rev_tuple.sport = tuple.dport;
1877-
rev_tuple.dport = tuple.sport;
18781881
__builtin_memcpy(&rev_tuple.sip, &tuple.dip, IPV6_BYTE_LENGTH);
18791882
__builtin_memcpy(&rev_tuple.dip, &tuple.sip, IPV6_BYTE_LENGTH);
18801883

1881-
struct routing_result *routing_result;
1882-
1883-
routing_result =
1884+
struct routing_result *routing_result =
18841885
bpf_map_lookup_elem(&routing_tuples_map, &rev_tuple);
18851886
if (!routing_result || !routing_result->pid)
18861887
break;
@@ -1896,9 +1897,7 @@ int local_tcp_sockops(struct bpf_sock_ops *skops)
18961897

18971898
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: // local client sockets
18981899
{
1899-
struct routing_result *routing_result;
1900-
1901-
routing_result =
1900+
struct routing_result *routing_result =
19021901
bpf_map_lookup_elem(&routing_tuples_map, &tuple);
19031902
if (!routing_result || !routing_result->pid)
19041903
break;
@@ -1922,11 +1921,11 @@ int local_tcp_sockops(struct bpf_sock_ops *skops)
19221921
SEC("sk_msg/fast_redirect")
19231922
int sk_msg_fast_redirect(struct sk_msg_md *msg)
19241923
{
1925-
struct tuples_key rev_tuple = {};
1924+
struct tuples_key rev_tuple = { .l4proto = IPPROTO_TCP,
1925+
.sport = msg->remote_port >> 16,
1926+
.dport = bpf_htonl(msg->local_port) >>
1927+
16 };
19261928

1927-
rev_tuple.l4proto = IPPROTO_TCP;
1928-
rev_tuple.sport = msg->remote_port >> 16;
1929-
rev_tuple.dport = bpf_htonl(msg->local_port) >> 16;
19301929
if (msg->family == AF_INET) {
19311930
rev_tuple.sip.u6_addr32[2] = bpf_htonl(0x0000ffff);
19321931
rev_tuple.sip.u6_addr32[3] = msg->remote_ip4;

0 commit comments

Comments
 (0)