-
Notifications
You must be signed in to change notification settings - Fork 8.2k
TCP endpoint compare refactoring #24407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,38 +58,57 @@ static size_t tcp_endpoint_len(sa_family_t af) | |
| sizeof(struct sockaddr_in6); | ||
| } | ||
|
|
||
| static union tcp_endpoint *tcp_endpoint_new(struct net_pkt *pkt, int src) | ||
| static union tcp_endpoint *tcp_endpoint_new(struct net_pkt *pkt) | ||
| { | ||
| sa_family_t af = net_pkt_family(pkt); | ||
| union tcp_endpoint *ep = tcp_calloc(1, tcp_endpoint_len(af)); | ||
|
|
||
| ep->sa.sa_family = af; | ||
| if (ep) { | ||
| ep->sa.sa_family = af; | ||
| } | ||
|
|
||
| switch (af) { | ||
| case AF_INET: { | ||
| struct net_ipv4_hdr *ip = (struct net_ipv4_hdr *) | ||
| net_pkt_ip_data(pkt); | ||
| struct tcphdr *th = th_get(pkt); | ||
| return ep; | ||
| } | ||
|
|
||
| ep->sin.sin_port = src ? th->th_sport : th->th_dport; | ||
| static union tcp_endpoint *tcp_endpoint_set(union tcp_endpoint *ep, | ||
| struct net_pkt *pkt, | ||
| enum pkt_addr src) | ||
| { | ||
| if (!ep) { | ||
| return NULL; | ||
| } | ||
|
|
||
| ep->sin.sin_addr = src ? ip->src : ip->dst; | ||
| switch (ep->sa.sa_family) { | ||
| case AF_INET: | ||
| if (IS_ENABLED(CONFIG_NET_IPV4)) { | ||
| struct net_ipv4_hdr *ip = (struct net_ipv4_hdr *) | ||
| net_pkt_ip_data(pkt); | ||
| struct tcphdr *th = th_get(pkt); | ||
|
|
||
| ep->sin.sin_port = src == TCP_EP_SRC ? th->th_sport : | ||
| th->th_dport; | ||
| ep->sin.sin_addr = src == TCP_EP_SRC ? ip->src : | ||
| ip->dst; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| case AF_INET6: { | ||
| struct net_ipv6_hdr *ip = (struct net_ipv6_hdr *) | ||
| net_pkt_ip_data(pkt); | ||
| struct tcphdr *th = th_get(pkt); | ||
|
|
||
| ep->sin6.sin6_port = src ? th->th_sport : th->th_dport; | ||
|
|
||
| ep->sin6.sin6_addr = src ? ip->src : ip->dst; | ||
| case AF_INET6: | ||
| if (IS_ENABLED(CONFIG_NET_IPV6)) { | ||
| struct net_ipv6_hdr *ip = (struct net_ipv6_hdr *) | ||
| net_pkt_ip_data(pkt); | ||
| struct tcphdr *th = th_get(pkt); | ||
|
|
||
| ep->sin6.sin6_port = src == TCP_EP_SRC ? th->th_sport : | ||
| th->th_dport; | ||
| ep->sin6.sin6_addr = src == TCP_EP_SRC ? ip->src : | ||
| ip->dst; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| default: | ||
| NET_ERR("Unknown address family: %hu", af); | ||
| NET_ERR("Unknown address family: %hu", ep->sa.sa_family); | ||
| } | ||
|
|
||
| return ep; | ||
|
|
@@ -689,15 +708,15 @@ int net_tcp_get(struct net_context *context) | |
| } | ||
|
|
||
| static bool tcp_endpoint_cmp(union tcp_endpoint *ep, struct net_pkt *pkt, | ||
| int which) | ||
| enum pkt_addr which) | ||
| { | ||
| union tcp_endpoint *ep_new = tcp_endpoint_new(pkt, which); | ||
| bool is_equal = memcmp(ep, ep_new, tcp_endpoint_len(ep->sa.sa_family)) ? | ||
| false : true; | ||
| union tcp_endpoint ep_tmp = { 0 }, *ep_ptr; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, ep_ptr is extra here, &ep_tmp can be passed right to memcmp(). |
||
|
|
||
| ep_tmp.sa.sa_family = net_pkt_family(pkt); | ||
|
|
||
| tcp_free(ep_new); | ||
| ep_ptr = tcp_endpoint_set(&ep_tmp, pkt, which); | ||
|
|
||
| return is_equal; | ||
| return !memcmp(ep, ep_ptr, tcp_endpoint_len(ep->sa.sa_family)); | ||
| } | ||
|
|
||
| static bool tcp_conn_cmp(struct tcp *conn, struct net_pkt *pkt) | ||
|
|
@@ -789,8 +808,8 @@ static struct tcp *tcp_conn_new(struct net_pkt *pkt) | |
|
|
||
| net_context_set_family(conn->context, pkt->family); | ||
|
|
||
| conn->dst = tcp_endpoint_new(pkt, TCP_EP_SRC); | ||
| conn->src = tcp_endpoint_new(pkt, TCP_EP_DST); | ||
| conn->dst = tcp_endpoint_set(tcp_endpoint_new(pkt), pkt, TCP_EP_SRC); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per your judgement, consider the first comment (tcp_enpoint_set() -> bool), might make the error/reporting handling more clear. |
||
| conn->src = tcp_endpoint_set(tcp_endpoint_new(pkt), pkt, TCP_EP_DST); | ||
|
|
||
| NET_DBG("conn: src: %s, dst: %s", | ||
| log_strdup(tcp_endpoint_to_string(conn->src)), | ||
|
|
@@ -1259,8 +1278,10 @@ static enum net_verdict tcp_input(struct net_conn *net_conn, | |
| net_tcp_get(context); | ||
| net_context_set_family(context, pkt->family); | ||
| conn = context->tcp; | ||
| conn->dst = tcp_endpoint_new(pkt, TCP_EP_SRC); | ||
| conn->src = tcp_endpoint_new(pkt, TCP_EP_DST); | ||
| conn->dst = tcp_endpoint_set(tcp_endpoint_new(pkt), | ||
| pkt, TCP_EP_SRC); | ||
| conn->src = tcp_endpoint_set(tcp_endpoint_new(pkt), | ||
| pkt, TCP_EP_DST); | ||
| /* Make an extra reference, the sanity check suite | ||
| * will delete the connection explicitly | ||
| */ | ||
|
|
@@ -1379,8 +1400,12 @@ enum net_verdict tp_input(struct net_conn *net_conn, | |
| net_tcp_get(context); | ||
| net_context_set_family(context, pkt->family); | ||
| conn = context->tcp; | ||
| conn->dst = tcp_endpoint_new(pkt, TCP_EP_SRC); | ||
| conn->src = tcp_endpoint_new(pkt, TCP_EP_DST); | ||
| conn->dst = | ||
| tcp_endpoint_set(tcp_endpoint_new(pkt), | ||
| pkt, TCP_EP_SRC); | ||
| conn->src = | ||
| tcp_endpoint_set(tcp_endpoint_new(pkt), | ||
| pkt, TCP_EP_DST); | ||
| conn->iface = pkt->iface; | ||
| tcp_conn_ref(conn); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to change the return type to bool. This way it would be easier to do the error handling more clear and intuitive inside this function and in places of its usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not want to change the current behaviour too much. So new
tcp_endpoint_set()can be put to same place wheretcp_endpoint_new()was earlier. Actually we do not seem to handle the out-of-mem case very well in current code either. Hmm, I will send another PR that adds more error checks, will check also would it be better to change the return type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, this can be done later. These (handling out or memory and more explicit/clear error handling) were my points.
Alternative approach to consider later is just to embed endpoints into struct tcp and to drop the heap allocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be better way indeed.