From c1f633c5c7bcf81ae9d612a6da8bfd893477dba9 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Mon, 1 Oct 2018 15:17:26 +0100 Subject: [PATCH] Dropping bitfields for now. Using #defines and uint8_t for the flags. --- lib/include/srslte/upper/gtpu.h | 74 +++++++++++++++------------------ lib/src/upper/gtpu.cc | 73 +++++++++++++++----------------- srsenb/src/upper/gtpu.cc | 15 +++---- srsepc/src/mbms-gw/mbms-gw.cc | 4 +- srsepc/src/spgw/spgw.cc | 6 +-- 5 files changed, 75 insertions(+), 97 deletions(-) diff --git a/lib/include/srslte/upper/gtpu.h b/lib/include/srslte/upper/gtpu.h index 5cb0ed8ce..41e9dc2e9 100644 --- a/lib/include/srslte/upper/gtpu.h +++ b/lib/include/srslte/upper/gtpu.h @@ -56,10 +56,13 @@ namespace srslte { #define GTPU_BASE_HEADER_LEN 8 #define GTPU_EXTENDED_HEADER_LEN 12 -#define GTPU_VERSION_V1 1 - -#define GTP_PRIME_PROTO 0 -#define GTP_PROTO 1 +#define GTPU_FLAGS_VERSION_MASK 0xE0 +#define GTPU_FLAGS_VERSION_V1 0x20 +#define GTPU_FLAGS_GTP_PRIME_PROTOCOL 0x00 +#define GTPU_FLAGS_GTP_PROTOCOL 0x10 +#define GTPU_FLAGS_EXTENDED_HDR 0x04 +#define GTPU_FLAGS_SEQUENCE 0x02 +#define GTPU_FLAGS_PACKET_NUM 0x01 #define GTPU_MSG_ECHO_REQUEST 0 #define GTPU_MSG_ECHO_RESPONSE 1 @@ -69,19 +72,7 @@ namespace srslte { #define GTPU_MSG_DATA_PDU 255 typedef struct{ - uint8_t version : 3; - uint8_t protocol_type : 1; - uint8_t star : 1; - uint8_t ext_header :1; - uint8_t sequence :1; - uint8_t pkt_number :1; -}gtpu_flags_t; - -typedef struct{ - union{ - gtpu_flags_t flag_bits; - uint8_t flags; - } gtpu_flags; + uint8_t flags; uint8_t message_type; uint16_t length; uint32_t teid; @@ -94,35 +85,38 @@ typedef struct{ bool gtpu_read_header(srslte::byte_buffer_t *pdu, gtpu_header_t *header, srslte::log *gtpu_log); bool gtpu_write_header(gtpu_header_t *header, srslte::byte_buffer_t *pdu, srslte::log *gtpu_log); -inline void uint8_to_uint32(uint8_t *buf, uint32_t *i) -{ - *i = (uint32_t)buf[0] << 24 | - (uint32_t)buf[1] << 16 | - (uint32_t)buf[2] << 8 | - (uint32_t)buf[3]; -} - -inline void uint32_to_uint8(uint32_t i, uint8_t *buf) +inline bool gtpu_supported_flags_check(gtpu_header_t *header, srslte::log *gtpu_log) { - buf[0] = (i >> 24) & 0xFF; - buf[1] = (i >> 16) & 0xFF; - buf[2] = (i >> 8) & 0xFF; - buf[3] = i & 0xFF; + //flags + if( (header->flags & GTPU_FLAGS_VERSION_MASK) != GTPU_FLAGS_VERSION_V1 ) { + gtpu_log->error("gtpu_write_header - Unhandled GTP-U Version.\n"); + return false; + } + if( !(header->flags & GTPU_FLAGS_GTP_PROTOCOL) ) { + gtpu_log->error("gtpu_write_header - Unhandled Protocol Type.\n"); + return false; + } + if( !(header->flags & GTPU_FLAGS_EXTENDED_HDR) ) { + gtpu_log->error("gtpu_write_header - Unhandled Header Extensions.\n"); + return false; + } + if( !(header->flags & GTPU_FLAGS_PACKET_NUM) ) { + gtpu_log->error("gtpu_write_header - Unhandled Packet Number.\n"); + return false; + } + return true; } -inline void uint8_to_uint16(uint8_t *buf, uint16_t *i) +inline bool gtpu_supported_msg_type_check(gtpu_header_t *header, srslte::log *gtpu_log) { - *i = (uint32_t)buf[0] << 8 | - (uint32_t)buf[1]; + //msg_tpye + if(header->message_type != GTPU_MSG_DATA_PDU || header->message_type != GTPU_MSG_ECHO_REQUEST) { + gtpu_log->error("gtpu_write_header - Unhandled message type: 0x%x\n", header->message_type); + return false; + } + return true; } -inline void uint16_to_uint8(uint16_t i, uint8_t *buf) -{ - buf[0] = (i >> 8) & 0xFF; - buf[1] = i & 0xFF; -} - - }//namespace #endif diff --git a/lib/src/upper/gtpu.cc b/lib/src/upper/gtpu.cc index b01f834fb..b373cc546 100644 --- a/lib/src/upper/gtpu.cc +++ b/lib/src/upper/gtpu.cc @@ -26,7 +26,7 @@ #include "srslte/upper/gtpu.h" - +#include "srslte/common/int_helpers.h" namespace srslte { @@ -37,40 +37,45 @@ namespace srslte { bool gtpu_write_header(gtpu_header_t *header, srslte::byte_buffer_t *pdu, srslte::log *gtpu_log) { - - if(header->gtpu_flags.flag_bits.version != GTPU_VERSION_V1) { - gtpu_log->error("gtpu_write_header - Unhandled GTP-U Version.\n"); - return false; - } - if(header->gtpu_flags.flag_bits.protocol_type != GTP_PROTO) { - gtpu_log->error("gtpu_write_header - Unhandled Protocol Type.\n"); - return false; - } - if(header->gtpu_flags.flag_bits.ext_header) { - gtpu_log->error("gtpu_write_header - Unhandled Header Extensions.\n"); + //flags + if(!gtpu_supported_flags_check(header,gtpu_log)){ return false; } - if(header->message_type != GTPU_MSG_DATA_PDU) { + + //msg type + if(header->message_type != GTPU_MSG_DATA_PDU || header->message_type != GTPU_MSG_ECHO_REQUEST) { gtpu_log->error("gtpu_write_header - Unhandled message type: 0x%x\n", header->message_type); return false; } - if(pdu->get_headroom() < GTPU_BASE_HEADER_LEN) { - gtpu_log->error("gtpu_write_header - No room in PDU for header\n"); - return false; - } - pdu->msg -= GTPU_BASE_HEADER_LEN; - pdu->N_bytes += GTPU_BASE_HEADER_LEN; + //If E, S or PN are set, the header is longer + if (header->flags & (GTPU_FLAGS_EXTENDED_HDR | GTPU_FLAGS_SEQUENCE | GTPU_FLAGS_PACKET_NUM)) { + if(pdu->get_headroom() < GTPU_EXTENDED_HEADER_LEN) { + gtpu_log->error("gtpu_write_header - No room in PDU for header\n"); + return false; + } + pdu->msg -= GTPU_EXTENDED_HEADER_LEN; + pdu->N_bytes += GTPU_EXTENDED_HEADER_LEN; + } else { + if(pdu->get_headroom() < GTPU_BASE_HEADER_LEN) { + gtpu_log->error("gtpu_write_header - No room in PDU for header\n"); + return false; + } + pdu->msg -= GTPU_BASE_HEADER_LEN; + pdu->N_bytes += GTPU_BASE_HEADER_LEN; + } + //write mandatory fields uint8_t *ptr = pdu->msg; - - *ptr = header->gtpu_flags.flags; + *ptr = header->flags; ptr++; *ptr = header->message_type; ptr++; uint16_to_uint8(header->length, ptr); ptr += 2; uint32_to_uint8(header->teid, ptr); + //write optional fields + return true; } @@ -79,7 +84,7 @@ bool gtpu_read_header(srslte::byte_buffer_t *pdu, gtpu_header_t *header, srslte: { uint8_t *ptr = pdu->msg; - header->gtpu_flags.flags = *ptr; + header->flags = *ptr; ptr++; header->message_type = *ptr; ptr++; @@ -87,21 +92,18 @@ bool gtpu_read_header(srslte::byte_buffer_t *pdu, gtpu_header_t *header, srslte: ptr += 2; uint8_to_uint32(ptr, &header->teid); - if(header->gtpu_flags.flag_bits.version != GTPU_VERSION_V1) { - gtpu_log->error("gtpu_read_header - Unhandled GTP-U version. Flags: 0x%x\n", header->gtpu_flags.flags); - return false; - } - if(header->gtpu_flags.flag_bits.protocol_type != GTP_PROTO) { - gtpu_log->error("gtpu_read_header - Unhandled GTP Protocol. Flags: 0x%x\n", header->gtpu_flags.flags); + //flags + if(!gtpu_supported_flags_check(header,gtpu_log)){ return false; } - if(header->gtpu_flags.flag_bits.ext_header) { - gtpu_log->error("gtpu_read_header - Unhandled GTP-U Header Extensions. Flags: 0x%x\n", header->gtpu_flags.flags); + + //message_type + if(!gtpu_supported_msg_type_check(header,gtpu_log)){ return false; } //If E, S or PN are set, header is longer - if(header->gtpu_flags.flag_bits.sequence || header->gtpu_flags.flag_bits.ext_header || header->gtpu_flags.flag_bits.ext_header) { + if (header->flags & (GTPU_FLAGS_EXTENDED_HDR | GTPU_FLAGS_SEQUENCE | GTPU_FLAGS_PACKET_NUM)) { pdu->msg += GTPU_EXTENDED_HEADER_LEN; pdu->N_bytes -= GTPU_EXTENDED_HEADER_LEN; @@ -118,15 +120,6 @@ bool gtpu_read_header(srslte::byte_buffer_t *pdu, gtpu_header_t *header, srslte: pdu->N_bytes -= GTPU_BASE_HEADER_LEN; } - if(header->gtpu_flags.flag_bits.sequence){ - ptr+=2; - } - - if(header->message_type != GTPU_MSG_DATA_PDU || header->message_type != GTPU_MSG_ECHO_REQUEST) { - gtpu_log->error("gtpu_read_header - Unhandled message type: 0x%x\n", header->message_type); - return false; - } - return true; } diff --git a/srsenb/src/upper/gtpu.cc b/srsenb/src/upper/gtpu.cc index 99bcd7e25..acce49eb6 100644 --- a/srsenb/src/upper/gtpu.cc +++ b/srsenb/src/upper/gtpu.cc @@ -122,9 +122,7 @@ void gtpu::write_pdu(uint16_t rnti, uint32_t lcid, srslte::byte_buffer_t* pdu) { gtpu_log->info_hex(pdu->msg, pdu->N_bytes, "TX PDU, RNTI: 0x%x, LCID: %d, n_bytes=%d", rnti, lcid, pdu->N_bytes); gtpu_header_t header; - bzero(&header,sizeof(header)); - header.gtpu_flags.flag_bits.version = GTPU_VERSION_V1; - header.gtpu_flags.flag_bits.protocol_type = GTP_PROTO; + header.flags = GTPU_FLAGS_VERSION_V1 | GTPU_FLAGS_GTP_PROTOCOL; header.message_type = GTPU_MSG_DATA_PDU; header.length = pdu->N_bytes; header.teid = rnti_bearers[rnti].teids_out[lcid]; @@ -280,19 +278,16 @@ void gtpu::echo_response(in_addr_t addr, in_port_t port, uint16_t seq) gtpu_log->info("TX GTPU Echo Response, Seq: %d\n", seq); gtpu_header_t header; - bzero(&header, sizeof(header)); - srslte::byte_buffer_t *pdu = pool_allocate; - //flags - header.gtpu_flags.flag_bits.version = GTPU_VERSION_V1; - header.gtpu_flags.flag_bits.protocol_type = GTP_PROTO; - header.gtpu_flags.flag_bits.sequence = 1; - + //header + header.flags = GTPU_FLAGS_VERSION_V1 | GTPU_FLAGS_GTP_PROTOCOL | GTPU_FLAGS_SEQUENCE; header.message_type = GTPU_MSG_ECHO_RESPONSE; header.teid = 0; header.length = 4; header.seq_number = seq; + header.n_pdu = 0; + header.next_ext_hdr_type = 0; gtpu_write_header(&header,pdu,gtpu_log); diff --git a/srsepc/src/mbms-gw/mbms-gw.cc b/srsepc/src/mbms-gw/mbms-gw.cc index e9d828451..dc8199e27 100644 --- a/srsepc/src/mbms-gw/mbms-gw.cc +++ b/srsepc/src/mbms-gw/mbms-gw.cc @@ -285,11 +285,9 @@ mbms_gw::handle_sgi_md_pdu(srslte::byte_buffer_t *msg) { uint8_t version; srslte::gtpu_header_t header; - bzero(&header, sizeof(srslte::gtpu_header_t)); //Setup GTP-U header - header.gtpu_flags.flag_bits.version = GTPU_VERSION_V1; - header.gtpu_flags.flag_bits.protocol_type = GTP_PROTO; + header.flags = GTPU_FLAGS_VERSION_V1 | GTPU_FLAGS_GTP_PROTOCOL; header.message_type = GTPU_MSG_DATA_PDU; header.length = msg->N_bytes; header.teid = 0xAAAA; //FIXME Harcoded TEID for now diff --git a/srsepc/src/spgw/spgw.cc b/srsepc/src/spgw/spgw.cc index da36fdaac..0a867d51c 100644 --- a/srsepc/src/spgw/spgw.cc +++ b/srsepc/src/spgw/spgw.cc @@ -359,10 +359,8 @@ spgw::handle_sgi_pdu(srslte::byte_buffer_t *msg) //Setup GTP-U header srslte::gtpu_header_t header; - bzero(&header,sizeof(srslte::gtpu_header_t)); - header.gtpu_flags.flag_bits.version = GTPU_VERSION_V1; - header.gtpu_flags.flag_bits.protocol_type = GTP_PROTO; - header.message_type = GTPU_MSG_DATA_PDU; + header.flags = GTPU_FLAGS_VERSION_V1 | GTPU_FLAGS_GTP_PROTOCOL; + header.message_type = GTPU_MSG_DATA_PDU; header.length = msg->N_bytes; header.teid = enb_fteid.teid;