-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement decap_packet lib function
- Loading branch information
Showing
6 changed files
with
127 additions
and
78 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,105 @@ | ||
#include <netinet/in.h> | ||
#include <rte_ether.h> | ||
#include <rte_gre.h> | ||
|
||
#include "decap.h" | ||
#include "packet.h" | ||
|
||
#include <string.h> | ||
// (first u32 + count of optional u32) * 4 | ||
#define decap_gre_header_size(byte0) \ | ||
(1 + __builtin_popcount((byte0) & 0xb0)) << 2 | ||
|
||
#include <rte_ether.h> | ||
/// GRE Header: | ||
/// 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 | ||
/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
/// |C| |K|S| Reserved0 | Ver | Protocol Type | | ||
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
/// | Checksum (optional) | Reserved1 (Optional) | | ||
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
/// | Key (optional) | | ||
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
/// | Sequence Number (optional) | | ||
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
/// https://datatracker.ietf.org/doc/html/rfc2890#section-2 | ||
static int | ||
packet_skip_gre( | ||
struct packet *packet, | ||
uint16_t *type, | ||
uint16_t *offset, | ||
uint16_t *next_proto | ||
) { | ||
struct rte_gre_hdr *gre_hdr = rte_pktmbuf_mtod_offset( | ||
packet->mbuf, | ||
struct rte_gre_hdr *, | ||
packet->transport_header.offset | ||
); | ||
*next_proto = gre_hdr->proto; | ||
if (gre_hdr->proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) { | ||
*type = IPPROTO_IPIP; | ||
} else if (gre_hdr->proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) { | ||
*type = IPPROTO_IPV6; | ||
} else { // error case | ||
return -1; | ||
} | ||
*offset += decap_gre_header_size(*(uint8_t *)gre_hdr); | ||
return 0; | ||
} | ||
|
||
int | ||
packet_decap(struct packet *packet) { | ||
(void)packet; | ||
|
||
/* | ||
if (packet->network_header.type == | ||
rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) { | ||
struct rte_ipv4_hdr *ipv4Header = | ||
rte_pktmbuf_mtod_offset( mbuf, struct rte_ipv4_hdr *, | ||
packet->network_header.offset | ||
); | ||
if (ipv4Header->proto = IPPROTO_IPIP) { | ||
} | ||
} else { | ||
uint16_t next_transport = packet->transport_header.type; | ||
uint16_t next_offset = packet->transport_header.offset; | ||
uint16_t next_ether_type; | ||
|
||
if (next_transport == IPPROTO_GRE) { | ||
if (packet_skip_gre( | ||
packet, | ||
&next_transport, | ||
&next_offset, | ||
&next_ether_type | ||
)) { | ||
return -1; | ||
} | ||
*/ | ||
} | ||
uint16_t tun_hdrs_size = next_offset - packet->network_header.offset; | ||
|
||
if (next_transport == IPPROTO_IPIP) { | ||
if (parse_ipv4_header(packet, &next_transport, &next_offset)) { | ||
return -1; | ||
} | ||
} else if (next_transport == IPPROTO_IPV6) { | ||
if (parse_ipv6_header(packet, &next_transport, &next_offset)) { | ||
return -1; | ||
} | ||
} else { | ||
// unknown tunnel | ||
return -1; | ||
} | ||
|
||
struct rte_mbuf *mbuf = packet_to_mbuf(packet); | ||
|
||
// Remove tunnel headers | ||
uint8_t *prev_start = rte_pktmbuf_mtod(mbuf, uint8_t *); | ||
|
||
// FIXME: separate strip header function | ||
// FIXME: handle rte_pktmbuf_adj() == NULL | ||
/* Copy ether header over rather than moving whole packet */ | ||
memmove(rte_pktmbuf_adj(mbuf, tun_hdrs_size), | ||
prev_start, | ||
// size of eth_hdr plus optional vlans | ||
packet->network_header.offset); | ||
|
||
// Update ether type | ||
uint16_t *prev_eth_type = rte_pktmbuf_mtod_offset( | ||
mbuf, | ||
uint16_t *, | ||
packet->network_header.offset - sizeof(uint16_t) | ||
); | ||
*prev_eth_type = next_ether_type; | ||
// and transport_header meta | ||
packet->transport_header.type = next_transport; | ||
packet->transport_header.offset = next_offset - tun_hdrs_size; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters