Add support for parsing PW Ethernet and Enhanced MPLS parsing

This commit is contained in:
luwenpeng
2024-06-02 00:07:33 +08:00
parent f3b92a8a15
commit 18fe1e2e41
13 changed files with 615 additions and 250 deletions

View File

@@ -13,6 +13,12 @@ target_link_libraries(gtest_ipv4_utils packet gtest)
add_executable(gtest_ipv6_utils gtest_ipv6_utils.cpp)
target_link_libraries(gtest_ipv6_utils packet gtest)
add_executable(gtest_mpls_utils gtest_mpls_utils.cpp)
target_link_libraries(gtest_mpls_utils packet gtest)
add_executable(gtest_eth_utils gtest_eth_utils.cpp)
target_link_libraries(gtest_eth_utils packet gtest)
add_executable(gtest_packet_frag gtest_packet_frag.cpp)
target_link_libraries(gtest_packet_frag packet gtest)
@@ -22,4 +28,6 @@ gtest_discover_tests(gtest_udp_utils)
gtest_discover_tests(gtest_tcp_utils)
gtest_discover_tests(gtest_ipv4_utils)
gtest_discover_tests(gtest_ipv6_utils)
gtest_discover_tests(gtest_mpls_utils)
gtest_discover_tests(gtest_eth_utils)
gtest_discover_tests(gtest_packet_frag)

View File

@@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include "eth_utils.h"
/*
* Ethernet II, Src: 00:00:00_00:04:36 (00:00:00:00:04:36), Dst: 18:10:04:00:03:1f (18:10:04:00:03:1f)
* Destination: 18:10:04:00:03:1f (18:10:04:00:03:1f)
* Address: 18:10:04:00:03:1f (18:10:04:00:03:1f)
* .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
* .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
* Source: 00:00:00_00:04:36 (00:00:00:00:04:36)
* Address: 00:00:00_00:04:36 (00:00:00:00:04:36)
* .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
* .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
* Type: MPLS label switched packet (0x8847)
*/
unsigned char data[] = {
0x18, 0x10, 0x04, 0x00, 0x03, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x04, 0x36, 0x88, 0x47};
TEST(ETH_UTILS, GET)
{
const struct ethhdr *hdr = (struct ethhdr *)data;
char dest[18] = {0};
char source[18] = {0};
EXPECT_TRUE(eth_hdr_get_dest(hdr, dest, sizeof(dest)) == 17);
EXPECT_TRUE(eth_hdr_get_source(hdr, source, sizeof(source)) == 17);
EXPECT_TRUE(memcmp(dest, "18:10:04:00:03:1f", 17) == 0);
EXPECT_TRUE(memcmp(source, "00:00:00:00:04:36", 17) == 0);
EXPECT_TRUE(eth_hdr_get_proto(hdr) == ETH_P_MPLS_UC);
}
TEST(ETH_UTILS, SET)
{
char buff[14] = {0};
struct ethhdr *hdr = (struct ethhdr *)buff;
eth_hdr_set_dest(hdr, "18:10:04:00:03:1f");
eth_hdr_set_source(hdr, "00:00:00:00:04:36");
eth_hdr_set_proto(hdr, ETH_P_MPLS_UC);
EXPECT_TRUE(memcmp(buff, data, 14) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,42 @@
#include <gtest/gtest.h>
#include "mpls_utils.h"
/*
* MultiProtocol Label Switching Header, Label: 779408, Exp: 0, S: 0, TTL: 255
* 1011 1110 0100 1001 0000 .... .... .... = MPLS Label: 779408 (0xbe490)
* .... .... .... .... .... 000. .... .... = MPLS Experimental Bits: 0
* .... .... .... .... .... ...0 .... .... = MPLS Bottom Of Label Stack: 0
* .... .... .... .... .... .... 1111 1111 = MPLS TTL: 255
*/
unsigned char data[] = {
0xbe, 0x49, 0x00, 0xff};
TEST(MPLS_UTILS, GET)
{
const struct mpls_hdr *hdr = (struct mpls_hdr *)data;
EXPECT_TRUE(mpls_hdr_get_label(hdr) == 0xbe490);
EXPECT_TRUE(mpls_hdr_get_exp(hdr) == 0);
EXPECT_TRUE(mpls_hdr_get_bos(hdr) == 0);
EXPECT_TRUE(mpls_hdr_get_ttl(hdr) == 255);
}
TEST(MPLS_UTILS, SET)
{
char buff[4] = {0};
struct mpls_hdr *hdr = (struct mpls_hdr *)buff;
mpls_hdr_set_label(hdr, 0xbe490);
mpls_hdr_set_exp(hdr, 0);
mpls_hdr_set_bos(hdr, 0);
mpls_hdr_set_ttl(hdr, 255);
EXPECT_TRUE(memcmp(buff, data, 4) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -2628,8 +2628,8 @@ TEST(PACKET, ETH_MPLS_MPLS_PWETHCW_ETH_ARP)
EXPECT_TRUE(inner_mpls_record != nullptr);
EXPECT_TRUE(inner_mpls_record->hdr_offset == 18);
EXPECT_TRUE(inner_mpls_record->hdr_len == 4 + 4); // MPLS + PWETH
EXPECT_TRUE(inner_mpls_record->pld_len == 64);
EXPECT_TRUE(inner_mpls_record->hdr_len == 4);
EXPECT_TRUE(inner_mpls_record->pld_len == 68);
// LAYER_TYPE_L2_TUN
const struct packet_layer *inner_l2_tun_record = packet_get_innermost_layer(&handler, LAYER_TYPE_L2_TUN);
@@ -2637,6 +2637,14 @@ TEST(PACKET, ETH_MPLS_MPLS_PWETHCW_ETH_ARP)
EXPECT_TRUE(inner_l2_tun_record != nullptr);
EXPECT_TRUE(inner_l2_tun_record == inner_mpls_record);
// LAYER_TYPE_PWETHCW
const struct packet_layer *inner_pweth_record = packet_get_innermost_layer(&handler, LAYER_TYPE_PWETH);
EXPECT_TRUE(inner_pweth_record != nullptr);
EXPECT_TRUE(inner_pweth_record->hdr_offset == 22);
EXPECT_TRUE(inner_pweth_record->hdr_len == 4);
EXPECT_TRUE(inner_pweth_record->pld_len == 64);
// LAYER_TYPE_ETHER
const struct packet_layer *inner_eth_record = packet_get_innermost_layer(&handler, LAYER_TYPE_ETHER);