Refactoring the MPLS utils

This commit is contained in:
luwenpeng
2024-06-05 10:39:57 +08:00
parent ade7b4c8ab
commit a1e693a735
4 changed files with 36 additions and 51 deletions

View File

@@ -9,7 +9,7 @@ extern "C"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ether.h> #include <linux/if_ether.h>
/****************************************************************************** /******************************************************************************
* get * get

View File

@@ -9,93 +9,79 @@ extern "C"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <linux/mpls.h>
#define MPLS_LABEL_MASK 0xFFFFF000
#define MPLS_EXP_MASK 0x00000E00
#define MPLS_STACK_MASK 0x00000100
#define MPLS_TTL_MASK 0x000000FF
#define MPLS_LABEL_SHIFT 12
#define MPLS_EXP_SHIFT 9
#define MPLS_STACK_SHIFT 8
#define MPLS_TTL_SHIFT 0
/* Reference: RFC 5462, RFC 3032 /* Reference: RFC 5462, RFC 3032
* *
* 0 1 2 3 * 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 * 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Label | EXP |S| TTL | * | Label | TC |S| TTL |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* *
* Label: Label Value, 20 bits * Label: Label Value, 20 bits
* EXP: Experimental, 3 bits * TC: Traffic Class, 3 bits
* S: Bottom of Stack, 1 bit * S: Bottom of Stack, 1 bit
* TTL: Time to Live, 8 bits * TTL: Time to Live, 8 bits
*/ */
struct mpls_hdr
{
uint32_t entry;
};
/****************************************************************************** /******************************************************************************
* get * get
******************************************************************************/ ******************************************************************************/
static inline uint32_t mpls_hdr_get_label(const struct mpls_hdr *hdr) static inline uint32_t mpls_label_get_label(const struct mpls_label *hdr)
{ {
return ((ntohl(hdr->entry) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT); return ((ntohl(hdr->entry) & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT);
} }
static inline uint8_t mpls_hdr_get_exp(const struct mpls_hdr *hdr) static inline uint8_t mpls_label_get_tc(const struct mpls_label *hdr)
{ {
return ((ntohl(hdr->entry) & MPLS_EXP_MASK) >> MPLS_EXP_SHIFT); return ((ntohl(hdr->entry) & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT);
} }
static inline uint8_t mpls_hdr_get_bos(const struct mpls_hdr *hdr) static inline uint8_t mpls_label_get_bos(const struct mpls_label *hdr)
{ {
return ((ntohl(hdr->entry) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT); return ((ntohl(hdr->entry) & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT);
} }
static inline uint8_t mpls_hdr_get_ttl(const struct mpls_hdr *hdr) static inline uint8_t mpls_label_get_ttl(const struct mpls_label *hdr)
{ {
return ((ntohl(hdr->entry) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT); return ((ntohl(hdr->entry) & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT);
} }
/****************************************************************************** /******************************************************************************
* set * set
******************************************************************************/ ******************************************************************************/
static inline void mpls_hdr_set_label(struct mpls_hdr *hdr, uint32_t label) static inline void mpls_label_set_label(struct mpls_label *hdr, uint32_t label)
{ {
hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_LABEL_MASK) | (label << MPLS_LABEL_SHIFT)); hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_LS_LABEL_MASK) | (label << MPLS_LS_LABEL_SHIFT));
} }
static inline void mpls_hdr_set_exp(struct mpls_hdr *hdr, uint8_t exp) static inline void mpls_label_set_tc(struct mpls_label *hdr, uint8_t tc)
{ {
hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_EXP_MASK) | (exp << MPLS_EXP_SHIFT)); hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_LS_TC_MASK) | (tc << MPLS_LS_TC_SHIFT));
} }
static inline void mpls_hdr_set_bos(struct mpls_hdr *hdr, uint8_t bos) static inline void mpls_label_set_bos(struct mpls_label *hdr, uint8_t bos)
{ {
hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_STACK_MASK) | (bos << MPLS_STACK_SHIFT)); hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_LS_S_MASK) | (bos << MPLS_LS_S_SHIFT));
} }
static inline void mpls_hdr_set_ttl(struct mpls_hdr *hdr, uint8_t ttl) static inline void mpls_label_set_ttl(struct mpls_label *hdr, uint8_t ttl)
{ {
hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_TTL_MASK) | (ttl << MPLS_TTL_SHIFT)); hdr->entry = htonl((ntohl(hdr->entry) & ~MPLS_LS_TTL_MASK) | (ttl << MPLS_LS_TTL_SHIFT));
} }
/****************************************************************************** /******************************************************************************
* print * print
******************************************************************************/ ******************************************************************************/
static inline int mpls_hdr_to_str(const struct mpls_hdr *hdr, char *buf, size_t size) static inline int mpls_label_to_str(const struct mpls_label *hdr, char *buf, size_t size)
{ {
return snprintf(buf, size, "MPLS: label=%u exp=%u bos=%u ttl=%u", return snprintf(buf, size, "MPLS: label=%u tc=%u bos=%u ttl=%u",
mpls_hdr_get_label(hdr), mpls_hdr_get_exp(hdr), mpls_label_get_label(hdr), mpls_label_get_tc(hdr),
mpls_hdr_get_bos(hdr), mpls_hdr_get_ttl(hdr)); mpls_label_get_bos(hdr), mpls_label_get_ttl(hdr));
} }
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -464,7 +464,6 @@ static inline uint16_t get_gre_hdr_len(const char *data, uint16_t len)
} }
} }
} }
else if (version == 1) else if (version == 1)
{ {
hdr_offset = 8; hdr_offset = 8;
@@ -828,7 +827,7 @@ static inline const char *parse_mpls(struct packet *pkt, const char *data, uint1
return data; return data;
} }
if (mpls_hdr_get_bos((const struct mpls_hdr *)data)) if (mpls_label_get_bos((const struct mpls_label *)data))
{ {
SET_LAYER(pkt, layer, LAYER_TYPE_MPLS, 4, data, len, 0); SET_LAYER(pkt, layer, LAYER_TYPE_MPLS, 4, data, len, 0);
if (layer->pld_len == 0) if (layer->pld_len == 0)
@@ -1384,7 +1383,7 @@ void packet_print_str(const struct packet *pkt)
case LAYER_TYPE_PPPOE: case LAYER_TYPE_PPPOE:
break; break;
case LAYER_TYPE_MPLS: case LAYER_TYPE_MPLS:
used = mpls_hdr_to_str((const struct mpls_hdr *)layer->hdr_ptr, buffer, sizeof(buffer)); used = mpls_label_to_str((const struct mpls_label *)layer->hdr_ptr, buffer, sizeof(buffer));
break; break;
break; break;
case LAYER_TYPE_IPV4: case LAYER_TYPE_IPV4:

View File

@@ -15,23 +15,23 @@ unsigned char data[] = {
TEST(MPLS_UTILS, GET) TEST(MPLS_UTILS, GET)
{ {
const struct mpls_hdr *hdr = (struct mpls_hdr *)data; const struct mpls_label *hdr = (struct mpls_label *)data;
EXPECT_TRUE(mpls_hdr_get_label(hdr) == 0xbe490); EXPECT_TRUE(mpls_label_get_label(hdr) == 0xbe490);
EXPECT_TRUE(mpls_hdr_get_exp(hdr) == 0); EXPECT_TRUE(mpls_label_get_tc(hdr) == 0);
EXPECT_TRUE(mpls_hdr_get_bos(hdr) == 0); EXPECT_TRUE(mpls_label_get_bos(hdr) == 0);
EXPECT_TRUE(mpls_hdr_get_ttl(hdr) == 255); EXPECT_TRUE(mpls_label_get_ttl(hdr) == 255);
} }
TEST(MPLS_UTILS, SET) TEST(MPLS_UTILS, SET)
{ {
char buff[4] = {0}; char buff[4] = {0};
struct mpls_hdr *hdr = (struct mpls_hdr *)buff; struct mpls_label *hdr = (struct mpls_label *)buff;
mpls_hdr_set_label(hdr, 0xbe490); mpls_label_set_label(hdr, 0xbe490);
mpls_hdr_set_exp(hdr, 0); mpls_label_set_tc(hdr, 0);
mpls_hdr_set_bos(hdr, 0); mpls_label_set_bos(hdr, 0);
mpls_hdr_set_ttl(hdr, 255); mpls_label_set_ttl(hdr, 255);
EXPECT_TRUE(memcmp(buff, data, 4) == 0); EXPECT_TRUE(memcmp(buff, data, 4) == 0);
} }