Add eviction filter

This commit is contained in:
luwenpeng
2024-01-11 16:46:33 +08:00
parent 76d5fb36bb
commit 02f8d40c1e
23 changed files with 666 additions and 44 deletions

View File

@@ -94,16 +94,26 @@ static inline uint16_t ipv4_hdr_get_checksum(const struct ip *hdr)
return ntohs(hdr->ip_sum);
}
static inline uint32_t ipv4_hdr_get_src(const struct ip *hdr)
static inline uint32_t ipv4_hdr_get_host_order_saddr(const struct ip *hdr)
{
return ntohl(hdr->ip_src.s_addr);
}
static inline uint32_t ipv4_hdr_get_dst(const struct ip *hdr)
static inline uint32_t ipv4_hdr_get_host_order_daddr(const struct ip *hdr)
{
return ntohl(hdr->ip_dst.s_addr);
}
static inline struct in_addr ipv4_hdr_get_net_order_saddr(const struct ip *hdr)
{
return hdr->ip_src;
}
static inline struct in_addr ipv4_hdr_get_net_order_daddr(const struct ip *hdr)
{
return hdr->ip_dst;
}
static inline uint8_t ipv4_hdr_get_opt_len(const struct ip *hdr)
{
return ipv4_hdr_get_hl(hdr) - sizeof(struct ip);

86
src/packet/ipv6_helpers.h Normal file
View File

@@ -0,0 +1,86 @@
#ifndef _IPV6_HELPERS_H
#define _IPV6_HELPERS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <arpa/inet.h>
#include <netinet/ip6.h>
/*
* IPv6 Header Format
*
* 0 1 2 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Version| Traffic Class | Flow Label |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Payload Length | Next Header | Hop Limit |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Source Address +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* + +
* | |
* + Destination Address +
* | |
* + +
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
static inline uint8_t ipv6_hdr_get_version(const struct ip6_hdr *hdr)
{
return (ntohl(hdr->ip6_flow) & 0xf0000000) >> 28;
}
static inline uint8_t ipv6_hdr_get_traffic_class(const struct ip6_hdr *hdr)
{
return (ntohl(hdr->ip6_flow) & 0x0ff00000) >> 20;
}
static inline uint32_t ipv6_hdr_get_flow_label(const struct ip6_hdr *hdr)
{
return ntohl(hdr->ip6_flow) & 0x000fffff;
}
static inline uint16_t ipv6_hdr_get_payload_len(const struct ip6_hdr *hdr)
{
return ntohs(hdr->ip6_plen);
}
static inline uint8_t ipv6_hdr_get_next_header(const struct ip6_hdr *hdr)
{
return hdr->ip6_nxt;
}
static inline uint8_t ipv6_hdr_get_hop_limit(const struct ip6_hdr *hdr)
{
return hdr->ip6_hlim;
}
static inline struct in6_addr ipv6_hdr_get_net_order_saddr(const struct ip6_hdr *hdr)
{
return hdr->ip6_src;
}
static inline struct in6_addr ipv6_hdr_get_net_order_daddr(const struct ip6_hdr *hdr)
{
return hdr->ip6_dst;
}
// TODO IPv6 extension headers
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -34,16 +34,26 @@ extern "C"
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
static inline uint16_t tcp_hdr_get_sport(const struct tcphdr *hdr)
static inline uint16_t tcp_hdr_get_host_order_sport(const struct tcphdr *hdr)
{
return ntohs(hdr->th_sport);
}
static inline uint16_t tcp_hdr_get_dport(const struct tcphdr *hdr)
static inline uint16_t tcp_hdr_get_host_order_dport(const struct tcphdr *hdr)
{
return ntohs(hdr->th_dport);
}
static inline uint16_t tcp_hdr_get_net_order_sport(const struct tcphdr *hdr)
{
return hdr->th_sport;
}
static inline uint16_t tcp_hdr_get_net_order_dport(const struct tcphdr *hdr)
{
return hdr->th_dport;
}
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
{
return ntohl(hdr->th_seq);

View File

@@ -17,6 +17,10 @@ add_executable(gtest_ipv4_helpers gtest_ipv4_helpers.cpp)
target_include_directories(gtest_ipv4_helpers PUBLIC ${CMAKE_SOURCE_DIR}/src/packet)
target_link_libraries(gtest_ipv4_helpers gtest)
add_executable(gtest_ipv6_helpers gtest_ipv6_helpers.cpp)
target_include_directories(gtest_ipv6_helpers PUBLIC ${CMAKE_SOURCE_DIR}/src/packet)
target_link_libraries(gtest_ipv6_helpers gtest)
add_executable(gtest_packet_helpers gtest_packet_helpers.cpp)
target_link_libraries(gtest_packet_helpers packet gtest)
@@ -25,4 +29,5 @@ gtest_discover_tests(gtest_packet)
gtest_discover_tests(gtest_udp_helpers)
gtest_discover_tests(gtest_tcp_helpers)
gtest_discover_tests(gtest_ipv4_helpers)
gtest_discover_tests(gtest_ipv6_helpers)
gtest_discover_tests(gtest_packet_helpers)

View File

@@ -45,8 +45,8 @@ TEST(IPV4_HELPERS, TEST)
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
EXPECT_TRUE(ipv4_hdr_get_protocol(hdr) == 6);
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x4d8b);
EXPECT_TRUE(ipv4_hdr_get_src(hdr) == 0xc0a82467);
EXPECT_TRUE(ipv4_hdr_get_dst(hdr) == 0xc0a82889);
EXPECT_TRUE(ipv4_hdr_get_host_order_saddr(hdr) == 0xc0a82467);
EXPECT_TRUE(ipv4_hdr_get_host_order_daddr(hdr) == 0xc0a82889);
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_opt_ptr(hdr) == data + 20);
}

View File

@@ -0,0 +1,47 @@
#include <gtest/gtest.h>
#include "ipv6_helpers.h"
/*
* Internet Protocol Version 6, Src: fe80::250:56ff:fe69:dc00, Dst: ff02::1:2
* 0110 .... = Version: 6
* .... 0000 0000 .... .... .... .... .... = Traffic Class: 0x00 (DSCP: CS0, ECN: Not-ECT)
* .... 0000 00.. .... .... .... .... .... = Differentiated Services Codepoint: Default (0)
* .... .... ..00 .... .... .... .... .... = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* .... 0000 0000 0000 0000 0000 = Flow Label: 0x00000
* Payload Length: 60
* Next Header: UDP (17)
* Hop Limit: 1
* Source Address: fe80::250:56ff:fe69:dc00
* Destination Address: ff02::1:2
* [Source SLAAC MAC: VMware_69:dc:00 (00:50:56:69:dc:00)]
* */
unsigned char data[] = {
0x60, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x11, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x50, 0x56, 0xff, 0xfe, 0x69, 0xdc, 0x00, 0xff, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02};
TEST(IPV6_HELPERS, TEST)
{
char src_str[INET6_ADDRSTRLEN];
char dst_str[INET6_ADDRSTRLEN];
const struct ip6_hdr *hdr = (struct ip6_hdr *)data;
EXPECT_TRUE(ipv6_hdr_get_version(hdr) == 6);
EXPECT_TRUE(ipv6_hdr_get_traffic_class(hdr) == 0);
EXPECT_TRUE(ipv6_hdr_get_flow_label(hdr) == 0);
EXPECT_TRUE(ipv6_hdr_get_payload_len(hdr) == 60);
EXPECT_TRUE(ipv6_hdr_get_next_header(hdr) == 17);
EXPECT_TRUE(ipv6_hdr_get_hop_limit(hdr) == 1);
struct in6_addr src_addr = ipv6_hdr_get_net_order_saddr(hdr);
struct in6_addr dst_addr = ipv6_hdr_get_net_order_daddr(hdr);
inet_ntop(AF_INET6, &src_addr, src_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &dst_addr, dst_str, INET6_ADDRSTRLEN);
EXPECT_TRUE(strcmp(src_str, "fe80::250:56ff:fe69:dc00") == 0);
EXPECT_TRUE(strcmp(dst_str, "ff02::1:2") == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -77,8 +77,8 @@ unsigned char data[] = {
TEST(TCP_HELPERS, TEST)
{
const struct tcphdr *hdr = (struct tcphdr *)data;
EXPECT_TRUE(tcp_hdr_get_sport(hdr) == 55555);
EXPECT_TRUE(tcp_hdr_get_dport(hdr) == 40856);
EXPECT_TRUE(tcp_hdr_get_host_order_sport(hdr) == 55555);
EXPECT_TRUE(tcp_hdr_get_host_order_dport(hdr) == 40856);
EXPECT_TRUE(tcp_hdr_get_seq(hdr) == 3965699644);
EXPECT_TRUE(tcp_hdr_get_ack(hdr) == 991053714);
EXPECT_TRUE(tcp_hdr_get_doff(hdr) == 40);

View File

@@ -17,8 +17,8 @@ unsigned char data[] = {0x0f, 0xa1, 0x1f, 0x40, 0x00, 0x9b, 0x1e, 0x1e};
TEST(UDP_HELPERS, TEST)
{
const struct udphdr *hdr = (struct udphdr *)data;
EXPECT_TRUE(udp_hdr_get_sport(hdr) == 4001);
EXPECT_TRUE(udp_hdr_get_dport(hdr) == 8000);
EXPECT_TRUE(udp_hdr_get_host_order_sport(hdr) == 4001);
EXPECT_TRUE(udp_hdr_get_host_order_dport(hdr) == 8000);
EXPECT_TRUE(udp_hdr_get_len(hdr) == 155);
EXPECT_TRUE(udp_hdr_get_checksum(hdr) == 0x1e1e);
}

View File

@@ -24,16 +24,26 @@ extern "C"
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
static inline uint16_t udp_hdr_get_sport(const struct udphdr *hdr)
static inline uint16_t udp_hdr_get_host_order_sport(const struct udphdr *hdr)
{
return ntohs(hdr->uh_sport);
}
static inline uint16_t udp_hdr_get_dport(const struct udphdr *hdr)
static inline uint16_t udp_hdr_get_host_order_dport(const struct udphdr *hdr)
{
return ntohs(hdr->uh_dport);
}
static inline uint16_t udp_hdr_get_net_order_sport(const struct udphdr *hdr)
{
return hdr->uh_sport;
}
static inline uint16_t udp_hdr_get_net_order_dport(const struct udphdr *hdr)
{
return hdr->uh_dport;
}
static inline uint16_t udp_hdr_get_len(const struct udphdr *hdr)
{
return ntohs(hdr->uh_ulen);