O-CU-UP
[scp/ocu/5gnr.git] / Include / gtpCommon.h
1 /******************************************************************************
2 ###############################################################################
3 #   Copyright (c) [2017-2020] [ICT/CAS]                                        #
4 #   Licensed under the ORAN Software License v1.0 (License)             #
5 ###############################################################################
6 ******************************************************************************/
7
8 #ifndef GTPU_COMMON_H
9 #define GTPU_COMMON_H
10
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include "vos_lib.h"
14 #include "vos_linklist.h"
15 #include "vos_types.h"
16 #include "plat_syslog.h"
17 #include "vos_sockMgr.h"
18 #include "vos_module.h"
19 #include "gnbCommon.h"
20 #include "msgb.h"
21
22
23 #define GTP_MAX_TUN_NUM                                  100    
24 #define GTPU_MODULE                                        1    /* just for compile */
25 #define GTP_MAX_DATA_LEN                                8000
26 #define GTP_HEADER_SIZE_SHORT                      8
27 #define GTP_HEADER_SIZE_LONG                      12
28
29 /* GTP version 1 extension header type definitions. */
30 #define GTP_EXT_PDCP_PDU                        0xC0    /* PDCP PDU Number */
31 #define GTP_EXT_RAN_CONTAINER                   0X81    /* RAN Container */
32 #define GTP_EXT_NRRAN_CONTAINER                 0X84    /* NR RAN Container */
33 #define GTP_EXT_PDUSESSION_CONTAINER    0X85    /* PDU Session Container */
34
35
36 #define GTP_ECHO_REQ                               1    /* Echo Request */
37 #define GTP_ECHO_RSP                               2    /* Echo Response */
38 #define GTP_NOT_SUPPORTED                          3    /* Version Not Supported */
39
40 #define GTP_ERROR                                 26    /* Error Indication */
41 #define GTP_SUPP_EXT_HEADER                       31    /* Supported Extension Headers Notification */
42 #define GTP_GPDU                                 255    /* G-PDU */
43 #define GTP_END_MARKER                           254 
44
45 /* for f1u pdu */
46 #define GTP_DLUSERDATA_EX_LEN                      2
47 #define GTP_DLUSERDATA_BYTES_LEN                   8
48 #define GTP_DDDS_EX_LEN                                    6
49 #define GTP_DDDS_BYTES_LEN                                24
50
51 /* for ngu pdu */
52 #define GTP_DLPDUSESSION_EX_LEN                    2
53 #define GTP_DLPDUSESSION_BYTES_LEN                 8
54 #define GTP_ULPDUSESSION_EX_LEN                    1
55 #define GTP_ULPDUSESSION_BYTES_LEN                 4
56
57 #define GTP_EXHEAD_LEN                                   256
58
59
60 #define PATH_DETECT_TIMER                                 60      /* detect path every 60 seconds */
61 #define T3RESPONSE_TIMER                                   3      /* T3-RESPONSE timer defaults to 1 second */
62 #define N3REQUESTS                                         5      /* max times of retransmssion of echo quest */
63
64 #define GTPU_ENTITY_NUM                                    2      /* CUGTPU+DUGTPU */
65
66 #define gtpLog(iLog_Level, gtp, content, arg...) \
67 {\
68         if(NULL != gtp)\
69         {\
70                 VOS_SysLog(gtp->userModuleID, iLog_Level, __FILE__, __LINE__, __func__, content, ##arg);\
71         }else\
72         {\
73                 vos_err_print("gtp entity is NULL!\n");\
74         }\
75 }
76
77 #if 0
78 #define gtpNullCheck(p,gtp) \
79 {\
80    if(NULL == p)\
81    {\
82            gtpLog(LOG_ERR,gtp,"[GTPU] NULL pointer\n");\
83        return VOS_ERROR;\
84    }\
85 }
86 #endif
87
88 typedef enum
89 {
90         CU_GTPU,
91         DU_GTPU
92 }GtpType_e;
93
94 typedef enum
95 {
96         CU_NG_TUNNEL,
97         CU_F1_TUNNEL,
98         DU_TUNNEL
99 }TunnelType_e;
100
101 typedef enum
102 {
103         TUNNEL_INACTIVE = 0,
104         TUNNEL_ACTIVE
105 }TunnelStatus_e;
106
107
108
109 /* GTP-U header definitions */
110
111 typedef struct
112 { /*    Descriptions from 3GPP 29060 */
113     UINT8 pn :1;                /*    .......0 PN: N-PDU Number flag */
114     UINT8 s :1;                 /*    ......0. Sequence number flag: 0 */
115     UINT8 e :1;                 /*    .....0.. Extension header flag: 0 */
116     UINT8 spare :1;             /*    ....0... Spare = 0 */
117     UINT8 pt :1;                /*    ...1.... Protocol Type: GTP=1, GTP'=0 */
118     UINT8 ver :3;               /*    001..... Version: 1 */
119     UINT8 type;                 /* 02 Message type. T-PDU = 0xff */
120     UINT16 length;              /* 03 Length (of IP packet or signalling) */
121     UINT32 tei;                 /* 05 - 08 Tunnel Endpoint ID */
122 } GtpHeadShort_t;
123
124 typedef struct 
125 { /*    Descriptions from 3GPP 29060 */
126     UINT8 pn :1;                /*    .......0 PN: N-PDU Number flag */
127     UINT8 s :1;                 /*    ......0. Sequence number flag: 0 */
128     UINT8 e :1;                 /*    .....0.. Extension header flag: 0 */
129     UINT8 spare :1;             /*    ....0... Spare = 0 */
130     UINT8 pt :1;                /*    ...1.... Protocol Type: GTP=1, GTP'=0 */
131     UINT8 ver :3;               /*    001..... Version: 1 */
132     UINT8 type;                 /* 02 Message type. T-PDU = 0xff */
133     UINT16 length;              /* 03 Length (of IP packet or signalling) */
134     UINT32 tei;                 /* 05 - 08 Tunnel Endpoint ID */
135     UINT16 seq;                 /* 09 - 10 Sequence Number */
136     UINT8 npdu;                 /* 11 N-PDU Number */
137     UINT8 next;                 /* 12 Next extension header type. Empty = 0 */
138 }GtpHeadLong_t;
139
140
141 struct gtpExtHead{
142         uint8_t type;
143         uint8_t len;
144         uint8_t data[];
145 } __attribute__((packed));
146 #define GTP_EXTHDR(buf) ((struct gtpExtHead *)buf)
147 #define GTP_LHDR(buf) ((GtpHeadLong_t *)buf)
148 #define GTP_FIRSTHDR(buf) ((GTP_LHDR(buf)->length >= 5 &&               \
149                 GTP_LHDR(buf)->next) ?                                  \
150                 (struct gtpExtHead *)&GTP_LHDR(buf)->next : NULL)
151 #define GTP_NXTHDR(buf, ext)                                            \
152         (ext == NULL ? GTP_FIRSTHDR(buf) :                              \
153                 ((uint8_t *)&ext->len + ext->len * 4 -                  \
154                 (uint8_t *)&GTP_LHDR(buf)->seq) >                       \
155                 GTP_LHDR(buf)->length ?                                 \
156                 NULL : *((uint8_t *)&ext->len + ext->len * 4 - 1) ?     \
157                         (struct gtpExtHead *)((uint8_t *)&ext->len +    \
158                         ext->len * 4 - 1) : NULL)
159 #define GTP_EXTDATA(buf) (GTP_LHDR(buf)->data)
160
161
162
163 /* GTP-U packet */
164 typedef struct
165 {
166     GtpHeadShort_t h;    
167     UINT8                        p[GTP_MAX_DATA_LEN];           /* store data */
168 }GtpPacketShort_t;
169
170 typedef struct
171 {
172     GtpHeadLong_t h;
173     UINT8                       p[GTP_MAX_DATA_LEN];            /* store data */
174 }GtpPacketLong_t;
175
176 union gtpPacket
177 {
178     GtpPacketShort_t gtps;
179     GtpPacketLong_t  gtpl;
180 };
181
182
183 /* GTP-U tunnels management structures */
184 typedef struct tun
185 {
186         TunnelStatus_e                  tunState;                               /* 0 for suspend, others are active */
187         UINT16                                  seq;                                    /* G-PUD Sequence number counter */
188     UINT32                                      tunId;
189     UINT32                                      ownTeid;                                /* own TEID */
190     UINT32                                      remTeid;                                /* remote TEID */
191     vos_sock_info_t                     tunOpval;                               /* Address packet was sent to / received from */
192         TunnelType_e                    tunType;
193 } Tun_t;
194
195
196 typedef struct gtpMsg
197 {
198     UINT16                                      seq;                    /* The sequence number */
199     UINT8                                       type;                   /* The type of packet */    
200     UINT32                                      l;                              /* Length of the packet */
201     union gtpPacket             p;                              /* The packet stored */
202     vos_sock_info_t                     *pOpval;                /* Address packet was sent to / received from */
203     time_t                                      timeout;                /* When do we retransmit this packet? */
204         UINT8                                   retrans;                /* How many times did we retransmit this? */
205 }GtpMsgNode_t;
206
207
208 /* GTP-U Protocol entity */
209 typedef struct
210 {
211         GtpType_e               gtpId;
212         ULONG                   userModuleID;
213         
214         /* Parameters related to the network INT32erface */
215         vos_sockaddr_t  localAddr;                                              /* Local IP address for signaling and G-PDU */
216         
217     /* Parameters related to signaling messages */
218     UINT16                      seq_next;                                               /* Next sequence number to use */
219     UINT8                       restart_counter;                                /* Increment on restart. */
220         plist                   pReqMsgList;                                    /* list header */
221         Tun_t              *tuns[GTP_MAX_TUN_NUM];                      /* store memory,use TEID_OWN assignment */
222                                         
223     /* Counters ,some of them may not be used. */
224     UINT32 err_socket;                  /* Number of socket errors */
225     UINT32 err_readfrom;                /* Number of readfrom errors */
226     UINT32 err_sendto;                  /* Number of sendto errors */
227     UINT32 err_memcpy;                  /* Number of memcpy */
228     UINT32 err_seq;                     /* Number of seq out of range */
229     UINT32 err_cause;                   /* Unexpected cause value received */
230
231     UINT32 empty;                               /* Number of empty packets */
232     UINT32 unsup;                               /* Number of unsupported version 29.60 11.1.1 */
233     UINT32 tooshort;                    /* Number of too short headers 29.60 11.1.2 */
234     UINT32 unknown;                     /* Number of unknown messages 29.60 11.1.3 */
235     UINT32 nu_rabid;                    /* Number of unknown RAD ID message */
236     UINT32 unexpect;                    /* Number of unexpected messages 29.60 11.1.4 */
237     UINT32 duplicate;                   /* Number of duplicate or unsolicited replies */
238     UINT32 missing;                     /* Number of missing information field messages */
239     UINT32 incorrect;                   /* Number of incorrect information field messages */
240     UINT32 invalid;                     /* Number of invalid message format messages */
241 } Gtp_t;
242
243
244 typedef struct 
245 {
246         UINT8      *pPdu;                       /* length, content, next extension type */
247         UINT16          len;                    /* len = exHeaderLen * 4 */
248         UINT16          exHdrLen;
249         UINT8           exHdrType;
250 }ExPduInfo_t;
251
252 /* log level */
253 typedef enum gtpuLogLevel
254 {
255         GTPULOG_CRIT = 1,        ///<  critical conditions 
256         GTPULOG_ERR,             ///<  error conditions 
257         GTPULOG_WARNING,         ///<  warning conditions 
258         GTPULOG_INFO,            ///<  informational 
259         GTPULOG_DEBUG,           ///<  debug info
260 } GtpuLogLevel_e;
261
262 /* struct for gtpu receive data */
263 typedef struct
264 {
265         vos_sock_info_t op;
266         MsgbBuff_t *pData;
267 }GtpuRecvUdpInfo_t;
268
269 typedef struct
270 {
271     UINT32 tunId;
272         MsgbBuff_t *pMsgBuff;
273         ExPduInfo_t *pExPduInfo;
274 }GpduInfo_t;
275
276 typedef INT32 (* GtpuDataProcFn)(GpduInfo_t *pInfo);
277
278 typedef struct
279 {
280         GtpuDataProcFn action_fn_p;
281 }GtpuGpduHandler_t;
282
283
284
285
286
287 //check teid
288 #define gtpCheckTeid(n) if(n<0 || n>GTP_MAX_TUN_NUM) {vos_err_print(#n" is wrong\r\n");return VOS_ERROR;}
289
290
291 #endif /* GTPU_COMMON_H */
292