net/eth: Remove duplicated tcp/udp_hdr structures
cover net/eth: Remove duplicated tcp/udp_hdr structures
This is a preparatory cleanup series.
Commit 75020a70215 introduced 4 very equivalent structures:
- tcp_header and tcp_hdr,
- udp_header and udp_hdr.
Choose the most widely use in the codebase, which happens to provide convenient bitfields manipulation macros and is not endian-specific.
Philippe Mathieu-Daudé (6):
hw/net/virtio-net: Use TCP_HEADER_FLAGS/TCP_HEADER_DATA_OFFSET macros net/colo-compare: Use the tcp_header structure net/filter-rewriter: Use the tcp_header structure hw/net/vmxnet3: Use the tcp_header structure
net/eth: Remove the unused tcp_hdr structure
net/eth: Remove the single use of udp_hdr structure
hw/net/net_rx_pkt.c | 2 +- hw/net/net_tx_pkt.c | 9 +++++---- hw/net/virtio-net.c | 9 ++------- include/net/eth.h | 47 ++++++++----------------------------------- net/colo-compare.c | 14 ++++++------- net/filter-rewriter.c | 37 +++++++++++++++++++--------------- 6 files changed, 44 insertions(+), 74 deletions(-)
Signature · 1 line
2.20.1
RFC PATCH 1/6 hw/net/virtio-net: Use TCP_HEADER_FLAGS/TCP_HEADER_DATA_OFFSET macros
"net/eth.h" provides convenient macros to avoid manipulating the TCP header flags/offset bits manually, let's use them.
---
RFC: Check the macro uses the correct bits
hw/net/virtio-net.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
hw/net/virtio-net.c +2 -7
@@ -46,9 +46,6 @@#define VIRTIO_NET_IP4_ADDR_SIZE 8/* ipv4 saddr + daddr */ -#define VIRTIO_NET_TCP_FLAG 0x3F -#define VIRTIO_NET_TCP_HDR_LENGTH 0xF000 -/* IPv4 max payload, 16 bits in the header */ #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))#define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
@@ -1658,10 +1655,8 @@ static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,uint16_t tcp_hdr;uint16_t tcp_flag; - tcp_flag =htons (tcp->th_offset_flags); - tcp_hdr = (tcp_flag &VIRTIO_NET_TCP_HDR_LENGTH ) >>10 ; - tcp_flag &=VIRTIO_NET_TCP_FLAG ; - tcp_flag =htons (tcp->th_offset_flags) &0x3F ; + tcp_hdr =TCP_HEADER_DATA_OFFSET (tcp); + tcp_flag =TCP_HEADER_FLAGS (tcp);if (tcp_flag &TH_SYN ) { chain->stat.tcp_syn++;return RSC_BYPASS ;
Signature · 1 line
2.20.1
RFC PATCH 2/6 net/colo-compare: Use the tcp_header structure
In reply to Philippe Mathieu-DaudéThe tcp_header structure comes convenient macros to avoid manipulating the TCP header flags/offset bits manually. Replace the tcp_hdr structure by the tcp_header equivalent, and use the macros.
---
RFC: Verify th_off endianess
net/colo-compare.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
net/colo-compare.c +7 -7
@@ -161,28 +161,28 @@ static void colo_compare_inconsistency_notify(CompareState *s)static gint seq_sorter (Packet *a,Packet *b,gpointer data) { -struct tcp_hdr *atcp, *btcp; +struct tcp_header *atcp, *btcp; - atcp = (struct tcp_hdr *)(a->transport_header); - btcp = (struct tcp_hdr *)(b->transport_header); + atcp = (struct tcp_header *)(a->transport_header); + btcp = (struct tcp_header *)(b->transport_header);return ntohl (atcp->th_seq) -ntohl (btcp->th_seq); }static void fill_pkt_tcp_info (void *data,uint32_t *max_ack) {Packet *pkt = data; -struct tcp_hdr *tcphd; +struct tcp_header *tcphd; - tcphd = (struct tcp_hdr *)pkt->transport_header; + tcphd = (struct tcp_header *)pkt->transport_header; pkt->tcp_seq =ntohl (tcphd->th_seq); pkt->tcp_ack =ntohl (tcphd->th_ack); *max_ack = *max_ack > pkt->tcp_ack ? *max_ack : pkt->tcp_ack; pkt->header_size = pkt->transport_header - (uint8_t *)pkt->data - + (tcphd->th_off <<2 ) - pkt->vnet_hdr_len; + +TCP_HEADER_DATA_OFFSET (tcphd) - pkt->vnet_hdr_len; pkt->payload_size = pkt->size - pkt->header_size; pkt->seq_end = pkt->tcp_seq + pkt->payload_size; - pkt->flags = tcphd->th_flags; + pkt->flags =TCP_HEADER_FLAGS (tcphd); }/*
Signature · 1 line
2.20.1
PATCH 3/6 net/filter-rewriter: Use the tcp_header structure
In reply to Philippe Mathieu-DaudéThe tcp_header structure comes convenient macros to avoid manipulating the TCP header flags/offset bits manually. Replace the tcp_hdr structure by the tcp_header equivalent, and use the TCP_HEADER_FLAGS macro.
--- net/filter-rewriter.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
net/filter-rewriter.c +21 -16
@@ -73,23 +73,26 @@ static int handle_primary_tcp_pkt(RewriterState *rf, Connection *conn, Packet *pkt, ConnectionKey *key) { -struct tcp_hdr *tcp_pkt; +struct tcp_header *tcp_pkt; +uint8_t tcp_flags; + + tcp_pkt = (struct tcp_header *)pkt->transport_header; + tcp_flags =TCP_HEADER_FLAGS (tcp_pkt); - tcp_pkt = (struct tcp_hdr *)pkt->transport_header;if (trace_event_get_state_backends (TRACE_COLO_FILTER_REWRITER_DEBUG )) {trace_colo_filter_rewriter_pkt_info (__func__,inet_ntoa (pkt->ip->ip_src),inet_ntoa (pkt->ip->ip_dst),ntohl (tcp_pkt->th_seq),ntohl (tcp_pkt->th_ack), - tcp_pkt->th_flags); + tcp_flags);trace_colo_filter_rewriter_conn_offset (conn->offset); } -if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) == (TH_ACK |TH_SYN )) && +if (((tcp_flags & (TH_ACK |TH_SYN )) == (TH_ACK |TH_SYN )) && conn->tcp_state ==TCPS_SYN_SENT ) { conn->tcp_state =TCPS_ESTABLISHED ; } -if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_SYN )) { +if (((tcp_flags & (TH_ACK |TH_SYN )) ==TH_SYN )) {/* * we use this flag update offset func * run once in independent tcp connection
@@ -97,7 +100,7 @@ static int handle_primary_tcp_pkt(RewriterState *rf, conn->tcp_state =TCPS_SYN_RECEIVED ; } -if (((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_ACK )) { +if (((tcp_flags & (TH_ACK |TH_SYN )) ==TH_ACK )) {if (conn->tcp_state ==TCPS_SYN_RECEIVED ) {/* * offset = secondary_seq - primary seq
@@ -119,13 +122,13 @@ static int handle_primary_tcp_pkt(RewriterState *rf,* Passive close step 3 */ if ((conn->tcp_state ==TCPS_LAST_ACK ) && - (ntohl (tcp_pkt->th_ack) == (conn->fin_ack_seq +1 ))) { + (ldl_be_p (&tcp_pkt->th_ack) == (conn->fin_ack_seq +1 ))) { conn->tcp_state =TCPS_CLOSED ;g_hash_table_remove (rf->connection_track_table, key); } } -if ((tcp_pkt->th_flags &TH_FIN ) ==TH_FIN ) { +if ((tcp_flags &TH_FIN ) ==TH_FIN ) {/* * Passive close. * Step 1:
@@ -176,20 +179,22 @@ static int handle_secondary_tcp_pkt(RewriterState *rf, Connection *conn, Packet *pkt, ConnectionKey *key) { -struct tcp_hdr *tcp_pkt; +struct tcp_header *tcp_pkt; +uint8_t tcp_flags; - tcp_pkt = (struct tcp_hdr *)pkt->transport_header; + tcp_pkt = (struct tcp_header *)pkt->transport_header; + tcp_flags =TCP_HEADER_FLAGS (tcp_pkt);if (trace_event_get_state_backends (TRACE_COLO_FILTER_REWRITER_DEBUG )) {trace_colo_filter_rewriter_pkt_info (__func__,inet_ntoa (pkt->ip->ip_src),inet_ntoa (pkt->ip->ip_dst),ntohl (tcp_pkt->th_seq),ntohl (tcp_pkt->th_ack), - tcp_pkt->th_flags); + tcp_flags);trace_colo_filter_rewriter_conn_offset (conn->offset); }if (conn->tcp_state ==TCPS_SYN_RECEIVED && - ((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) == (TH_ACK |TH_SYN ))) { + ((tcp_flags & (TH_ACK |TH_SYN )) == (TH_ACK |TH_SYN ))) {/* * save offset = secondary_seq and then * in handle_primary_tcp_pkt make offset
@@ -200,11 +205,11 @@ static int handle_secondary_tcp_pkt(RewriterState *rf,/* VM active connect */ if (conn->tcp_state ==TCPS_CLOSED && - ((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_SYN )) { + ((tcp_flags & (TH_ACK |TH_SYN )) ==TH_SYN )) { conn->tcp_state =TCPS_SYN_SENT ; } -if ((tcp_pkt->th_flags & (TH_ACK |TH_SYN )) ==TH_ACK ) { +if ((tcp_flags & (TH_ACK |TH_SYN )) ==TH_ACK ) {/* Only need to adjust seq while offset is Non-zero */ if (conn->offset) {/* handle packets to the primary from the secondary*/
@@ -219,7 +224,7 @@ static int handle_secondary_tcp_pkt(RewriterState *rf,* Passive close step 2: */ if (conn->tcp_state ==TCPS_CLOSE_WAIT && - (tcp_pkt->th_flags & (TH_ACK |TH_FIN )) == (TH_ACK |TH_FIN )) { + (tcp_flags & (TH_ACK |TH_FIN )) == (TH_ACK |TH_FIN )) { conn->fin_ack_seq =ntohl (tcp_pkt->th_seq); conn->tcp_state =TCPS_LAST_ACK ; }
@@ -237,7 +242,7 @@ static int handle_secondary_tcp_pkt(RewriterState *rf,* CLOSING status. */ if (conn->tcp_state ==TCPS_ESTABLISHED && - (tcp_pkt->th_flags & (TH_ACK |TH_FIN )) ==TH_FIN ) { + (tcp_flags & (TH_ACK |TH_FIN )) ==TH_FIN ) { conn->tcp_state =TCPS_FIN_WAIT_1 ; }
Signature · 1 line
2.20.1
RFC PATCH 4/6 hw/net/vmxnet3: Use the tcp_header structure
In reply to Philippe Mathieu-DaudéThe tcp_header structure comes convenient macros to avoid manipulating the TCP header flags/offset bits manually. Replace the tcp_hdr structure by the tcp_header equivalent, and use the macros.
Since we will remove the duplicated TCP_FLAG_ACK definition in the next commit, replace its use now.
---
RFC: Verify th_off endianess
hw/net/net_rx_pkt.c | 2 +- hw/net/net_tx_pkt.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-)
hw/net/net_rx_pkt.c +1 -1
@@ -376,7 +376,7 @@ bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt)assert (pkt);if (pkt->istcp) { -return TCP_HEADER_FLAGS (&pkt->l4hdr_info.hdr.tcp) &TCP_FLAG_ACK ; +return TCP_HEADER_FLAGS (&pkt->l4hdr_info.hdr.tcp) &TH_ACK ; }return false;
hw/net/net_tx_pkt.c +4 -3
@@ -307,7 +307,7 @@ func_exit:void net_tx_pkt_build_vheader (struct NetTxPkt *pkt,bool tso_enable,bool csum_enable,uint32_t gso_size) { -struct tcp_hdr l4hdr; +struct tcp_header l4hdr;assert (pkt);/* csum has to be enabled if tso is. */
@@ -330,7 +330,8 @@ void net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable,case VIRTIO_NET_HDR_GSO_TCPV6 :iov_to_buf (&pkt->vec[NET_TX_PKT_PL_START_FRAG ], pkt->payload_frags,0 , &l4hdr,sizeof (l4hdr)); - pkt->virt_hdr.hdr_len = pkt->hdr_len + l4hdr.th_off *sizeof (uint32_t ); + pkt->virt_hdr.hdr_len = pkt->hdr_len + +TCP_HEADER_DATA_OFFSET (&l4hdr) *sizeof (uint32_t ); pkt->virt_hdr.gso_size = gso_size;break ;
@@ -343,7 +344,7 @@ void net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable,case IP_PROTO_TCP : pkt->virt_hdr.flags =VIRTIO_NET_HDR_F_NEEDS_CSUM ; pkt->virt_hdr.csum_start = pkt->hdr_len; - pkt->virt_hdr.csum_offset = offsetof(struct tcp_hdr , th_sum); + pkt->virt_hdr.csum_offset = offsetof(struct tcp_header , th_sum);break ;case IP_PROTO_UDP : pkt->virt_hdr.flags =VIRTIO_NET_HDR_F_NEEDS_CSUM ;
Signature · 1 line
2.20.1
PATCH 5/6 net/eth: Remove the unused tcp_hdr structure
In reply to Philippe Mathieu-DaudéCommit 75020a70215 introduced 2 very similar structures:tcp_header and tcp_hdr.
We replaced the uses of struct tcp_hdr with the equivalent struct tcp_header. Remove the unused one.
---
I prefer the TCP_FLAG_XXX name, but there is only one single use of TCP_FLAG_ACK vs many use of the other set, so I'm keeping the set.
include/net/eth.h | 40 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-)
include/net/eth.h +8 -32
@@ -72,7 +72,14 @@ typedef struct tcp_header {#define TCP_HEADER_FLAGS (tcp) \ TCP_FLAGS_ONLY(be16_to_cpu((tcp)->th_offset_flags)) -#define TCP_FLAG_ACK 0x10 +#define TH_FIN 0x01 +#define TH_SYN 0x02 +#define TH_RST 0x04 +#define TH_PUSH 0x08 +#define TH_ACK 0x10 +#define TH_URG 0x20 +#define TH_ECE 0x40 +#define TH_CWR 0x80#define TCP_HEADER_DATA_OFFSET (tcp) \ (((be16_to_cpu((tcp)->th_offset_flags) >> 12) & 0xf) << 2)
@@ -153,37 +160,6 @@ struct udp_hdr {uint16_t uh_sum;/* udp checksum */ }; -struct tcp_hdr { -u_short th_sport;/* source port */ -u_short th_dport;/* destination port */ -uint32_t th_seq;/* sequence number */ -uint32_t th_ack;/* acknowledgment number */ -#ifdef HOST_WORDS_BIGENDIAN -u_char th_off :4 ,/* data offset */ - th_x2:4 ;/* (unused) */ -#else -u_char th_x2 :4 ,/* (unused) */ - th_off:4 ;/* data offset */ -#endif - -#define TH_ELN 0x1/* explicit loss notification */ -#define TH_ECN 0x2/* explicit congestion notification */ -#define TH_FS 0x4/* fast start */ - -u_char th_flags; -#define TH_FIN 0x01 -#define TH_SYN 0x02 -#define TH_RST 0x04 -#define TH_PUSH 0x08 -#define TH_ACK 0x10 -#define TH_URG 0x20 -#define TH_ECE 0x40 -#define TH_CWR 0x80 -u_short th_win;/* window */ -u_short th_sum;/* checksum */ -u_short th_urp;/* urgent pointer */ -}; -#define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt#define ip6_ecn_acc ip6_ctlun.ip6_un3.ip6_un3_ecn
Signature · 1 line
2.20.1
PATCH 6/6 net/eth: Remove the single use of udp_hdr structure
In reply to Philippe Mathieu-DaudéCommit 75020a70215 introduced 2 very equivalent structures:udp_header and udp_hdr.
Replace the single use of udp_hdr by udp_header (which has few more uses in the codebase) and remove the now unused structure.
--- hw/net/net_tx_pkt.c | 2 +- include/net/eth.h | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-)
hw/net/net_tx_pkt.c +1 -1
@@ -349,7 +349,7 @@ void net_tx_pkt_build_vheader(struct NetTxPkt *pkt, bool tso_enable,case IP_PROTO_UDP : pkt->virt_hdr.flags =VIRTIO_NET_HDR_F_NEEDS_CSUM ; pkt->virt_hdr.csum_start = pkt->hdr_len; - pkt->virt_hdr.csum_offset = offsetof(struct udp_hdr , uh_sum); + pkt->virt_hdr.csum_offset = offsetof(struct udp_header , uh_sum);break ;default :break ;
include/net/eth.h +0 -7
@@ -153,13 +153,6 @@ struct ip6_option_hdr {uint8_t len; }; -struct udp_hdr { -uint16_t uh_sport;/* source port */ -uint16_t uh_dport;/* destination port */ -uint16_t uh_ulen;/* udp length */ -uint16_t uh_sum;/* udp checksum */ -}; -#define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt#define ip6_ecn_acc ip6_ctlun.ip6_un3.ip6_un3_ecn
Signature · 1 line
2.20.1
Quoted 31 lines · Philippe Mathieu-Daudé
This is a preparatory cleanup series.
Commit
75020a70215introduced 4 very equivalent structures:
-tcp_headerandtcp_hdr,
-udp_headerandudp_hdr.Choose the most widely use in the codebase, which happens to provide convenient bitfields manipulation macros and is not endian-specific.
Philippe Mathieu-Daudé (6):
hw/net/virtio-net: Use TCP_HEADER_FLAGS/TCP_HEADER_DATA_OFFSET macrosnet/colo-compare: Use thetcp_headerstructurenet/filter-rewriter: Use thetcp_headerstructure hw/net/vmxnet3: Use thetcp_headerstructure
net/eth: Remove the unusedtcp_hdrstructure
net/eth: Remove the single use ofudp_hdrstructurehw/net/net_rx_pkt.c | 2 +- hw/net/net_tx_pkt.c | 9 +++++---- hw/net/virtio-net.c | 9 ++------- include/net/eth.h | 47 ++++++++----------------------------------- net/colo-compare.c | 14 ++++++------- net/filter-rewriter.c | 37 +++++++++++++++++++--------------- 6 files changed, 44 insertions(+), 74 deletions(-)Signature · 1 line
2.20.1
Hi Jason,
Quoted 10 lines · Philippe Mathieu-Daudé
This is a preparatory cleanup series.
Commit
75020a70215introduced 4 very equivalent structures:
-tcp_headerandtcp_hdr,
-udp_headerandudp_hdr.Choose the most widely use in the codebase, which happens to provide convenient bitfields manipulation macros and is not endian-specific.
Quoted 7 lines
Philippe Mathieu-Daudé (6):
hw/net/virtio-net: Use TCP_HEADER_FLAGS/TCP_HEADER_DATA_OFFSET macrosnet/colo-compare: Use thetcp_headerstructurenet/filter-rewriter: Use thetcp_headerstructure hw/net/vmxnet3: Use thetcp_headerstructure
net/eth: Remove the unusedtcp_hdrstructure
net/eth: Remove the single use ofudp_hdrstructure
Are you OK to take this series?
It got reviewed by Dmitry Fleytman.
Thanks,
Phil.
Quoted 20 lines · Philippe Mathieu-Daudé
Hi Jason,
Quoted 10 lines · Philippe Mathieu-Daudé
This is a preparatory cleanup series.
Commit
75020a70215introduced 4 very equivalent structures:
-tcp_headerandtcp_hdr,
-udp_headerandudp_hdr.Choose the most widely use in the codebase, which happens to provide convenient bitfields manipulation macros and is not endian-specific.
Quoted 7 lines
Philippe Mathieu-Daudé (6):
hw/net/virtio-net: Use TCP_HEADER_FLAGS/TCP_HEADER_DATA_OFFSET macrosnet/colo-compare: Use thetcp_headerstructurenet/filter-rewriter: Use thetcp_headerstructure hw/net/vmxnet3: Use thetcp_headerstructure
net/eth: Remove the unusedtcp_hdrstructure
net/eth: Remove the single use ofudp_hdrstructure
Quoted 7 lines
Are you OK to take this series?
It got reviewed by Dmitry Fleytman.
Thanks,
Phil.
Yes. Applied.
Thanks