* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / lib / src / xran_delay_measurement.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 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 XRAN layer one-way delay measurement support
21  * @file xran_delay_measurement.c
22  * @ingroup group_source_xran
23  * @author Intel Corporation
24  **/
25 #define _GNU_SOURCE
26 #include <immintrin.h>
27 #include <assert.h>
28 #include <err.h>
29 #include <arpa/inet.h>
30 #include <sys/time.h>
31 #include <time.h>
32 #include <stdio.h>
33 #include <pthread.h>
34 #include <rte_eal.h>
35 #include <rte_ethdev.h>
36 #include <rte_mbuf.h>
37
38 #include "xran_common.h"
39 #include "ethdi.h"
40 #include "xran_pkt.h"
41 #include "xran_dev.h"
42 #include "xran_lib_mlog_tasks_id.h"
43 #include "xran_ecpri_owd_measurements.h"
44
45 #include "xran_printf.h"
46 #include "xran_mlog_lnx.h"
47
48 //#define ORAN_OWD_DEBUG_MSG_FLOW
49 //#define XRAN_OWD_DEBUG_MSG_FLOW
50 //#define XRAN_OWD_DEBUG_DELAY_INFO
51 //#define XRAN_OWD_DEBUG_TIME_STAMPS_INFO
52 //#define XRAN_OWD_DEBUG_MEAS_DB
53 //#define XRAN_OWD_TIMING_MODS
54
55
56     // Support for 1-way eCPRI delay measurement per section 3.2.4.6 of eCPRI Specification V2.0
57
58 uint64_t xran_ptp_to_host(uint64_t compValue)
59 {
60     return (rte_be_to_cpu_64(compValue));
61 }
62 void xran_host_to_ptp_ts(TimeStamp *ts, struct timespec *t)
63 {
64     uint64_t seconds, nanoseconds;
65
66     seconds = t->tv_sec;
67     nanoseconds = t->tv_nsec%1000000000LL;
68 #ifdef XRAN_OWD_DEBUG_DELAY_CONV_FUNCTIONS
69     printf("H2P_ts tv_sec %8"PRIx64" tv_nsec %8"PRIx64" seconds %8"PRIx64" ns %8"PRIx64" \n",t->tv_sec,t->tv_nsec,seconds,nanoseconds);
70 #endif
71
72     ts->secs_msb = rte_cpu_to_be_16((rte_be16_t)((seconds >> 32) & 0xFFFF));
73     ts->secs_lsb = rte_cpu_to_be_32((rte_be32_t)(seconds & 0xFFFFFFFF));
74     ts->ns       = rte_cpu_to_be_32((rte_be32_t)nanoseconds);
75 #ifdef XRAN_OWD_DEBUG_DELAY_CONV_FUNCTIONS
76     printf("Net order s_msb %4"PRIx16" s_lsb %8"PRIx32" ns %8"PRIx32" \n", ts->secs_msb, ts->secs_lsb,ts->ns );
77 #endif
78 }
79
80 uint64_t xran_ptp_ts_to_ns(TimeStamp *t)
81 {
82         uint64_t seconds, nanoseconds;
83         uint64_t ret_value;
84         // Convert to host order
85         t->secs_msb=rte_be_to_cpu_16(t->secs_msb);
86         t->secs_lsb=rte_be_to_cpu_32(t->secs_lsb);
87         seconds = ((uint64_t)t->secs_msb << 32) | ((uint64_t)t->secs_lsb );
88         nanoseconds = rte_be_to_cpu_32((uint64_t)t->ns);
89         ret_value = seconds * NS_PER_SEC + nanoseconds;
90 #ifdef XRAN_OWD_DEBUG_DELAY_CONV_FUNCTIONS
91         printf("PTP ts to ns sec_msb %4"PRIx16" secs_lsb %4"PRIx32" ns %4"PRIx32"  seconds %8"PRIx64" nanosec %8"PRIx64" ret_value %8"PRIx64"\n",t->secs_msb,t->secs_lsb,t->ns,seconds, nanoseconds,ret_value);
92 #endif
93         return ret_value;
94
95 }
96 static inline uint64_t xran_timespec_to_ns(struct timespec *t)
97 {
98     uint64_t ret_val;
99
100     ret_val  = t->tv_sec * NS_PER_SEC + t->tv_nsec;
101 #ifdef XRAN_OWD_DEBUG_DELAY_CONV_FUNCTIONS
102     printf("t->tv_sec is %08"PRIx64" tv_nsec is %08"PRIx64" ret_val is %08"PRIx64" ts_to_ns\n",t->tv_sec,t->tv_nsec,ret_val);
103 #endif
104     return ret_val;
105
106 }
107
108 void xran_ns_to_timespec(uint64_t ns, struct timespec *t)
109 {
110     t->tv_sec = ns/NS_PER_SEC;
111     t->tv_nsec = ns % NS_PER_SEC;
112
113 }
114
115 void xran_initialize_and_verify_owd_pl_length(void* handle)
116 {
117     struct xran_device_ctx * p_xran_dev_ctx = (struct xran_device_ctx *)handle;
118     
119     if ((p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id].owdm_PlLength == 0)||(p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id].owdm_PlLength < MIN_OWDM_PL_LENGTH))
120     {
121          // Use default length value
122          p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id].owdm_PlLength = MIN_OWDM_PL_LENGTH;
123     }
124     else if ( p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id].owdm_PlLength > MAX_OWDM_PL_LENGTH)
125     {
126          p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id].owdm_PlLength  = MAX_OWDM_PL_LENGTH;
127     }
128     
129 }
130
131 void xran_adjust_timing_parameters(void* Handle)
132 {
133     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx*)Handle;
134 #ifdef XRAN_OWD_TIMING_MODS
135     printf("delayAvg is %d and DELAY_THRESHOLD is %d \n", p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][0].delayAvg, DELAY_THRESHOLD);
136 #endif
137     if (p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][0].delayAvg < DELAY_THRESHOLD )
138         {
139             /* Modify the timing parameters */
140             if (p_xran_dev_ctx->fh_cfg.T1a_max_up >= ADJUSTMENT)
141                 p_xran_dev_ctx->fh_cfg.T1a_max_up -= ADJUSTMENT;
142             if (p_xran_dev_ctx->fh_cfg.T2a_max_up >= ADJUSTMENT)
143                 p_xran_dev_ctx->fh_cfg.T2a_max_up -= ADJUSTMENT;
144             if (p_xran_dev_ctx->fh_cfg.Ta3_min >= ADJUSTMENT)
145                 p_xran_dev_ctx->fh_cfg.Ta3_min -= ADJUSTMENT;
146             if (p_xran_dev_ctx->fh_cfg.T1a_max_cp_dl >= ADJUSTMENT)
147                 p_xran_dev_ctx->fh_cfg.T1a_max_cp_dl -= ADJUSTMENT;
148             if (p_xran_dev_ctx->fh_cfg.T1a_min_up >= ADJUSTMENT)
149                 p_xran_dev_ctx->fh_cfg.T1a_min_up -= ADJUSTMENT;
150             if (p_xran_dev_ctx->fh_cfg.T1a_max_up >= ADJUSTMENT)
151                 p_xran_dev_ctx->fh_cfg.T1a_max_up -= ADJUSTMENT;
152             if (p_xran_dev_ctx->fh_cfg.Ta4_min >= ADJUSTMENT)
153                 p_xran_dev_ctx->fh_cfg.Ta4_min -= ADJUSTMENT;
154             if (p_xran_dev_ctx->fh_cfg.Ta4_max >= ADJUSTMENT)
155                 p_xran_dev_ctx->fh_cfg.Ta4_max -= ADJUSTMENT;
156 #ifdef XRAN_OWD_TIMING_MODS
157             printf("Mod T1a_max_up is %d\n",p_xran_dev_ctx->fh_cfg.T1a_max_up);
158             printf("Mod T2a_max_up is %d\n",p_xran_dev_ctx->fh_cfg.T2a_max_up);
159             printf("Mod Ta3_min is %d\n",p_xran_dev_ctx->fh_cfg.Ta3_min);
160             printf("Mod T1a_max_cp_dl is %d\n",p_xran_dev_ctx->fh_cfg.T1a_max_cp_dl);
161             printf("Mod T1a_min_up is %d\n",p_xran_dev_ctx->fh_cfg.T1a_min_up);
162             printf("Mod T1a_max_up is %d\n",p_xran_dev_ctx->fh_cfg.T1a_max_up);
163             printf("Mod Ta4_min is %d\n",p_xran_dev_ctx->fh_cfg.Ta4_min);
164             printf("Mod Ta4_max is %d\n",p_xran_dev_ctx->fh_cfg.Ta4_max);
165 #endif
166         }
167    
168 }
169
170
171
172 void xran_compute_and_report_delay_estimate (struct xran_ecpri_del_meas_port *portData, uint16_t totalSamples, uint16_t id )
173 {
174     uint16_t i;
175     uint64_t *samples= portData->delaySamples;
176
177
178     for (i=2 ; i < MX_NUM_SAMPLES; i++) //Ignore first 2 samples
179     {
180         portData->delayAvg += samples[i];
181
182     }
183
184     // Average the delay by the number of samples
185     if ((totalSamples != 0)&&(totalSamples > 2))
186     {
187         portData->delayAvg /= (totalSamples-2);
188     }
189     // Report Average with printf
190     flockfile(stdout);
191     printf("OWD for port %i is %lu [ns] id %d \n", portData->portid, portData->delayAvg, id);
192     funlockfile(stdout);
193
194 }
195
196 int xran_get_delay_measurements_results (void* handle,  uint16_t port_id, uint8_t id, uint64_t* pdelay_avg)
197 {
198     int ret_value = FAIL;
199     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx*)handle;
200     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[id][port_id];
201     // Check is the one way delay measurement completed successfully
202     if (powdp->msState == XRAN_OWDM_DONE)
203     {
204         *pdelay_avg = powdp->delayAvg;
205         ret_value = OK;
206     }
207     return (ret_value);
208 }
209
210
211 void xran_build_owd_meas_ecpri_hdr(char* mbuf,    struct xran_ecpri_del_meas_cmn* eowdcmn)
212 {
213     union xran_ecpri_cmn_hdr *tmp= (union xran_ecpri_cmn_hdr*)mbuf;
214     /* Fill common header */
215     tmp->bits.ecpri_ver           = XRAN_ECPRI_VER;
216     tmp->bits.ecpri_resv          = 0;     // should be zero
217     tmp->bits.ecpri_concat        = 0;
218     tmp->bits.ecpri_mesg_type     = ECPRI_DELAY_MEASUREMENT;
219     tmp->bits.ecpri_payl_size    = 10 + eowdcmn->owdm_PlLength;
220     tmp->bits.ecpri_payl_size    = rte_cpu_to_be_16(tmp->bits.ecpri_payl_size);
221 }
222
223 void xran_add_at_and_measId_info_to_header(void* pbuf, uint8_t actionType, uint8_t MeasurementID)
224 {
225     struct xran_ecpri_delay_meas_pl* tmp = (struct xran_ecpri_delay_meas_pl*)pbuf;
226     // Fill ActionType and MeasurementId
227     tmp->ActionType = actionType;
228     tmp->MeasurementID = MeasurementID;
229 }
230
231 void xran_initialize_ecpri_del_meas_port(struct xran_ecpri_del_meas_cmn* pCmn, struct xran_ecpri_del_meas_port* pPort, uint16_t full)
232 {
233
234     uint16_t i=0;
235     // Initialize port parameters during the first pass
236     pPort->currentMeasID++;
237     pPort->runMeas = 1;
238     pPort->txDone = 0;
239
240     if (full)
241     {
242         pPort->numMeas = 0;
243         pPort->portid = pCmn->measVf;
244         pPort->delayAvg = 0;
245         pPort->delta = 0;
246         pPort->t1 = 0;
247         pPort->t2 = 0;
248         pPort->tr = 0;
249 #ifdef XRAN_OWD_DEBUG_MEAS_DB
250         printf("Clearing t1 and delta\n");
251 #endif
252
253         for (i=0; i < MX_NUM_SAMPLES; i++)
254         {
255             pPort->delaySamples[i] = 0;
256         }
257     }
258     // Set msState based on measMethod and whether the FHI is initiator or recipient
259
260     if (pCmn->initiator_en)
261     {
262         switch (pCmn->measMethod)
263         {
264             case XRAN_REQUEST:
265                 pPort->msState = XRAN_OWDM_WAITRESP;
266                 break;
267             case XRAN_REM_REQ:
268                 pPort->msState = XRAN_OWDM_WAITREQ;
269                 break;
270             case XRAN_REQ_WFUP:
271                 pPort->msState = XRAN_OWDM_WAITRESP;
272                 break;
273             case XRAN_REM_REQ_WFUP:
274                 pPort->msState = XRAN_OWDM_WAITREQWFUP;
275                 break;
276             default:
277                 pPort->msState = XRAN_OWDM_WAITRESP;
278                 break;
279         }
280     }
281     else
282     {
283         switch (pCmn->measMethod)
284         {
285             case XRAN_REQUEST:
286                 pPort->msState = XRAN_OWDM_WAITREQ;
287                 break;
288             case XRAN_REM_REQ:
289                 pPort->msState = XRAN_OWDM_WAITREMREQ;
290                 break;
291             case XRAN_REQ_WFUP:
292                 pPort->msState = XRAN_OWDM_WAITREQWFUP;
293                 break;
294             case XRAN_REM_REQ_WFUP:
295                 pPort->msState = XRAN_OWDM_WAITREMREQWFUP;
296                 break;
297             default:
298                 pPort->msState = XRAN_OWDM_WAITREQ;
299                 break;
300        }
301     }
302 }
303
304 int32_t xran_ecpri_port_update_required (struct xran_io_cfg * cfg, uint16_t port_id)
305 {
306     int32_t ret_value = 0;
307     int32_t* port = &cfg->port[0];
308
309     if (cfg != NULL)
310     {
311
312         struct xran_ecpri_del_meas_port* eowdp = &cfg->eowd_port[cfg->id][port_id];
313         struct xran_ecpri_del_meas_cmn*  eowdc = &cfg->eowd_cmn[cfg->id];
314
315
316         // Check if the current port has completed all the measurements to move to the next port
317         if (eowdp->numMeas == eowdc->numberOfSamples)
318         {
319             // Mark state as done and move to the next port
320             if (port_id < cfg->num_vfs)
321             {
322                 port_id++;
323                 if (port[port_id] == 0xFF)
324                 {
325                     // Done with all ports disable further execution
326                     eowdc->owdm_enable = 0;
327                 }
328                 else
329                 {
330                     eowdc->measVf++;
331                     eowdp= &cfg->eowd_port[cfg->id][port_id];
332                     // Initialize the next port
333 #ifdef XRAN_OWD_DEBUG_MEAS_DB
334                     printf("Init call_1 port %d\n", port_id);
335 #endif
336                     xran_initialize_ecpri_del_meas_port(eowdc, eowdp,1);
337                 }
338                 ret_value = 1;  // Wait for the next pass through the loop to go to the next port
339             }
340             else
341             {
342                 // Disable the measurements
343                 eowdc->owdm_enable = 0;
344                 ret_value = 1;
345             }
346         }
347         else
348         {
349             // Continue running on the same port
350             ret_value = 0;
351 //              xran_initialize_ecpri_del_meas_port(eowdc, eowdp,0); //Now this logic is driven by the receiver
352         }
353     }
354     else
355     {
356         errx(1, "Exit 1 epur with cfg null");
357     }
358     return ret_value;
359 }
360
361
362 /**
363  * @brief ecpri 2.0 one-way delay measurement transmitter control
364  *
365  * @ingroup group_source_xran
366  *
367  * @param port_id
368  *  port_id to be used
369  * @param handle
370  *  Pointer to an xran_device_ctx (cast)
371  *
372  * @return
373  *  OK on success
374  *  FAIL if failed to process the packet
375
376  */
377 int xran_ecpri_one_way_delay_measurement_transmitter(uint16_t port_id, void* handle)
378 {
379     // The ecpri one way delay measurement transmitter handles the transmission
380     // of the owd measurement packets on each of the vfs present in the system in a sequential order
381     // so the owd_meas_method is provided from the configuration file and it can be one of 4 possible
382     // methods: REQUEST, REM_REQ, REQ_WFUP or REM_REQ_WFUP
383     // In the current implementation the measurement is performed on one vf until completion of the number
384     // of measurements defined from the configuration file.
385     // A variable in the xran_ecpri_del_meas_cmn keeps track of the current vf that is using the transmitter and
386     // when the current vf completes all the measurements it moves to the next vf until all of the vfs complete
387     // the measurements
388     // In the current implementation the measurements start after the xran_if_current_state has reached the
389     // XRAN_RUNNING state (i.e. after having executed the xran_start())
390     // The measurements run only once for the current release.
391     int ret_value = FAIL;
392     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
393     struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
394     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
395
396     if (powdc->measState == OWDMTX_INIT)
397     {
398         // Perform the initialization for the very first call to the transmitter for a given port
399         powdc->measVf = port_id;
400         powdc->measState = OWDMTX_ACTIVE;
401         // Check whether PL length was passed in config file and if it is within bounds
402         if ((powdc->owdm_PlLength == 0)|| ( powdc->owdm_PlLength < MIN_OWDM_PL_LENGTH ))
403         {
404             // Use default length value
405             powdc->owdm_PlLength = MIN_OWDM_PL_LENGTH;
406         }
407         else if ( powdc->owdm_PlLength > MAX_OWDM_PL_LENGTH)
408         {
409             powdc->owdm_PlLength = MAX_OWDM_PL_LENGTH;
410         }
411 #ifdef XRAN_OWD_DEBUG_MEAS_DB
412         printf("Clear call 2 port_id %d\n", port_id);
413 #endif
414         xran_initialize_ecpri_del_meas_port(powdc, powdp,1);
415     }
416
417     // Initiator State Machine , recipient state machine driven from process_delay_meas()
418 //    printf("owdm tx w state %d runMeas %d inen %d\n", powdp->msState,powdp->runMeas,powdc->initiator_en);
419
420     if ((powdp->runMeas != 0 )&&(powdc->initiator_en != 0)) // Current port still running measurements
421     {
422         switch (powdp->msState)
423         {
424             case XRAN_OWDM_WAITRESP:
425                 // Check the measmethod to define the action
426                 if (powdc->measMethod == XRAN_REQUEST)
427                 {
428                     if (!powdp->txDone)
429                     {
430 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
431                         printf("owdm ecpri tx req gen\n");
432 #endif
433                         if (xran_generate_delay_meas(port_id, handle, (uint8_t)ECPRI_REQUEST, powdc->measId) == 0 )
434                         {
435                             errx(1, "Exit 1 owdm tx port_id %d measId %d", port_id, powdc->measId);
436                         }
437                         powdp->txDone =1;
438                     }
439                 }
440                 else
441                 {
442                     // The only else corresponds to XRAN_REQ_WFUP
443                     if (!powdp->txDone)
444                     {
445 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
446                         printf("owdm ecpri tx req w fup gen\n");
447 #endif
448                         if (xran_generate_delay_meas(port_id, handle, (uint8_t)ECPRI_REQUEST_W_FUP , powdc->measId) == 0 )
449                         {
450                             errx(1, "Exit 2 owdm tx port_id %d measId %d", port_id, powdc->measId );
451                         }
452                         powdp->txDone=0;            // Needs fup
453                     }
454                 }
455                 break;
456             case XRAN_OWDM_WAITREQ:
457                 if (!powdp->txDone)
458                 {
459 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
460                     printf("owdm ecpri tx rem req gen\n");
461 #endif
462                     if (xran_generate_delay_meas(port_id, handle, (uint8_t)ECPRI_REMOTE_REQ , powdc->measId) == 0 )
463                     {
464                         errx(1, "Exit 3 owdm tx port_id %d measId %d", port_id, powdc->measId );
465                     }
466                     powdp->txDone=1;
467                 }
468                 break;
469             case XRAN_OWDM_WAITREQWFUP:
470                 if (!powdp->txDone)
471                 {
472 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
473                     printf("owdm ecpri tx rem req w fup gen\n");
474 #endif
475                     if (xran_generate_delay_meas(port_id, handle, (uint8_t)ECPRI_REMOTE_REQ_W_FUP , powdc->measId) == 0 )
476                     {
477                         errx(1, "Exit 4 owdm tx port_id %d measId %d", port_id, powdc->measId );
478                     }
479                     powdp->txDone=1;
480                 }
481                 break;
482             case XRAN_OWDM_GENFUP:
483                 if (!powdp->txDone)
484                 {
485 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
486                     printf("owdm ecpri follow up gen\n");
487 #endif
488                     if (xran_generate_delay_meas(port_id, handle, (uint8_t)ECPRI_FOLLOW_UP , powdc->measId) == 0 )
489                     {
490                         errx(1, "Exit 4 owdm tx port_id %d measId %d", port_id, powdc->measId );
491                     }
492                     powdp->txDone=1;
493                 }
494                 break;
495             case XRAN_OWDM_WAITFUP:
496             case XRAN_OWDM_DONE:
497             case XRAN_OWDM_IDLE:
498                 // Transmitter doesn't have to do anything in these states
499                 break;
500             default:
501                 errx(1, "Exit 5 owdm tx port_id %d measId %d id %d state %d", port_id, powdc->measId, p_xran_dev_ctx->fh_init.io_cfg.id, powdp->msState );
502
503         }
504     }
505     ret_value = OK;
506     return ret_value;
507
508 }
509
510 /**
511  * @brief Generate a Delay Measurement packet
512  *  Transport layer fragmentation is not supported.
513  *
514  * @ingroup group_source_xran
515  *
516  * @param port_id
517  *  port_id to be used
518  * @param handle
519  *  Pointer to an xran_device_ctx (cast)
520  * @param actionType
521  * actionType to be used in the owd measurement packet
522  * @param MeasurementID
523  * MeasurementID to be populated in the owd measurement packet
524  * @return
525  *  OK on success
526  *  FAIL if failed to process the packet
527
528  */
529 int xran_generate_delay_meas(uint16_t port_id, void* handle, uint8_t actionType, uint8_t MeasurementID )
530 {
531     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
532     int pkt_len;
533     struct rte_mbuf *mbuf;
534     char* pChar;
535     struct xran_ecpri_delay_meas_pl * pdm= NULL;
536     uint64_t tcv1,tr2m,trm;
537     struct timespec tr2, tr;
538     struct xran_io_cfg* cfg = &p_xran_dev_ctx->fh_init.io_cfg;
539     struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
540     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
541     int32_t *port = &cfg->port[port_id];
542     int ret_value = FAIL;
543     struct rte_ether_addr addr;
544     uint16_t ethertype = ETHER_TYPE_ECPRI;
545
546 //    printf("in xran_generate_delay_meas for action_type %d\n", actionType);
547
548     pkt_len = sizeof(struct xran_ecpri_del_meas_pkt);
549     // Allocate a buffer from the pool
550     mbuf =xran_ethdi_mbuf_alloc();
551     if (mbuf == NULL)
552     {
553         MLogPrint(NULL);
554         errx(1,"exit 1 owdm gen");
555     }
556     pChar = rte_pktmbuf_append(mbuf, pkt_len);
557     if (pChar == NULL)
558     {
559         MLogPrint(NULL);
560         errx(1,"exit 2 owdm gen");
561     }
562     pChar = rte_pktmbuf_prepend(mbuf, sizeof(struct rte_ether_hdr));
563     if (pChar == NULL)
564     {
565         MLogPrint(NULL);
566         errx(1,"exit 3 owdm gen");
567     }
568
569     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
570
571     struct rte_ether_hdr *h = (struct rte_ether_hdr *)rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr*);
572     PANIC_ON(h == NULL, "mbuf prepend of ether_hdr failed");
573
574     /* Fill in the ethernet header. */
575     rte_eth_macaddr_get(port_id, &h->s_addr);          /* set source addr */
576
577     if (p_xran_dev_ctx->fh_init.io_cfg.id)
578     {
579 //        rte_ether_addr_copy( (struct rte_ether_addr *)p_xran_dev_ctx->fh_init.p_o_du_addr[port_id],&h->d_addr);
580         h->d_addr = ctx->entities[port_id][ID_O_DU];   /* set dst addr */
581     }
582     else
583     {
584         h->d_addr = ctx->entities[port_id][ID_O_RU];   /* set dst addr */
585 //        rte_ether_addr_copy( (struct rte_ether_addr *)p_xran_dev_ctx->fh_init.p_o_ru_addr[port_id],&h->d_addr);
586     }
587
588     h->ether_type = rte_cpu_to_be_16(ethertype);       /* ethertype too */
589     mbuf->port = ctx->io_cfg.port[port_id];
590
591
592     // Prepare the ecpri header info
593     // Advance pointer to the begining of the ecpri common header
594     pChar = pChar + sizeof (struct rte_ether_hdr);
595     xran_build_owd_meas_ecpri_hdr(pChar, powdc );
596     // Advance pointer to the begining of the xran_ecpri_delay_meas_pl
597     pChar  = pChar + sizeof (union xran_ecpri_cmn_hdr);
598     xran_add_at_and_measId_info_to_header(pChar, actionType, MeasurementID);
599
600     pdm  = (struct xran_ecpri_delay_meas_pl *)rte_pktmbuf_mtod_offset(mbuf, struct xran_ecpri_delay_meas_pl *, sizeof(struct rte_ether_hdr) + sizeof(union xran_ecpri_cmn_hdr));
601     switch (actionType)
602     {
603         // For owd meas originator there are a subset of actionTypes used see ecpri 2.0 Figures 25 and 26 for the details
604         case ECPRI_REQUEST:
605             // Record t1, prepare Request Message and determine tcv1 and include both time stamps in the packet
606             // 1) Record the current timestamp when the preparation of the message started i.e. t1
607             if (clock_gettime(CLOCK_REALTIME, &tr ))     // t1
608             {
609                 return ret_value;
610             }
611             trm = xran_timespec_to_ns(&tr);
612 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
613             printf("trm at gen is %8"PRIx64" \n", trm);
614 #endif
615             // 2) Prepare the delay measurement request packet
616             pdm->ActionType = ECPRI_REQUEST;
617             // 3) Record the current timestamp at the moment that the delay measurement packet is ready to be transmitted tr2 i.e.t1+tcv1 and write it
618             //    to the Delay Measurement request packet PL field
619             if (clock_gettime(CLOCK_REALTIME, &tr2 ))     // ts
620             {
621                 return ret_value;
622             }
623             // 4) Convert host to ptp time stamp format for tr and write to the outgoing packet
624             xran_host_to_ptp_ts(&pdm->ts, &tr);
625             // 5) Convert from Timestamp tr2 to ns before computing the compensation value
626             tr2m = xran_timespec_to_ns(&tr2);
627             // 6) Compute tcv1 as tr2m-trm
628             tcv1 = tr2m - trm;
629 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
630             printf("tcv1 is %08"PRIx64"\n",tcv1);
631 #endif
632
633             // 7) write tcv1 to the CompensationValue field of the delay measurement request packet
634             pdm->CompensationValue = rte_cpu_to_be_64(tcv1);
635 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
636             printf("compensation value after net order %8"PRIx64" \n", pdm->CompensationValue);
637 #endif
638             // 8) Store t1 and tcv1 to be used later once we get the response message
639             powdp->currentMeasID = pdm->MeasurementID;
640             powdp->t1 = trm;
641             powdp->delta = tcv1;
642             powdp->msState =   XRAN_OWDM_WAITRESP;
643 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
644             printf("At req gen t1 %8"PRIx64" and delta %8"PRIx64"  port %d  \n",powdp->t1,powdp->delta,port_id);
645 #endif
646             break;
647
648         case ECPRI_REMOTE_REQ:
649             // Prepare and send Remote Request Message with zero timestamp and correction values
650             tr.tv_sec = 0;
651             tr.tv_nsec = 0;
652             tcv1 = 0;
653             // Convert host to ptp time stamp format for tr and write to the outgoing packet
654             xran_host_to_ptp_ts(&pdm->ts, &tr);
655             // write zero to the CompensationValue field of the delay measurement remote request packet
656             pdm->CompensationValue = rte_cpu_to_be_64(tcv1);
657             // 1) Prepare the delay measurement request packet
658             pdm->ActionType = ECPRI_REMOTE_REQ;
659             // 2) Store MeasurementID and msState to be checked once the Request Message is received
660             powdp->currentMeasID = pdm->MeasurementID;
661             powdp->msState =   XRAN_OWDM_WAITREQ;
662
663             break;
664
665         case ECPRI_REQUEST_W_FUP:
666             // Record t1, prepare Request with follow up Message and determine tcv1, send zero timestamp and correction value  in the packet
667             // 1) Record the current timestamp when the message preparation started i.e. t1
668             if (clock_gettime(CLOCK_REALTIME, &tr ))     // t1
669             {
670                 return ret_value;
671             }
672             trm = xran_timespec_to_ns(&tr);
673             // 2) Prepare the delay measurement remote request with follow up packet
674             pdm->ActionType = ECPRI_REQUEST_W_FUP;
675             // 3) Record the current timestamp at the moment that the delay measurement packet is ready to be transmitted tr2 i.e.t1+tcv1
676             if (clock_gettime(CLOCK_REALTIME, &tr2 ))     // ts
677             {
678                 return ret_value;
679             }
680             // 4) Convert from Timestamp tr2 to ns before computing the compensation value
681             tr2m = xran_timespec_to_ns(&tr2);
682             // 5) Compute tcv1 as tr2m-trm
683             tcv1 = tr2m - trm;
684             // Prepare and send Remote Request Message with zero timestamp and correction values
685             tr.tv_sec = 0;
686             tr.tv_nsec = 0;
687             powdp->delta = tcv1; // Save tcv1 while waiting for the Response
688             tcv1 = 0;
689             // Convert host to ptp time stamp format for tr and write to the outgoing packet
690             xran_host_to_ptp_ts(&pdm->ts, &tr);
691             // write zero to the CompensationValue field of the delay measurement remote request packet
692             pdm->CompensationValue = rte_cpu_to_be_64(tcv1);
693             // 6) Store MeasurementID and msState to be checked once the Request Message is received
694             powdp->currentMeasID = pdm->MeasurementID;
695             powdp->t1 = trm;
696             powdp->msState =   XRAN_OWDM_GENFUP;
697
698             break;
699
700         case ECPRI_FOLLOW_UP:
701             // Use the t1 and tcv1 values recorded in the ECPRI_REQUEST_W_FUP packet generation step and send these values in the follow up packet
702             // 1) Prepare the delay measurement follow up packet
703             pdm->ActionType = ECPRI_FOLLOW_UP;
704             // 2) Convert t1 from host to ptp format
705             xran_ns_to_timespec(powdp->t1, &tr);
706             // 3) Convert host to ptp time stamp format for tr and write to the outgoing packet
707             xran_host_to_ptp_ts(&pdm->ts, &tr);
708             // 4) write tcv1 to the CompensationValue field of the delay measurement request packet
709             pdm->CompensationValue = rte_cpu_to_be_64(powdp->delta);
710             powdp->currentMeasID = pdm->MeasurementID;
711             powdp->msState =   XRAN_OWDM_WAITRESP;
712             break;
713
714         case ECPRI_REMOTE_REQ_W_FUP:
715             // Prepare the Remote Request with follow up Message, send zero timestamp and correction value  in the packet
716             tr.tv_sec = 0;
717             tr.tv_nsec = 0;
718             tcv1 = 0;
719             // Convert host to ptp time stamp format for tr and write to the outgoing packet
720             xran_host_to_ptp_ts(&pdm->ts, &tr);
721             // write zero to the CompensationValue field of the delay measurement remote request packet
722             pdm->CompensationValue = rte_cpu_to_be_64(tcv1);
723             // 1) Prepare the delay measurement request packet
724             pdm->ActionType = ECPRI_REMOTE_REQ_W_FUP;
725             // 2) Store MeasurementID and msState to be checked once the Request Message is received
726             powdp->currentMeasID = pdm->MeasurementID;
727             powdp->msState =   XRAN_OWDM_WAITREQWFUP;
728
729             break;
730
731         default:
732             errx(1,"exit 4 owdm gen");
733             break;
734     }
735
736  //   printf("xran_gen_del_4n");
737
738     // Retrieve Ethernet Header for the port and copy to the packet
739     rte_eth_macaddr_get(port_id, &addr);
740 #ifdef XRAN_OWD_DEBUG_PKTS
741     printf("id is %d\n", p_xran_dev_ctx->fh_init.io_cfg.id);
742     printf("Port %u SRC MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
743         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
744         (unsigned)port_id,
745         addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2],
746         addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5]);
747 #endif
748
749     if (p_xran_dev_ctx->fh_init.io_cfg.id)
750     {
751 #ifdef XRAN_OWD_DEBUG_PKTS
752         int8_t *pa = &p_xran_dev_ctx->fh_init.p_o_du_addr[0];
753         printf("DST_MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", pa[0],pa[1],pa[2],pa[3],pa[4],pa[5]);
754 #endif
755         rte_ether_addr_copy((struct rte_ether_addr *)&p_xran_dev_ctx->fh_init.p_o_du_addr[0], (struct rte_ether_addr *)&h->d_addr.addr_bytes[0]);
756
757     }
758     else
759     {
760 #ifdef XRAN_OWD_DEBUG_PKTS
761         int8_t *pb = &p_xran_dev_ctx->fh_init.p_o_ru_addr[0];
762         printf("DST_MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", pb[0],pb[1],pb[2],pb[3],pb[4],pb[5]);
763 #endif
764         rte_ether_addr_copy((struct rte_ether_addr *)&p_xran_dev_ctx->fh_init.p_o_ru_addr[0], (struct rte_ether_addr *)&h->d_addr.addr_bytes[0]);
765
766     }
767 #ifdef XRAN_OWD_DEBUG_PKTS
768     uint8_t *pc = &h->s_addr.addr_bytes[0];
769     printf(" Src MAC from packet: %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", pc[0],pc[1],pc[2],pc[3],pc[4],pc[5]);
770     uint8_t *pd = &h->d_addr.addr_bytes[0];
771     printf(" Dst MAC from packet: %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", pd[0],pd[1],pd[2],pd[3],pd[4],pd[5]);
772 #endif
773     // Copy dest address from above
774     // Send out the packet
775     ret_value = rte_eth_tx_burst((uint16_t)*port, 0, &mbuf, 1);
776 // Try using the normal scheme of passing through the ring
777 //    ret_value = xran_enqueue_mbuf(mbuf, ctx->tx_ring[port_id]);
778 #ifdef XRAN_OWD_DEBUG_PKTS
779     printf("owdt rte_eth_tx_burst returns %d for port %d\n", ret_value,port_id);
780 #endif
781     return ret_value;
782 }
783
784
785 /**
786  * @brief Process a Delay Measurement Request packet
787  *
788  * @ingroup group_source_xran
789  *
790  * @param mbuf
791  *  The pointer of the packet buffer to be processed
792  * @param handle
793  *  Pointer to an xran_device_ctx (cast)
794  * @param xran_ecpri_delay_meas_pl
795  * Pointer to an eCPRI delay measurement PL
796  * @return
797  *  OK on success
798  *  FAIL if failed to process the packet
799  */
800 int xran_process_delmeas_request(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt* ptr, uint16_t port_id)
801 {
802     int ret_value = FAIL;
803     TimeStamp pt1;
804     struct rte_mbuf* pkt1;
805     //char* pchar;
806     uint64_t tcv1, tcv2,t2m,trm, td12, t1m;
807     struct xran_ecpri_del_meas_pkt *pdm= NULL;
808     struct timespec tr, t2;
809     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
810     struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
811     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
812     struct rte_ether_hdr *eth_hdr;
813     struct rte_ether_addr addr;
814     //struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
815 //101620
816     struct xran_io_cfg* cfg = &p_xran_dev_ctx->fh_init.io_cfg;
817 //    struct xran_io_cfg *cfg = &ctx->io_cfg;
818     int32_t *port = &cfg->port[port_id];
819
820 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
821     printf("RX ecpri  Measure Request \n");
822 #endif
823     // Since we are processing the receipt of a delay measurement request packet the following actions
824     // need to be taken (Per eCPRI V2.0 Figure 25)
825     // 1) Record the current timestamp when the message was received i.e. tr
826     if (clock_gettime(CLOCK_REALTIME, &tr ))     // tr
827     {
828         errx(1, "Exit 1 owd rx f1 port_id %d", port_id);
829         return ret_value;
830     }
831
832     trm = xran_timespec_to_ns(&tr);
833     // 2) Copy MeasurementID to the Delay Measurement Response packet
834     //     but first prepend ethernet header since the info is still in the buffer
835 //    pchar = rte_pktmbuf_prepend(pkt, (uint16_t)(sizeof(struct rte_ether_hdr)+ sizeof(union xran_ecpri_cmn_hdr ))); // Pointer to new data start address 10/20/20 Now not removing ecpri_cmn in process_delay_meas
836     /*pchar = */rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(struct rte_ether_hdr));
837     pkt1 = rte_pktmbuf_copy(pkt, _eth_mbuf_pool, 0, UINT32_MAX);
838     pdm = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt*, sizeof(struct rte_ether_hdr));
839     // 3) Get time stamp T1 from the Timestamp field i.e. t1
840     pt1  = pdm->deMeasPl.ts;
841     // 3a) Convert to ns in the host format
842     t1m = xran_ptp_ts_to_ns(&pt1);
843     // 4) Get the compensation value from the packet i.e. tcv1
844     tcv1 = rte_be_to_cpu_64(pdm->deMeasPl.CompensationValue);
845     // 5) Prepare the delay measurement response packet
846     pdm->deMeasPl.ActionType = ECPRI_RESPONSE;
847     // 6) Record the current timestamp at the moment that the delay measurement packet is ready to be transmitted i.e.t2 and write it
848     //    to the Delay Measurement response packet PL field
849     if (clock_gettime(CLOCK_REALTIME, &t2 ))     // t2
850     {
851         errx(1,"Exit 2 owd rx f1 port_id %d", port_id);
852         return ret_value;
853     }
854     // 7) Convert host to ptp time stamp format for t2 and write to the outgoing packet
855     xran_host_to_ptp_ts(&pdm->deMeasPl.ts, &t2);
856     // 8) Convert from Timestamp t2 to ns before computing the compensation value
857     t2m = xran_timespec_to_ns(&t2);
858     // 9) Compute tcv2 as t2-tr
859     tcv2 = t2m - trm;
860     // 10) write cv2 to the CompensationValue field of the delay measurement response packet
861     pdm->deMeasPl.CompensationValue = rte_cpu_to_be_64(tcv2);
862     // 11) Fill the ethernet header properly by swapping src and dest addressed from the copied frame
863     eth_hdr = rte_pktmbuf_mtod(pkt1, struct rte_ether_hdr *);
864     /* Swap dest and src mac addresses. */
865     rte_ether_addr_copy(&eth_hdr->d_addr, &addr);
866     rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
867     rte_ether_addr_copy(&addr, &eth_hdr->s_addr);
868     // Still need to check ol_flags state and update if necessary
869     // Compute the delay td12 and save
870     // Still need to define the DB to save the info and run averages
871     td12 = t2m - tcv2 - (t1m + tcv1);
872     // 12) Send the response right away
873 #ifdef XRAN_OWD_DEBUG_PKTS
874     struct rte_ether_hdr *h = (struct rte_ether_hdr *)rte_pktmbuf_mtod(pkt1, struct rte_ether_hdr*);
875     uint8_t *pc = &h->s_addr.addr_bytes[0];
876     printf(" Src MAC from packet: %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", pc[0],pc[1],pc[2],pc[3],pc[4],pc[5]);
877     uint8_t *pd = &h->d_addr.addr_bytes[0];
878     printf(" Dst MAC from packet: %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n", pd[0],pd[1],pd[2],pd[3],pd[4],pd[5]);
879 //    printf("EtherType: %04"PRIx16" \n",&h->ether_type);
880 #endif
881     pdm  = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt *, sizeof(struct rte_ether_hdr) );
882     pdm->cmnhdr.bits.ecpri_payl_size     = 10 + powdc->owdm_PlLength; // 10 correponds to the xran_ecpri_delay_meas_pl minus the dummy_bytes field which now allows the user to select the length for this field to be sent
883     pdm->cmnhdr.bits.ecpri_payl_size    = rte_cpu_to_be_16(pdm->cmnhdr.bits.ecpri_payl_size);
884     pdm->cmnhdr.bits.ecpri_mesg_type = ECPRI_DELAY_MEASUREMENT;
885 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
886     printf ("pdm has:%02"PRIx8" %04"PRIx16" %02"PRIx8" %02"PRIx8" \n", pdm->cmnhdr.bits.ecpri_mesg_type, pdm->cmnhdr.bits.ecpri_payl_size, pdm->cmnhdr.bits.ecpri_ver,pdm->deMeasPl.MeasurementID);
887 #endif
888
889     // Copy dest address from above
890     ret_value = rte_eth_tx_burst((uint16_t)*port, 0, &pkt1, 1);  // Need to check for the proper method of getting the port and mac address
891 #ifdef  XRAN_OWD_DEBUG_MSG_FLOW
892     printf ("in dly ms req sending response rte_eth_tx_burst returns %d for port %d\n",ret_value, *port);
893 #endif
894     // 13) Update measurements DB and check if completed
895     powdp->delaySamples[powdp->numMeas]= td12 ;
896 #ifdef XRAN_OWD_DEBUG_DELAY_INFO
897     printf("Computed delay is %08"PRIx64" MeasNum %d portId %d id is %d \n",powdp->delaySamples[powdp->numMeas],powdp->numMeas, port_id, p_xran_dev_ctx->fh_init.io_cfg.id);
898 #endif
899
900     powdp->numMeas++;
901
902     if (powdp->numMeas == powdc->numberOfSamples)
903     {
904         xran_compute_and_report_delay_estimate(powdp, powdc->numberOfSamples, p_xran_dev_ctx->fh_init.io_cfg.id);
905         powdp->msState = XRAN_OWDM_DONE;
906         xran_if_current_state = XRAN_RUNNING;
907     }
908     else
909     {
910
911 //        powdp->msState = XRAN_OWDM_IDLE;
912         if (powdc->initiator_en)
913         {
914             // Reinitialize txDone for next pass
915             powdp->txDone = 0;
916 #ifdef XRAN_OWD_DEBUG_MEAS_DB
917             printf("Clear call 3 port id %d \n", port_id);
918 #endif
919             xran_initialize_ecpri_del_meas_port(powdc, powdp,0);
920         }
921     }
922     return 1;
923
924 }
925
926 int xran_process_delmeas_request_w_fup(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt* ptr, uint16_t port_id)
927 {
928     int ret_value = FAIL;
929     uint64_t trm;
930     struct xran_ecpri_del_meas_pkt* pdm= ptr;
931     struct timespec tr;
932     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
933     //struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
934     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
935     //struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
936     //struct xran_io_cfg *cfg = &ctx->io_cfg;
937     //int32_t* port = &cfg->port[port_id];
938
939     // Since we are processing the receipt of a delay measurement request with follow up packet the following actions
940     // need to be taken (Per eCPRI V2.0 Figure 26)
941 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
942     printf("RX ecpri  Measure Request with fup\n");
943 #endif
944
945     pdm = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod(pkt, struct xran_ecpri_del_meas_pkt*);
946     // Record tr and save to memory with the associated measurement Id and Port
947     // 1) Record the current timestamp when the message was received i.e. tr
948     if (clock_gettime(CLOCK_REALTIME, &tr ))     // tr
949     {
950         errx(1, "Exit 1 owd rx f2 port_id %d",port_id);
951         return ret_value;
952     }
953     trm = xran_timespec_to_ns(&tr);
954     // Save trm so when the Follow Up packet is received we can compute tcv2 as t2-trm
955     powdp->tr = trm;
956     // Save the measurement Id
957     powdp->currentMeasID = pdm->deMeasPl.MeasurementID;
958     // Change the state to waiting for follow up
959     powdp->msState = XRAN_OWDM_WAITFUP;
960
961     return ret_value;
962
963 }
964
965 int xran_process_delmeas_response(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt* ptr, uint16_t port_id)
966 {
967     int ret_value = 1;
968     TimeStamp pt2;
969     uint64_t tcv2,t2m;
970     struct xran_ecpri_del_meas_pkt* pdm;
971     //struct timespec t2;
972     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
973     struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
974     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
975     //struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
976     //struct xran_io_cfg *cfg = &ctx->io_cfg;
977     //struct xran_io_cfg* cfg1 = &p_xran_dev_ctx->fh_init.io_cfg;
978     //int32_t* port = &cfg->port[port_id];
979
980
981     // Since we are processing the receipt of a delay measurement response packet the following actions
982     // need to be taken (Per eCPRI V2.0 Figure 25)
983     // Need to know if a Remote Request was processed against this measurement ID if so then the receipt of the response
984     // is used to compute the one-way delay as td= (t2-tcv2) - (t1+tcv1) with t2, tcv2 contained in the packet and
985     // t1 and tcv1 stored from the previous Remote Request packet processing task
986 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
987     printf("RX ecpri  Measure Response \n");
988 #endif
989
990     pdm = (struct xran_ecpri_del_meas_pkt*)(struct xran_ecpri_del_meas_pkt *)rte_pktmbuf_mtod(pkt,  struct xran_ecpri_del_meas_pkt *);
991     // Save the measurement Id
992     powdp->currentMeasID = pdm->deMeasPl.MeasurementID;
993
994     // 1) Get time stamp T2 from the Timestamp field i.e. t2
995     pt2  = pdm->deMeasPl.ts;
996
997     // 2a) Convert to ns in the host format
998     t2m = xran_ptp_ts_to_ns(&pt2);
999     // 3) Get the compensation value from the packet i.e. tcv2
1000     tcv2 = rte_be_to_cpu_64(pdm->deMeasPl.CompensationValue);
1001 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
1002     printf ("tcv2 at Gen is %08"PRIx64" \n",tcv2);
1003 #endif
1004     // Compute the delay using the stored t1 and tcv1 used in the request message
1005     // td= (t2-tcv2) - (t1+tcv1) where t1 and tcv1 have been stored previously for the same measurement ID
1006 #ifdef XRAN_OWD_DEBUG_TIME_STAMPS_INFO
1007     printf("Delay comp at orig has t2m %08"PRIx64"  tcv2 %08"PRIx64" t1 %08"PRIx64" delta %08"PRIx64" port_id %d \n", t2m,tcv2,powdp->t1 ,powdp->delta,port_id);
1008 #endif
1009     powdp->delaySamples[powdp->numMeas]= (t2m-tcv2) -(powdp->t1 + powdp->delta);
1010 #ifdef XRAN_OWD_DEBUG_DELAY_INFO
1011         printf("Computed delay is %08"PRIx64" MeasNum %d portId %d id is %d \n",powdp->delaySamples[powdp->numMeas],powdp->numMeas, port_id,p_xran_dev_ctx->fh_init.io_cfg.id );
1012 #endif
1013
1014     powdp->numMeas++;
1015
1016
1017
1018     if (powdp->numMeas == powdc->numberOfSamples)
1019     {
1020         xran_compute_and_report_delay_estimate(powdp, powdc->numberOfSamples,p_xran_dev_ctx->fh_init.io_cfg.id);
1021         powdp->msState = XRAN_OWDM_DONE;
1022         xran_if_current_state= XRAN_RUNNING;
1023     }
1024     else
1025     {
1026
1027 //        powdp->msState = XRAN_OWDM_IDLE;
1028         if (powdc->initiator_en)
1029         {
1030             // Reinitialize txDone for next pass
1031             powdp->txDone = 0;
1032 #ifdef XRAN_OWD_DEBUG_MEAS_DB
1033             printf("Clear call_4 port_id %d \n", port_id);
1034 #endif
1035             xran_initialize_ecpri_del_meas_port(powdc, powdp,0);
1036 #ifdef XRAN_OWD_DEBUG_MEAS_DB
1037             printf("Reseting done \n");
1038 #endif
1039
1040         }
1041
1042     }
1043     // Needs work and change ret_value to OK
1044     return ret_value;
1045 }
1046
1047 int xran_process_delmeas_rem_request(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt* ptr, uint16_t port_id)
1048 {
1049     int ret_value = FAIL;
1050     struct rte_mbuf* pkt1;
1051     uint64_t tcv1,tr2m,trm;
1052     struct xran_ecpri_del_meas_pkt* pdm;
1053     //char* pchar;
1054     struct timespec tr2, tr;
1055     struct rte_ether_hdr *eth_hdr;
1056     struct rte_ether_addr addr;
1057     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
1058     struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
1059     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
1060     struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
1061     struct xran_io_cfg *cfg = &ctx->io_cfg;
1062     int32_t* port = &cfg->port[port_id];
1063
1064     // Since we are processing the receipt of a delay measurement remote request packet the following actions
1065     // need to be taken (Per eCPRI V2.0 Figure 25)
1066 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
1067     printf("RX ecpri  Measure Remote Request \n");
1068 #endif
1069
1070     // 1) Record the current timestamp when the message was received i.e. t1
1071     if (clock_gettime(CLOCK_REALTIME, &tr ))     // t1
1072     {
1073         errx(1,"Exit 1 owd rx f4 port_id %d", port_id);
1074         return ret_value;
1075     }
1076     trm = xran_timespec_to_ns(&tr);
1077     // 2) Copy MeasurementID to the Delay Measurement Request packet
1078     //     but first prepend ethernet header since the info is still in the buffer
1079     /*pchar = */rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(struct rte_ether_hdr));
1080     pkt1 = rte_pktmbuf_copy(pkt, _eth_mbuf_pool, 0, UINT32_MAX);
1081     pdm = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt*, sizeof(struct rte_ether_hdr));
1082
1083     // 3) Prepare the delay measurement request packet
1084     pdm->deMeasPl.ActionType = ECPRI_REQUEST;
1085     // 4) Record the current timestamp at the moment that the delay measurement packet is ready to be transmitted tr2 i.e.t1+tcv1 and write it
1086     //    to the Delay Measurement request packet PL field
1087     if (clock_gettime(CLOCK_REALTIME, &tr2 ))     // tr2
1088     {
1089         errx(1,"Exit 2 owd rx f4 port_id %d", port_id);
1090         return ret_value;
1091     }
1092     // 5) Convert host to ptp time stamp format for tr2 and write to the outgoing packet
1093     xran_host_to_ptp_ts(&pdm->deMeasPl.ts, &tr);
1094     // 6) Convert from Timestamp tr2 to ns before computing the compensation value
1095     tr2m = xran_timespec_to_ns(&tr2);
1096     // 7) Compute tcv1 as tr2m-trm
1097     tcv1 = tr2m - trm;
1098     // 8) write tcv1 to the CompensationValue field of the delay measurement request packet
1099     pdm->deMeasPl.CompensationValue = rte_cpu_to_be_64(tcv1);
1100     // 9) Fill the ethernet header properly by swapping src and dest addressed from the copied frame
1101     eth_hdr = rte_pktmbuf_mtod(pkt1, struct rte_ether_hdr *);
1102     /* Swap dest and src mac addresses. */
1103     rte_ether_addr_copy(&eth_hdr->d_addr, &addr);
1104     rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
1105     rte_ether_addr_copy(&addr, &eth_hdr->s_addr);
1106     // 10) Send the response right away
1107     pdm  = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt *, sizeof(struct rte_ether_hdr) );
1108     pdm->cmnhdr.bits.ecpri_payl_size     = 10 + powdc->owdm_PlLength; // 10 correponds to the xran_ecpri_delay_meas_pl minus the dummy_bytes field which now allows the user to select the length for this field to be sent
1109     pdm->cmnhdr.bits.ecpri_payl_size    = rte_cpu_to_be_16(pdm->cmnhdr.bits.ecpri_payl_size);
1110     pdm->cmnhdr.bits.ecpri_mesg_type = ECPRI_DELAY_MEASUREMENT;
1111 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
1112     printf("Ecpri  Measure Sending Request Msg \n");
1113 #endif
1114     ret_value = rte_eth_tx_burst((uint16_t)*port, 0, &pkt1, 1);  // Need to check for the proper method of getting the port and mac address
1115     // Still need to check ol_flags state and update if necessary
1116     // Save the computed delays and the measurementId
1117     powdp->t1 = trm;
1118     powdp->delta = tcv1;
1119     powdp->currentMeasID = pdm->deMeasPl.MeasurementID;
1120     powdp->msState = XRAN_OWDM_WAITRESP;
1121     return ret_value;
1122
1123
1124 }
1125 int xran_process_delmeas_rem_request_w_fup(struct rte_mbuf* pkt, void* handle, struct xran_ecpri_del_meas_pkt* ptr, uint16_t port_id)
1126 {
1127     int ret_value = FAIL;
1128     struct rte_mbuf* pkt1;
1129     struct rte_mbuf* pkt2;
1130     uint64_t tcv1,tsm,t1;
1131     struct rte_ether_hdr *eth_hdr;
1132     struct rte_ether_addr addr;
1133     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
1134     //struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
1135     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
1136     struct xran_ecpri_del_meas_pkt* pdm;
1137     struct timespec tr, ts;
1138     //char* pchar;
1139
1140
1141     struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
1142     struct xran_io_cfg *cfg = &ctx->io_cfg;
1143     int32_t* port = &cfg->port[port_id];
1144     tsm = 0;
1145
1146     // Since we are processing the receipt of a delay measurement remote request with follow up packet the following
1147     // actions need to be taken (Per eCPRI V2.0 Figure 26)
1148     // record t1 for the packet arrival time and then prepare Request with follow up packet which uses 0 for timsetamp
1149     // and for correctionvalue.
1150 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
1151     printf("RX ecpri  Measure Remote Request w Fup \n");
1152 #endif
1153     // 1) Record the current timestamp when the message was received i.e. t1
1154     if (clock_gettime(CLOCK_REALTIME, &tr ))     // t1
1155     {
1156         errx(1,"Exit 1 owd rx f5 port_id %d", port_id);
1157         return ret_value;
1158     }
1159     t1 = xran_timespec_to_ns(&tr);
1160     // 2) Copy MeasurementID to the Delay Measurement Request packet
1161     //     but first prepend ethernet header since the info is still in the buffer
1162     /*pchar = */rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(struct rte_ether_hdr));
1163     pkt1 = rte_pktmbuf_copy(pkt, _eth_mbuf_pool, 0, UINT32_MAX);
1164
1165     pdm = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt*, sizeof(struct rte_ether_hdr));
1166
1167
1168     // 3) Prepare the delay measurement request w fup packet
1169     pdm->deMeasPl.ActionType = ECPRI_REQUEST_W_FUP;
1170     // 4) Zero the ts and CompensationValue entries in the packet
1171     ts.tv_sec=0;
1172     ts.tv_nsec =0;
1173     // 5) Convert host to ptp time stamp format for t2 and write to the outgoing packet
1174     xran_host_to_ptp_ts(&pdm->deMeasPl.ts, &ts);
1175     // 6) write zero to the CompensationValue field of the delay measurement response packet
1176     pdm->deMeasPl.CompensationValue = rte_cpu_to_be_64(tsm);
1177     // 7) Fill the ethernet header properly by swapping src and dest addressed from the copied frame
1178     eth_hdr = rte_pktmbuf_mtod(pkt1, struct rte_ether_hdr *);
1179     /* Swap dest and src mac addresses. */
1180     rte_ether_addr_copy(&eth_hdr->d_addr, &addr);
1181     rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
1182     rte_ether_addr_copy(&addr, &eth_hdr->s_addr);
1183     // 8) Duplicate packet to be used for the follow up packet
1184     pkt2 = rte_pktmbuf_copy(pkt1, _eth_mbuf_pool, 0, UINT32_MAX);
1185     // 9) Record the current timestamp when the request with follow up is being sent
1186     if (clock_gettime(CLOCK_REALTIME, &ts ))     // ts
1187     {
1188         errx(1,"Exit 2 owd rx f5 port_id %d", port_id);
1189         return ret_value;
1190     }
1191     // 10) Send the request with follow up
1192 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
1193     printf("ecpri Measure sending Request with Fup \n");
1194 #endif
1195     ret_value = rte_eth_tx_burst((uint16_t)*port, 0, &pkt1, 1);  // Need to check for the proper method of getting the port and mac address
1196
1197     // After the Request with follow up packet has been sent, prepare follow up packet with t1 and tcv1, where
1198     // tcv1 = ts - t1 and writing it to the outgoing packet
1199     pdm = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt2, struct xran_ecpri_del_meas_pkt*, sizeof(struct rte_ether_hdr));
1200     // 11) Prepare the delay measurement request with follow up packet
1201     pdm->deMeasPl.ActionType = ECPRI_FOLLOW_UP;
1202     // 12) Convert host to ptp time stamp format for t1 and write to the outgoing packet
1203     xran_host_to_ptp_ts(&pdm->deMeasPl.ts, &tr);
1204     // 13) Convert from Timestamp t2 to ns before computing the compensation value
1205     tsm = xran_timespec_to_ns(&ts);
1206     // 14) Compute tcv1 as tsm-t1
1207     tcv1 = tsm - t1;
1208     // 15) write cv1 to the CompensationValue field of the delay measurement response packet
1209     pdm->deMeasPl.CompensationValue = rte_cpu_to_be_64(tcv1);
1210
1211     // 16) Send the follow up message
1212 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
1213     printf("ecpri Measure sending Follow Up \n");
1214 #endif
1215     ret_value = rte_eth_tx_burst((uint16_t)*port, 0, &pkt2, 1);  // Need to check for the proper method of getting the port and mac address
1216
1217     // Save trm since it will be used to compute tcv2 based on the arrival of the Follow Up packet
1218     powdp->currentMeasID = pdm->deMeasPl.MeasurementID;
1219     powdp->t1 = t1;
1220     powdp->delta = tcv1;
1221     powdp->msState =   XRAN_OWDM_WAITRESP;
1222
1223     return ret_value;
1224
1225 }
1226
1227 int xran_process_delmeas_follow_up(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt* ptr, uint16_t port_id)
1228 {
1229     int ret_value = FAIL;
1230     struct rte_mbuf *pkt1;
1231     //char* pChar= NULL;
1232     uint64_t tcv1,tr2m, tcv2, t1;
1233     struct xran_ecpri_del_meas_pkt *pdm;
1234     struct timespec tr2;
1235     struct rte_ether_hdr *eth_hdr;
1236     struct rte_ether_addr addr;
1237     TimeStamp pt1;
1238     struct xran_device_ctx* p_xran_dev_ctx = (struct xran_device_ctx *)handle;
1239     struct xran_ecpri_del_meas_cmn* powdc  = &p_xran_dev_ctx->fh_init.io_cfg.eowd_cmn[p_xran_dev_ctx->fh_init.io_cfg.id];
1240     struct xran_ecpri_del_meas_port* powdp = &p_xran_dev_ctx->fh_init.io_cfg.eowd_port[p_xran_dev_ctx->fh_init.io_cfg.id][port_id];
1241     struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
1242     struct xran_io_cfg *cfg = &ctx->io_cfg;
1243     int32_t *port = &cfg->port[0];
1244     // Since we are processing the receipt of a delay measurement follow up packet the following actions
1245     // need to be taken (Per eCPRI V2.0 Figure 26)
1246 #ifdef XRAN_OWD_DEBUG_MSG_FLOW
1247     printf("ecpri Measure received Followup \n");
1248 #endif
1249
1250     // 1) Record the current timestamp when the message was received i.e. tr2
1251     if (clock_gettime(CLOCK_REALTIME, &tr2 ))     // tr2
1252     {
1253         errx(1,"Exit 1 owd rx f6 port_id %d", port_id);
1254         return ret_value;
1255     }
1256     tr2m = xran_timespec_to_ns(&tr2);
1257
1258
1259     // 2) Copy MeasurementID to the Delay Measurement Response packet
1260     //     but first prepend ethernet header since the info is still in the buffer
1261     /*pChar = */rte_pktmbuf_prepend(pkt, (uint16_t)sizeof(struct rte_ether_hdr));
1262     pkt1 = rte_pktmbuf_copy(pkt, _eth_mbuf_pool, 0, UINT32_MAX);
1263     pdm = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt*, sizeof(struct rte_ether_hdr));
1264
1265     // 3) Get time stamp T1 from the Timestamp field i.e. t1
1266     pt1  = pdm->deMeasPl.ts;
1267     // 4) Convert to ns in the host format
1268     t1 = xran_ptp_ts_to_ns(&pt1);
1269     // 5) Get the compensation value from the packet i.e. tcv1
1270     tcv1 = rte_be_to_cpu_64(pdm->deMeasPl.CompensationValue);
1271
1272     // 6) Prepare the delay measurement response packet
1273     pdm->deMeasPl.ActionType = ECPRI_RESPONSE;
1274
1275     // 7) Convert host to ptp time stamp format for tr2 and write to the outgoing packet
1276     xran_host_to_ptp_ts(&pdm->deMeasPl.ts, &tr2);
1277     // 8) Convert from Timestamp tr2 to ns before computing the compensation value
1278     tr2m = xran_timespec_to_ns(&tr2);
1279     // 9) Compute tcv2 as tr2m-trm
1280     tcv2 = tr2m - powdp->tr;
1281     // 0) write tcv2 to the CompensationValue field of the delay measurement request packet
1282     pdm->deMeasPl.CompensationValue = rte_cpu_to_be_64(tcv2);
1283     // 9) Fill the ethernet header properly by swapping src and dest addressed from the copied frame
1284     eth_hdr = rte_pktmbuf_mtod(pkt1, struct rte_ether_hdr *);
1285     /* Swap dest and src mac addresses. */
1286     rte_ether_addr_copy(&eth_hdr->d_addr, &addr);
1287     rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
1288     rte_ether_addr_copy(&addr, &eth_hdr->s_addr);
1289     pdm  = (struct xran_ecpri_del_meas_pkt*)rte_pktmbuf_mtod_offset(pkt1, struct xran_ecpri_del_meas_pkt *, sizeof(struct rte_ether_hdr) );
1290     pdm->cmnhdr.bits.ecpri_payl_size     = 10 + powdc->owdm_PlLength; // 10 correponds to the xran_ecpri_delay_meas_pl minus the dummy_bytes field which now allows the user to select the length for this field to be sent
1291     pdm->cmnhdr.bits.ecpri_payl_size    = rte_cpu_to_be_16(pdm->cmnhdr.bits.ecpri_payl_size);
1292     pdm->cmnhdr.bits.ecpri_mesg_type = ECPRI_DELAY_MEASUREMENT;
1293
1294     // 10) Send the response right away
1295     ret_value = rte_eth_tx_burst((uint16_t)*port, 0, &pkt1, 1);  // Need to check for the proper method of getting the port and mac address
1296
1297     // Compute the delay using the stored t1 and tcv1 used in the request message
1298     // td= (t2-tcv2) - (t1+tcv1) where t1 and tcv1 have been stored previously for the same measurement ID
1299     powdp->delaySamples[powdp->numMeas]= (tr2m-tcv2) -(t1 + tcv1);
1300 #ifdef XRAN_OWD_DEBUG_DELAY_INFO
1301     printf("Computed delay is %08"PRIx64" MeasNum %d portId %d id %d \n",powdp->delaySamples[powdp->numMeas],powdp->numMeas,port_id,p_xran_dev_ctx->fh_init.io_cfg.id);
1302 #endif
1303     powdp->numMeas++;
1304
1305     if (powdp->numMeas == powdc->numberOfSamples)
1306     {
1307         xran_compute_and_report_delay_estimate(powdp, powdc->numberOfSamples, p_xran_dev_ctx->fh_init.io_cfg.id);
1308         powdp->msState = XRAN_OWDM_DONE;
1309         xran_if_current_state = XRAN_RUNNING;
1310     }
1311     else
1312     {
1313 ;
1314 //        powdp->msState = XRAN_OWDM_IDLE;
1315         if (powdc->initiator_en)
1316         {
1317             // Reinitialize txDone for next pass
1318             powdp->txDone = 0;
1319 #ifdef XRAN_OWD_DEBUG_MEAS_DB
1320             printf("Clear Call_5 port_id %d \n", port_id);
1321 #endif
1322             xran_initialize_ecpri_del_meas_port(powdc, powdp,0);
1323         }
1324     }
1325
1326     return ret_value;
1327
1328 }
1329
1330
1331 /**
1332  * @brief Parse a Delay Measurement packet
1333  *  Transport layer fragmentation is not supported.
1334  *
1335  * @ingroup group_source_xran
1336  *
1337  * @param mbuf
1338  *  The pointer of the packet buffer to be parsed
1339  * @param handle
1340  *  Pointer to an xran_device_ctx (cast)
1341  * @return
1342  *  OK on success
1343  *  FAIL if failed to process the packet
1344  */
1345 int process_delay_meas(struct rte_mbuf *pkt,  void* handle, uint16_t port_id)
1346 {
1347     struct xran_device_ctx * p_xran_dev_ctx = (struct xran_device_ctx *)handle;
1348     struct xran_ecpri_del_meas_pkt *ecpri_delmeas_pkt;
1349     //union  xran_ecpri_cmn_hdr * ecpricmn;
1350     int ret_value = FAIL;
1351 #ifdef XRAN_OWD_DEBUG_PKTS
1352     printf("pdm Device is %d\n", p_xran_dev_ctx->fh_init.io_cfg.id);
1353 #endif
1354
1355     /* Process eCPRI cmn header. */
1356  //   (void *)rte_pktmbuf_adj(pkt, sizeof(*ecpricmn));
1357     ecpri_delmeas_pkt = (struct xran_ecpri_del_meas_pkt *)rte_pktmbuf_mtod(pkt,  struct xran_ecpri_del_meas_pkt *);
1358     // The processing of the delay measurement here corresponds to eCPRI sections 3.2.4.6.2 and 3.42.6.3
1359
1360     switch(ecpri_delmeas_pkt->deMeasPl.ActionType) {
1361         case ECPRI_REQUEST:
1362 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1363             printf("Proc rx  Dly Meas Req\n");
1364 #endif
1365             ret_value = xran_process_delmeas_request(pkt, p_xran_dev_ctx, ecpri_delmeas_pkt, port_id);
1366             break;
1367         case ECPRI_REQUEST_W_FUP:
1368 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1369             printf("Proc Dly Meas rx Req w Fup\n");
1370 #endif
1371             ret_value = xran_process_delmeas_request_w_fup(pkt, p_xran_dev_ctx, ecpri_delmeas_pkt, port_id);
1372             break;
1373         case ECPRI_RESPONSE:
1374 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1375             printf("Proc Dly Meas rx Resp\n");
1376 #endif
1377             ret_value = xran_process_delmeas_response(pkt, p_xran_dev_ctx, ecpri_delmeas_pkt, port_id);
1378             break;
1379         case ECPRI_REMOTE_REQ:
1380 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1381             printf("Proc Dly Meas rx Rem Req\n");
1382 #endif
1383             ret_value = xran_process_delmeas_rem_request(pkt, p_xran_dev_ctx, ecpri_delmeas_pkt, port_id);
1384            break;
1385         case ECPRI_REMOTE_REQ_W_FUP:
1386 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1387             printf("Proc Dly Meas Rem rx Req w Fup\n");
1388 #endif
1389             ret_value = xran_process_delmeas_rem_request_w_fup(pkt, p_xran_dev_ctx, ecpri_delmeas_pkt, port_id);
1390            break;
1391         case ECPRI_FOLLOW_UP:
1392 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1393             printf("Proc Dly Meas rx Fup\n");
1394 #endif
1395             ret_value = xran_process_delmeas_follow_up(pkt, p_xran_dev_ctx, ecpri_delmeas_pkt, port_id);
1396            break;
1397         default:
1398 #ifdef ORAN_OWD_DEBUG_MSG_FLOW
1399             printf("Proc Dly Meas default\n");
1400 #endif
1401            break;
1402     }
1403     return ret_value;
1404
1405 }