Front Haul Interface Library update to third seed code contribution
[o-du/phy.git] / fhi_lib / lib / src / xran_transport.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief This file provides the implementation for Transport lyaer (eCPRI) API.
21  *
22  * @file xran_transport.c
23  * @ingroup group_lte_source_xran
24  * @author Intel Corporation
25  *
26  **/
27
28 #include <stdint.h>
29 #include <endian.h>
30 #include <rte_common.h>
31 #include <rte_config.h>
32
33 #include "xran_fh_o_du.h"
34 #include "xran_common.h"
35 #include "xran_transport.h"
36 #include "xran_pkt_cp.h"
37 #include "xran_cp_api.h"
38 #include "xran_up_api.h"
39 #include "xran_printf.h"
40
41
42 /**
43  * @brief return eCPRI header size without eCPRI common header
44  *
45  * @ingroup xran
46  *
47  * @return the size of eCPRI header without common header
48  */
49 int xran_get_ecpri_hdr_size(void)
50 {
51     return(sizeof(struct xran_ecpri_hdr) - sizeof(struct xran_ecpri_cmn_hdr));
52 }
53
54 /**
55  * @brief Compose ecpriRtcid/ecpriPcid
56  *
57  * @ingroup xran
58  *
59  * @param CU_Port_ID CU Port ID
60  * @param BanbSector_ID Band Sector ID
61  * @param CC_ID Component Carrier ID
62  * @param Ant_ID RU Port ID (antenna ID)
63  * @return uint16_t composed ecpriRtcid/ecpriPcid (network byte order)
64  */
65 uint16_t xran_compose_cid(uint8_t CU_Port_ID, uint8_t BandSector_ID, uint8_t CC_ID, uint8_t Ant_ID)
66 {
67   uint16_t cid;
68   struct xran_eaxcid_config *conf;
69
70     conf = xran_get_conf_eAxC(NULL);
71
72     cid = ((CU_Port_ID      << conf->bit_cuPortId)      & conf->mask_cuPortId)
73         | ((BandSector_ID   << conf->bit_bandSectorId)  & conf->mask_bandSectorId)
74         | ((CC_ID           << conf->bit_ccId)          & conf->mask_ccId)
75         | ((Ant_ID          << conf->bit_ruPortId)      & conf->mask_ruPortId);
76
77     return (rte_cpu_to_be_16(cid));
78 }
79
80 /**
81  * @brief Decompose ecpriRtcid/ecpriPcid
82  *
83  * @ingroup xran
84  *
85  * @param cid composed ecpriRtcid/ecpriPcid (network byte order)
86  * @param result the pointer of the structure to store decomposed values
87  * @return none
88  */
89 void xran_decompose_cid(uint16_t cid, struct xran_eaxc_info *result)
90 {
91   struct xran_eaxcid_config *conf;
92
93     conf = xran_get_conf_eAxC(NULL);
94     cid = rte_be_to_cpu_16(cid);
95
96     result->cuPortId        = (cid&conf->mask_cuPortId)     >> conf->bit_cuPortId;
97     result->bandSectorId    = (cid&conf->mask_bandSectorId) >> conf->bit_bandSectorId;
98     result->ccId            = (cid&conf->mask_ccId)         >> conf->bit_ccId;
99     result->ruPortId        = (cid&conf->mask_ruPortId)     >> conf->bit_ruPortId;
100
101     return;
102 }
103
104 /**
105  * @brief modify the payload size of eCPRI header in xRAN packet
106  *
107  * @ingroup xran
108  *
109  * @param mbuf Initialized rte_mbuf packet which has eCPRI header already
110  * @param size payload size to be updated
111  * @return none
112  */
113 inline void xran_update_ecpri_payload_size(struct rte_mbuf *mbuf, int size)
114 {
115   struct xran_ecpri_hdr *ecpri_hdr;
116
117     ecpri_hdr = rte_pktmbuf_mtod(mbuf, struct xran_ecpri_hdr *);
118
119     ecpri_hdr->cmnhdr.ecpri_payl_size = rte_cpu_to_be_16(size);
120 }
121
122
123 /**
124  * @brief Build ECPRI header and returns added length
125  *
126  * @ingroup xran
127  *
128  * @param mbuf
129  *  The pointer of the packet buffer to be parsed
130  * @param CC_ID
131  *  Component Carrier ID for this C-Plane message
132  * @param Ant_ID
133  *  Antenna ID(RU Port ID) for this C-Plane message
134  * @param seq_id
135  *  Sequence ID for this C-Plane message
136  * @param ecpri_hdr
137  *  The pointer to ECPRI header
138  * @return
139  *  added payload size on success
140  *  XRAN_STATUS_RESOURCE if failed to allocate the space to packet buffer
141  */
142 int xran_build_ecpri_hdr(struct rte_mbuf *mbuf,
143                         uint8_t CC_ID, uint8_t Ant_ID,
144                         uint8_t seq_id,
145                         struct xran_ecpri_hdr **ecpri_hdr)
146 {
147   uint32_t payloadlen;
148   struct xran_ecpri_hdr *tmp;
149
150
151     tmp = (struct xran_ecpri_hdr *)rte_pktmbuf_append(mbuf, sizeof(struct xran_ecpri_hdr));
152     if(unlikely(tmp == NULL)) {
153         print_err("Fail to allocate the space for eCPRI hedaer!");
154         return (XRAN_STATUS_RESOURCE);
155         }
156
157     /* Fill common header */
158     tmp->cmnhdr.ecpri_ver           = XRAN_ECPRI_VER;
159     tmp->cmnhdr.ecpri_resv          = 0;     // should be zero
160     tmp->cmnhdr.ecpri_concat        = 0;
161     tmp->cmnhdr.ecpri_mesg_type     = ECPRI_RT_CONTROL_DATA;
162     tmp->ecpri_xtc_id               = xran_compose_cid(0, 0, CC_ID, Ant_ID);
163
164     /* TODO: Transport layer fragmentation is not supported */
165     tmp->ecpri_seq_id.seq_id        = seq_id;
166     tmp->ecpri_seq_id.sub_seq_id    = 0;
167     tmp->ecpri_seq_id.e_bit         = 1;
168
169     /* Starts with eCPRI header size */
170     payloadlen = xran_get_ecpri_hdr_size();
171
172     *ecpri_hdr = tmp;
173
174     return (payloadlen);
175 }
176
177 /**
178  * @brief Parse ECPRI header
179  *
180  * @ingroup xran
181  *
182  * @param mbuf
183  *  The pointer of the packet buffer to be parsed
184  * @param ecpri_hdr
185  *  The pointer to ECPRI header
186  * @param pkt_info
187  *  The pointer of sturcture to store the information from header
188  * @return
189  *  XRAN_STATUS_SUCCESS on success
190  *  XRAN_STATUS_INVALID_PACKET if failed to parse the packet
191  */
192 int xran_parse_ecpri_hdr(struct rte_mbuf *mbuf,
193                     struct xran_ecpri_hdr **ecpri_hdr,
194                     struct xran_recv_packet_info *pkt_info)
195 {
196   int ret;
197
198
199     *ecpri_hdr = rte_pktmbuf_mtod(mbuf, void *);
200     if(*ecpri_hdr == NULL) {
201         print_err("Invalid packet - eCPRI hedaer!");
202         return (XRAN_STATUS_INVALID_PACKET);
203         }
204
205     /* Process eCPRI header */
206     ret = XRAN_STATUS_SUCCESS;
207     if((*ecpri_hdr)->cmnhdr.ecpri_ver != XRAN_ECPRI_VER) {
208         print_err("Invalid eCPRI version - %d", (*ecpri_hdr)->cmnhdr.ecpri_ver);
209         ret = XRAN_STATUS_INVALID_PACKET;
210         }
211     if((*ecpri_hdr)->cmnhdr.ecpri_resv != 0) {
212         print_err("Invalid reserved field - %d", (*ecpri_hdr)->cmnhdr.ecpri_resv);
213         ret = XRAN_STATUS_INVALID_PACKET;
214         }
215
216     if(pkt_info != NULL) {
217         /* store the information from header */
218         pkt_info->ecpri_version = (*ecpri_hdr)->cmnhdr.ecpri_ver;
219         pkt_info->msg_type      = (enum ecpri_msg_type)(*ecpri_hdr)->cmnhdr.ecpri_mesg_type;
220         pkt_info->payload_len   = rte_be_to_cpu_16((*ecpri_hdr)->cmnhdr.ecpri_payl_size);
221
222         pkt_info->seq_id        = (*ecpri_hdr)->ecpri_seq_id.seq_id;
223         pkt_info->subseq_id     = (*ecpri_hdr)->ecpri_seq_id.sub_seq_id;
224         pkt_info->ebit          = (*ecpri_hdr)->ecpri_seq_id.e_bit;
225         xran_decompose_cid((*ecpri_hdr)->ecpri_xtc_id, &(pkt_info->eaxc));
226         }
227
228     return (ret);
229 }
230