rlc_am_retransmit.c 54.2 KB
Newer Older
1 2
/*******************************************************************************
Eurecom OpenAirInterface 2
gauthier's avatar
gauthier committed
3
Copyright(c) 1999 - 2014 Eurecom
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.

This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

The full GNU General Public License is included in this distribution in
the file called "COPYING".

Contact Information
Openair Admin: openair_admin@eurecom.fr
Openair Tech : openair_tech@eurecom.fr
Forums       : http://forums.eurecom.fsr/openairinterface
gauthier's avatar
gauthier committed
25 26 27 28 29 30
Address      : EURECOM,
               Campus SophiaTech,
               450 Route des Chappes,
               CS 50193
               06904 Biot Sophia Antipolis cedex,
               FRANCE
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
*******************************************************************************/
#define RLC_AM_MODULE
#define RLC_AM_RETRANSMIT_C
//-----------------------------------------------------------------------------
//#include "rtos_header.h"
//-----------------------------------------------------------------------------
#include "rlc_am.h"
#include "rlc.h"
#include "LAYER2/MAC/extern.h"
#include "UTIL/LOG/log.h"
//#define TRACE_RLC_AM_RESEGMENT
//#define TRACE_RLC_AM_FORCE_TRAFFIC
//#define TRACE_RLC_AM_NACK
//#define TRACE_RLC_AM_ACK
//-----------------------------------------------------------------------------
46
void rlc_am_nack_pdu (rlc_am_entity_t *rlc_pP, frame_t frameP, rlc_sn_t snP, sdu_size_t so_startP, sdu_size_t so_endP)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
//-----------------------------------------------------------------------------
{
    // 5.2.1 Retransmission
    // ...
    // When an AMD PDU or a portion of an AMD PDU is considered for retransmission, the transmitting side of the AM
    // RLC entity shall:
    //     - if the AMD PDU is considered for retransmission for the first time:
    //         - set the RETX_COUNT associated with the AMD PDU to zero;
    //     - else, if it (the AMD PDU or the portion of the AMD PDU that is considered for retransmission) is not pending
    //            for retransmission already, or a portion of it is not pending for retransmission already:
    //         - increment the RETX_COUNT;
    //     - if RETX_COUNT = maxRetxThreshold:
    //         - indicate to upper layers that max retransmission has been reached.


62
    mem_block_t* mb_pP         = rlc_pP->pdu_retrans_buffer[snP].mem_block;
63 64 65
    int          pdu_sdu_index;
    int          sdu_index;

66 67
    if (mb_pP != NULL) {
        rlc_pP->num_nack_sn += 1;
68 69 70 71 72 73 74
        assert(so_startP <= so_endP);
        //-----------------------------------------
        // allow holes in reports
        // it is assumed that hole reports are done in byte offset
        // increasing order among calls refering to only one status PDU
        //  and among time
        //-----------------------------------------
75 76 77
        if (rlc_pP->pdu_retrans_buffer[snP].last_nack_time != frameP) {
            rlc_pP->pdu_retrans_buffer[snP].last_nack_time = frameP;
            rlc_am_clear_holes(rlc_pP, snP);
78 79
        }
        if (!((so_startP == 0) && (so_endP == 0x7FFF))) {
80
            rlc_pP->num_nack_so += 1;
81
        }
82
        rlc_am_add_hole(rlc_pP, frameP, snP, so_startP, so_endP);
83

84 85 86 87
        if (rlc_pP->first_retrans_pdu_sn < 0) {
            rlc_pP->first_retrans_pdu_sn = snP;
        } else if (rlc_am_tx_sn1_gt_sn2(rlc_pP, rlc_pP->first_retrans_pdu_sn, snP)){
            rlc_pP->first_retrans_pdu_sn = snP;
88
        }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][NACK-PDU] NACK PDU SN %04d previous retx_count %d  1ST_RETRANS_PDU %04d\n",
                frameP,
                (rlc_pP->is_enb) ? "eNB" : "UE",
                rlc_pP->enb_module_id,
                rlc_pP->ue_module_id,
                rlc_pP->rb_id,
                snP,
                rlc_pP->pdu_retrans_buffer[snP].retx_count,
                rlc_pP->first_retrans_pdu_sn);

        rlc_pP->pdu_retrans_buffer[snP].flags.retransmit = 1;

        if (rlc_pP->pdu_retrans_buffer[snP].retx_count == -1) {
            rlc_pP->pdu_retrans_buffer[snP].retx_count = 0;
            rlc_pP->retrans_num_bytes_to_retransmit += rlc_pP->pdu_retrans_buffer[snP].header_and_payload_size;
104
        } else {
105
            rlc_pP->pdu_retrans_buffer[snP].retx_count += 1;
106
        }
107 108 109
        if (rlc_pP->pdu_retrans_buffer[snP].retx_count >= rlc_pP->max_retx_threshold) {
            for (pdu_sdu_index = 0; pdu_sdu_index < rlc_pP->pdu_retrans_buffer[snP].nb_sdus; pdu_sdu_index++) {
                sdu_index = rlc_pP->pdu_retrans_buffer[snP].sdus_index[pdu_sdu_index];
110 111
                assert(pdu_sdu_index < RLC_AM_MAX_SDU_IN_PDU);
                assert(sdu_index < RLC_AM_SDU_CONTROL_BUFFER_SIZE);
112 113
                rlc_pP->input_sdus[sdu_index].nb_pdus_ack += 1;
                if (rlc_pP->input_sdus[sdu_index].nb_pdus_ack == rlc_pP->input_sdus[sdu_index].nb_pdus) {
114
                    #ifdef TEST_RLC_AM
115
                    rlc_am_v9_3_0_test_data_conf (rlc_pP->module_id, rlc_pP->rb_id, rlc_pP->input_sdus[sdu_index].mui, RLC_SDU_CONFIRM_NO);
116
                    #else
117
                    rlc_data_conf(rlc_pP->enb_module_id, rlc_pP->ue_module_id, frameP, rlc_pP->is_enb, rlc_pP->rb_id, rlc_pP->input_sdus[sdu_index].mui, RLC_SDU_CONFIRM_NO, rlc_pP->is_data_plane);
118
                    #endif
119 120 121
                    rlc_pP->stat_tx_pdcp_sdu_discarded   += 1;
                    rlc_pP->stat_tx_pdcp_bytes_discarded += rlc_pP->input_sdus[sdu_index].sdu_size;
                    rlc_am_free_in_sdu(rlc_pP, frameP, sdu_index);
122 123 124 125
                }
            }
        }
    } else {
126 127 128 129 130 131 132
        LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][NACK-PDU] ERROR NACK MISSING PDU SN %05d\n",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              snP);
133 134 135 136
        //assert(2==3);
    }
}
//-----------------------------------------------------------------------------
137
void rlc_am_ack_pdu (rlc_am_entity_t *rlc_pP, frame_t frameP, rlc_sn_t snP)
138 139
//-----------------------------------------------------------------------------
{
140
    mem_block_t* mb_pP         = rlc_pP->pdu_retrans_buffer[snP].mem_block;
141 142 143
    int          pdu_sdu_index;
    int          sdu_index;

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    rlc_pP->pdu_retrans_buffer[snP].flags.retransmit = 0;

    if ((rlc_pP->pdu_retrans_buffer[snP].flags.ack == 0) && (mb_pP != NULL)) {
    //if (mb_pP != NULL) {
        free_mem_block(mb_pP);
        rlc_pP->pdu_retrans_buffer[snP].mem_block = NULL;
        LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][ACK-PDU] ACK PDU SN %05d previous retx_count %d \n",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              snP,
              rlc_pP->pdu_retrans_buffer[snP].retx_count);
        rlc_pP->retrans_num_pdus  -= 1;
        rlc_pP->retrans_num_bytes -= rlc_pP->pdu_retrans_buffer[snP].header_and_payload_size;

        if (rlc_pP->pdu_retrans_buffer[snP].retx_count >= 0) {
            rlc_pP->retrans_num_bytes_to_retransmit -= rlc_pP->pdu_retrans_buffer[snP].header_and_payload_size;
163 164
        }

165 166
        for (pdu_sdu_index = 0; pdu_sdu_index < rlc_pP->pdu_retrans_buffer[snP].nb_sdus; pdu_sdu_index++) {
            sdu_index = rlc_pP->pdu_retrans_buffer[snP].sdus_index[pdu_sdu_index];
167 168
            assert(sdu_index >= 0);
            assert(sdu_index < RLC_AM_SDU_CONTROL_BUFFER_SIZE);
169 170 171
            rlc_pP->input_sdus[sdu_index].nb_pdus_ack += 1;
            if ((rlc_pP->input_sdus[sdu_index].nb_pdus_ack == rlc_pP->input_sdus[sdu_index].nb_pdus) &&
                (rlc_pP->input_sdus[sdu_index].sdu_remaining_size == 0)) {
172
#ifdef TEST_RLC_AM
173
                rlc_am_v9_3_0_test_data_conf (rlc_pP->module_id, rlc_pP->rb_id, rlc_pP->input_sdus[sdu_index].mui, RLC_SDU_CONFIRM_YES);
174
#else
175
                rlc_data_conf(rlc_pP->enb_module_id, rlc_pP->ue_module_id, frameP, rlc_pP->is_enb, rlc_pP->rb_id, rlc_pP->input_sdus[sdu_index].mui, RLC_SDU_CONFIRM_YES, rlc_pP->is_data_plane);
176
#endif
177
                rlc_am_free_in_sdu(rlc_pP, frameP, sdu_index);
178 179 180 181 182 183 184
            }
        }
        // 7.1...
        // VT(A) – Acknowledgement state variable
        // This state variable holds the value of the SN of the next AMD PDU for which a positive acknowledgment is to be
        // received in-sequence, and it serves as the lower edge of the transmitting window. It is initially set to 0, and is updated
        // whenever the AM RLC entity receives a positive acknowledgment for an AMD PDU with SN = VT(A).
185 186 187
        rlc_pP->pdu_retrans_buffer[snP].flags.ack = 1;
        if (snP == rlc_pP->vt_a) {
            //rlc_pP->pdu_retrans_buffer[snP].flags.ack = 1;
188
            do {
189 190 191
                memset(&rlc_pP->pdu_retrans_buffer[rlc_pP->vt_a], 0, sizeof(rlc_am_tx_data_pdu_management_t));
                if (rlc_pP->vt_a == rlc_pP->first_retrans_pdu_sn) {
                    rlc_pP->first_retrans_pdu_sn = (rlc_pP->vt_a  + 1) & RLC_AM_SN_MASK;
192
                }
193 194 195 196 197 198 199 200 201 202 203 204 205 206
                rlc_pP->vt_a = (rlc_pP->vt_a  + 1) & RLC_AM_SN_MASK;
            } while ((rlc_pP->pdu_retrans_buffer[rlc_pP->vt_a].flags.ack == 1) && (rlc_pP->vt_a != rlc_pP->vt_s));


            rlc_pP->vt_ms   = (rlc_pP->vt_a + RLC_AM_WINDOW_SIZE) & RLC_AM_SN_MASK;
            LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][ACK-PDU] UPDATED VT(A) %04d VT(MS) %04d  VT(S) %04d\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id,
                  rlc_pP->vt_a,
                  rlc_pP->vt_ms,
                  rlc_pP->vt_s);
207
        }
208
        if (snP == rlc_pP->first_retrans_pdu_sn) {
209
            do {
210 211 212 213 214 215 216 217 218
                rlc_pP->first_retrans_pdu_sn = (rlc_pP->first_retrans_pdu_sn  + 1) & RLC_AM_SN_MASK;
                if (rlc_pP->pdu_retrans_buffer[rlc_pP->first_retrans_pdu_sn].retx_count >= 0) {
                    LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][ACK-PDU] UPDATED  first_retrans_pdu_sn -> %04d\n",
                          frameP,
                          (rlc_pP->is_enb) ? "eNB" : "UE",
                          rlc_pP->enb_module_id,
                          rlc_pP->ue_module_id,
                          rlc_pP->rb_id,
                          rlc_pP->first_retrans_pdu_sn);
219 220
                    break;
                }
221 222 223 224 225 226 227 228 229 230
            } while (rlc_pP->first_retrans_pdu_sn != rlc_pP->vt_s);
            if (rlc_pP->vt_s == rlc_pP->first_retrans_pdu_sn) {
                rlc_pP->first_retrans_pdu_sn = -1;
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][ACK-PDU] UPDATED  first_retrans_pdu_sn -> %04d\n",
                      frameP,
                      (rlc_pP->is_enb) ? "eNB" : "UE",
                      rlc_pP->enb_module_id,
                      rlc_pP->ue_module_id,
                      rlc_pP->rb_id,
                      rlc_pP->first_retrans_pdu_sn);
231 232 233
            }
        }
    } else {
234 235 236 237 238 239 240 241 242 243
        LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][ACK-PDU] WARNING ACK PDU SN %05d -> NO PDU TO ACK\n",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              snP);
        if (mb_pP != NULL) {
            free_mem_block(mb_pP);
            rlc_pP->pdu_retrans_buffer[snP].mem_block = NULL;
244
        }
245 246 247
        if (rlc_pP->pdu_retrans_buffer[snP].flags.ack > 0) {
            if (snP == rlc_pP->vt_a) {
                //rlc_pP->pdu_retrans_buffer[snP].flags.ack = 1;
248
                do {
249 250 251
                    memset(&rlc_pP->pdu_retrans_buffer[rlc_pP->vt_a], 0, sizeof(rlc_am_tx_data_pdu_management_t));
                    if (rlc_pP->vt_a == rlc_pP->first_retrans_pdu_sn) {
                        rlc_pP->first_retrans_pdu_sn = (rlc_pP->vt_a  + 1) & RLC_AM_SN_MASK;
252
                    }
253 254 255 256 257 258 259 260 261 262 263 264 265
                    rlc_pP->vt_a = (rlc_pP->vt_a  + 1) & RLC_AM_SN_MASK;
                } while ((rlc_pP->pdu_retrans_buffer[rlc_pP->vt_a].flags.ack == 1) && (rlc_pP->vt_a != rlc_pP->vt_s));

                rlc_pP->vt_ms   = (rlc_pP->vt_a + RLC_AM_WINDOW_SIZE) & RLC_AM_SN_MASK;
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][ACK-PDU] UPDATED VT(A) %04d VT(MS) %04d  VT(S) %04d\n",
                    frameP,
                    (rlc_pP->is_enb) ? "eNB" : "UE",
                    rlc_pP->enb_module_id,
                    rlc_pP->ue_module_id,
                    rlc_pP->rb_id,
                    rlc_pP->vt_a,
                    rlc_pP->vt_ms,
                    rlc_pP->vt_s);
266 267 268 269 270
            }
        }
    }
}
//-----------------------------------------------------------------------------
271
mem_block_t* rlc_am_retransmit_get_copy (rlc_am_entity_t *rlc_pP, frame_t frameP, rlc_sn_t snP)
272 273
//-----------------------------------------------------------------------------
{
274 275
    mem_block_t* mb_original_p = rlc_pP->pdu_retrans_buffer[snP].mem_block;
    if (mb_original_p != NULL) {
276

277
        rlc_am_tx_data_pdu_management_t *pdu_mngt = &rlc_pP->pdu_retrans_buffer[snP % RLC_AM_PDU_RETRANSMISSION_BUFFER_SIZE];
278 279 280

        int size             = pdu_mngt->header_and_payload_size + sizeof(struct mac_tb_req);
        mem_block_t* mb_copy = get_free_mem_block(size);
281
        memcpy(mb_copy->data, mb_original_p->data, size);
282

283
        rlc_am_pdu_sn_10_t *pdu_p                         = (rlc_am_pdu_sn_10_t*) (&mb_copy->data[sizeof(struct mac_tb_req)]);
284
        ((struct mac_tb_req*)(mb_copy->data))->data_ptr = (uint8_t*)pdu_p;
285 286 287

        pdu_mngt->flags.retransmit = 0;

288
        rlc_am_pdu_polling(rlc_pP, frameP, pdu_p, pdu_mngt->payload_size);
289 290 291 292 293 294
        return mb_copy;
    } else {
        return NULL;
    }
}
//-----------------------------------------------------------------------------
295
mem_block_t* rlc_am_retransmit_get_subsegment(rlc_am_entity_t *rlc_pP, frame_t frameP, rlc_sn_t snP, sdu_size_t *sizeP)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
//-----------------------------------------------------------------------------
{

  // 5.2 ARQ procedures
  // ARQ procedures are only performed by an AM RLC entity.
  // 5.2.1 Retransmission
  // The transmitting side of an AM RLC entity can receive a negative acknowledgement (notification of reception failure
  // by its peer AM RLC entity) for an AMD PDU or a portion of an AMD PDU by the following:
  // - STATUS PDU from its peer AM RLC entity.
  //
  // When receiving a negative acknowledgement for an AMD PDU or a portion of an AMD PDU by a STATUS PDU from
  // its peer AM RLC entity, the transmitting side of the AM RLC entity shall:
  //     - if the SN of the corresponding AMD PDU falls within the range VT(A) <= SN < VT(S):
  //         - consider the AMD PDU or the portion of the AMD PDU for which a negative acknowledgement was
  //           received for retransmission.
  //
  // When an AMD PDU or a portion of an AMD PDU is considered for retransmission, the transmitting side of the AM
  // RLC entity shall:
  //     - if the AMD PDU is considered for retransmission for the first time:
  //         - set the RETX_COUNT associated with the AMD PDU to zero;
  //     - else, if it (the AMD PDU or the portion of the AMD PDU that is considered for retransmission) is not pending
  //       for retransmission already, or a portion of it is not pending for retransmission already:
  //         - increment the RETX_COUNT;
  //     - if RETX_COUNT = maxRetxThreshold:
  //         - indicate to upper layers that max retransmission has been reached.
  //
  // When retransmitting an AMD PDU, the transmitting side of an AM RLC entity shall:
  //     - if the AMD PDU can entirely fit within the total size of RLC PDU(s) indicated by lower layer at the particular
  //       transmission opportunity:
  //         - deliver the AMD PDU as it is except for the P field (the P field should be set according to sub clause 5.2.2) to
  //           lower layer;
  //     - otherwise:
  //         - segment the AMD PDU, form a new AMD PDU segment which will fit within the total size of RLC PDU(s)
  //           indicated by lower layer at the particular transmission opportunity and deliver the new AMD PDU segment
  //           to lower layer.
  //
  // When retransmitting a portion of an AMD PDU, the transmitting side of an AM RLC entity shall:
  //    - segment the portion of the AMD PDU as necessary, form a new AMD PDU segment which will fit within the
  //    total size of RLC PDU(s) indicated by lower layer at the particular transmission opportunity and deliver the new
  //    AMD PDU segment to lower layer.
  //
  // When forming a new AMD PDU segment, the transmitting side of an AM RLC entity shall:
  //    - only map the Data field of the original AMD PDU to the Data field of the new AMD PDU segment;
  //    - set the header of the new AMD PDU segment in accordance with the description in sub clause 6.;
  //    - set the P field according to sub clause 5.2.2.

342
    mem_block_t*           mb_original_p     = rlc_pP->pdu_retrans_buffer[snP].mem_block;
343

344 345 346 347
    if (mb_original_p != NULL) {
        mem_block_t*           mb_sub_segment_p  = get_free_mem_block(*sizeP + sizeof(struct mac_tb_req));
        rlc_am_pdu_sn_10_t*    pdu_original_p    = (rlc_am_pdu_sn_10_t*) (&mb_original_p->data[sizeof(struct mac_tb_req)]);
        rlc_am_pdu_sn_10_t*    pdu_sub_segment_p = (rlc_am_pdu_sn_10_t*) (&mb_sub_segment_p->data[sizeof(struct mac_tb_req)]);
348 349 350 351 352
        rlc_am_pdu_info_t      pdu_info;
        int                    max_copy_payload_size;
        //LG avoid WARNING int                    test_max_copy_payload_size;
        int                    test_pdu_copy_size          = 0;

353
        ((struct mac_tb_req*)(mb_sub_segment_p->data))->data_ptr         = (uint8_t*)&(mb_sub_segment_p->data[sizeof(struct mac_tb_req)]);
354

355
        if (rlc_am_get_data_pdu_infos(frameP,pdu_original_p, rlc_pP->pdu_retrans_buffer[snP].header_and_payload_size, &pdu_info) >= 0) {
356
            int li_index = 0;
357 358 359 360 361 362 363 364 365 366 367
            int start_offset       = rlc_pP->pdu_retrans_buffer[snP].nack_so_start;
            int stop_offset        = rlc_pP->pdu_retrans_buffer[snP].nack_so_stop;

            LOG_D(RLC, "\n[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] ORIGINAL PDU SN %04d:\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id,
                  snP);
            rlc_am_display_data_pdu_infos(rlc_pP, frameP,&pdu_info);
368 369 370 371

            // all 15 bits set to 1 (indicate that the missing portion of the AMD PDU includes all bytes
            // to the last byte of the AMD PDU)
            if (stop_offset == 0x7FFF) {
372 373 374 375 376 377 378 379 380
                rlc_pP->pdu_retrans_buffer[snP].nack_so_stop = rlc_pP->pdu_retrans_buffer[snP].payload_size - 1;
                stop_offset = rlc_pP->pdu_retrans_buffer[snP].nack_so_stop;
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] UPDATED RETRANS PDU SN %04d nack_so_stop FROM 0x7FFF to %05d\n",
                      frameP,
                      (rlc_pP->is_enb) ? "eNB" : "UE",
                      rlc_pP->enb_module_id,
                      rlc_pP->ue_module_id,
                      rlc_pP->rb_id,
                      snP, stop_offset);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
            }

            // FIXED PART AMD PDU SEGMENT HEADER
            // 6.2.1.5 AMD PDU segment
            // AMD PDU segment consists of a Data field and an AMD PDU segment header.
            //
            // AMD PDU segment header consists of a fixed part (fields that are present for every AMD PDU segment) and an
            // extension part (fields that are present for an AMD PDU segment when necessary). The fixed part of the AMD PDU
            // segment header itself is byte aligned and consists of a D/C, a RF, a P, a FI, an E, a SN, a LSF and a SO. The extension
            // part of the AMD PDU segment header itself is byte aligned and consists of E(s) and LI(s).
            //
            // An AMD PDU segment header consists of an extension part only when more than one Data field elements are present
            // in the AMD PDU segment, in which case an E and a LI are present for every Data field element except the last.
            // Furthermore, when an AMD PDU segment header consists of an odd number of LI(s), four padding bits follow after the
            // last LI.

397 398
            pdu_sub_segment_p->b1 = (pdu_original_p->b1 & 0x83) | 0x40;
            pdu_sub_segment_p->b2 = pdu_original_p->b2;
399 400
            pdu_sub_segment_p->data[0] = ((uint8_t)(start_offset >> 8));
            pdu_sub_segment_p->data[1] = ((uint8_t)(start_offset & 0xFF));
401 402 403 404 405 406 407 408 409 410 411 412

            *sizeP = *sizeP - 4;


            int            continue_fill_pdu_with_pdu  = 1;
            int            test_pdu_remaining_size     = *sizeP;
            int            test_num_li                 = 0;
            int            fill_num_li                    ;
            int            test_li_sum                 = 0;
            int            not_test_fi                 = 0; // by default not 1st byte and not last byte af a SDU
            int            test_start_offset;
            unsigned int   test_li_length_in_bytes     = 1;
413 414
            int16_t          test_li_list[RLC_AM_MAX_SDU_IN_PDU];
            uint8_t*          fill_payload_p;
415 416 417
            //int            test_fi_last_byte_pdu_is_last_byte_sdu = 0;
            //int            test_fi_first_byte_pdu_is_first_byte_sdu = 0;

418
            rlc_am_e_li_t* e_li_sub_segment            = (rlc_am_e_li_t*)(&pdu_sub_segment_p->data[2]);
419
            //int            first_enter_in_start_offset_lt_li_sum = 1;
420
            int            not_fi_original                 = ((pdu_original_p->b1 & 0x18) >> 3) ^ 3;
421 422 423 424

            //-------------------------------------------------------
            // set MAX payload size that can be copied
            // first constraint : the size of the hole to retransmit
425
            rlc_am_get_next_hole(rlc_pP, frameP, snP, &start_offset, &stop_offset);
426 427 428
            max_copy_payload_size = stop_offset - start_offset + 1;
            assert(max_copy_payload_size > 0);
            assert(test_pdu_remaining_size > 0);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] HOLE FOUND SO %d -> %d\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id,
                  start_offset,
                  stop_offset);
            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] ORIGINAL FI %d\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id,
                  (pdu_original_p->b1 & 0x18) >> 3);
            // second constraint the size of the pdu_p requested by MAC layer
445
            if (max_copy_payload_size > test_pdu_remaining_size) {
446 447 448 449 450 451 452 453
                  LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] CUT max_copy_payload_size with test_pdu_remaining_size %d -> %d\n",
                      frameP,
                      (rlc_pP->is_enb) ? "eNB" : "UE",
                      rlc_pP->enb_module_id,
                      rlc_pP->ue_module_id,
                      rlc_pP->rb_id,
                      max_copy_payload_size,
                      test_pdu_remaining_size);
454 455 456 457 458 459 460 461 462 463 464
                  max_copy_payload_size = test_pdu_remaining_size;
            }


            if (start_offset == 0) {
                not_test_fi = (not_fi_original & 0x02);
            }
            test_start_offset = start_offset;

            //.find the li corresponding to the nack_so_start (start_offset)
            if (pdu_info.num_li > 0) {
465 466 467 468 469 470 471
                LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] ORIGINAL NUM LI %d\n",
                    frameP,
                    (rlc_pP->is_enb) ? "eNB" : "UE",
                    rlc_pP->enb_module_id,
                    rlc_pP->ue_module_id,
                    rlc_pP->rb_id,
                    pdu_info.num_li);
472
                while ((li_index < pdu_info.num_li) && (continue_fill_pdu_with_pdu)) {
473 474 475 476 477 478 479
                    LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] FIND LI %d\n",
                        frameP,
                        (rlc_pP->is_enb) ? "eNB" : "UE",
                        rlc_pP->enb_module_id,
                        rlc_pP->ue_module_id,
                        rlc_pP->rb_id,
                        pdu_info.li_list[li_index]);
480
                    if (max_copy_payload_size > test_pdu_remaining_size) {
481 482 483 484 485 486 487 488
                        LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] CUT max_copy_payload_size with test_pdu_remaining_size %d -> %d\n",
                            frameP,
                            (rlc_pP->is_enb) ? "eNB" : "UE",
                            rlc_pP->enb_module_id,
                            rlc_pP->ue_module_id,
                            rlc_pP->rb_id,
                            max_copy_payload_size,
                            test_pdu_remaining_size);
489 490 491 492 493 494 495 496 497 498
                          max_copy_payload_size = test_pdu_remaining_size;
                    }
                    assert(max_copy_payload_size >= 0);
                    assert(test_pdu_remaining_size >= 0);

                    test_li_sum               += pdu_info.li_list[li_index];

                    //---------------------------------------------------------------
                    if (test_start_offset < test_li_sum) {

499 500 501 502 503 504 505 506
                        LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] test_start_offset < test_li_sum  %d < %d\n",
                            frameP,
                            (rlc_pP->is_enb) ? "eNB" : "UE",
                            rlc_pP->enb_module_id,
                            rlc_pP->ue_module_id,
                            rlc_pP->rb_id,
                            test_start_offset,
                            test_li_sum);
507 508
                        /*if (test_max_copy_payload_size > (test_li_sum - test_start_offset)) {
                            #ifdef TRACE_RLC_AM_RESEGMENT
509
                            LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] CUT test_max_copy_payload_size with test_li_sum - test_start_offset %d -> %d\n",frameP, rlc_pP->module_id, rlc_pP->rb_id,  test_max_copy_payload_size, test_li_sum - test_start_offset);
510 511 512 513 514
                            #endif
                            test_max_copy_payload_size = test_li_sum - test_start_offset;
                        }*/

                        if ((max_copy_payload_size + test_start_offset) < test_li_sum) {
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] (max_copy_payload_size %d + test_start_offset %d) < test_li_sum %d\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                max_copy_payload_size,
                                test_start_offset,
                                test_li_sum);
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                test_start_offset ,
                                test_start_offset + max_copy_payload_size - 1,
                                max_copy_payload_size );
533 534 535 536 537 538 539 540
                            assert(max_copy_payload_size > 0);
                            continue_fill_pdu_with_pdu = 0;
                            test_pdu_copy_size         = test_pdu_copy_size + max_copy_payload_size;
                            test_start_offset          = test_start_offset + max_copy_payload_size;
                            not_test_fi                = not_test_fi & 0x02;  // clear b0, last byte does not correspond to last byte of a SDU
                            max_copy_payload_size      = 0;

                        } else if ((max_copy_payload_size + test_start_offset) == test_li_sum) {
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] (max_copy_payload_size + test_start_offset) == test_li_sum %d == %d\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                (max_copy_payload_size + test_start_offset) ,
                                test_li_sum);
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                test_start_offset ,
                                test_start_offset + max_copy_payload_size - 1,
                                max_copy_payload_size );
558 559 560 561 562 563 564 565
                            assert(max_copy_payload_size > 0);
                            continue_fill_pdu_with_pdu = 0;
                            test_pdu_copy_size         = test_pdu_copy_size + max_copy_payload_size;
                            test_start_offset          = test_start_offset + max_copy_payload_size;
                            not_test_fi                = not_test_fi | 0x01;// set b0, last byte does correspond to last byte of a SDU
                            max_copy_payload_size      = 0;

                        } else if ((max_copy_payload_size + test_start_offset - (test_li_length_in_bytes ^ 3)) > test_li_sum) {
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] (max_copy_payload_size + test_start_offset - (test_li_length_in_bytes ^ 3)) > test_li_sum %d > %d\n SET LI %d\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                (max_copy_payload_size + test_start_offset)  + (test_li_length_in_bytes ^ 3),
                                test_li_sum,
                                test_li_sum - test_start_offset);
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                test_start_offset ,
                                test_li_sum - 1,
                                test_li_sum - test_start_offset );
584 585 586 587 588 589 590 591 592
                            assert((test_li_sum - test_start_offset) > 0);
                            test_li_list[test_num_li++] = test_li_sum - test_start_offset;
                            test_pdu_copy_size          = test_pdu_copy_size + test_li_sum - test_start_offset;
                            test_li_length_in_bytes      = test_li_length_in_bytes ^ 3;

                            test_pdu_remaining_size     = test_pdu_remaining_size - test_li_sum + test_start_offset -
                                                          test_li_length_in_bytes;

                            max_copy_payload_size       = max_copy_payload_size - test_li_sum + test_start_offset - test_li_length_in_bytes;
593 594 595 596 597 598 599 600
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] NOW max_copy_payload_size %d BYTES test_start_offset %d\n",
                                  frameP,
                                  (rlc_pP->is_enb) ? "eNB" : "UE",
                                  rlc_pP->enb_module_id,
                                  rlc_pP->ue_module_id,
                                  rlc_pP->rb_id,
                                  max_copy_payload_size,
                                  test_li_sum);
601 602 603 604 605
                            // normally the next while itereation will add bytes to PDU
                            //not_test_fi = not_test_fi | 0x01;  // set b0, last byte does correspond to last byte of a SDU
                            test_start_offset           = test_li_sum;

                        } else {
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] (test_max_copy_payload_size + test_start_offset ) > test_li_sum %d > %d\n NO REMAINING SIZE FOR LI",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                (max_copy_payload_size + test_start_offset),
                                test_li_sum);
                            LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                                frameP,
                                (rlc_pP->is_enb) ? "eNB" : "UE",
                                rlc_pP->enb_module_id,
                                rlc_pP->ue_module_id,
                                rlc_pP->rb_id,
                                test_start_offset ,
                                test_li_sum - 1,
                                test_li_sum - test_start_offset );
623 624 625 626 627 628 629 630 631 632 633 634 635 636
                            assert((test_li_sum - test_start_offset) > 0);
                            continue_fill_pdu_with_pdu = 0;
                            test_pdu_copy_size         = test_pdu_copy_size + test_li_sum - test_start_offset;

                            test_pdu_remaining_size    = test_pdu_remaining_size - test_li_sum + test_start_offset;

                            max_copy_payload_size      = max_copy_payload_size - test_li_sum + test_start_offset;
                            test_start_offset          = test_li_sum;
                            not_test_fi                = not_test_fi | 0x01;  // set b0, last byte does correspond to last byte of a SDU
                        }
                    //---------------------------------------------------------------
                    // start offset start at the begining of a SDU
                    // and it cant be the first data field of the original PDU
                    } else if (test_start_offset == test_li_sum) {
637 638 639 640 641 642 643 644
                        LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] (test_start_offset == test_li_sum) %d == %d\n",
                            frameP,
                            (rlc_pP->is_enb) ? "eNB" : "UE",
                            rlc_pP->enb_module_id,
                            rlc_pP->ue_module_id,
                            rlc_pP->rb_id,
                            test_start_offset ,
                            test_li_sum);
645 646 647 648 649 650 651 652 653 654 655 656 657
                        if ((test_num_li == 0) && (test_pdu_copy_size == 0)) {
                            not_test_fi = not_test_fi | 0x02;  // set b1, first byte does correspond to first byte of a SDU
                        }
                    }
                    li_index = li_index + 1;
                }
                if ((continue_fill_pdu_with_pdu > 0) &&
                    (li_index    == pdu_info.num_li) &&
                    (pdu_info.hidden_size       > 0) &&
                    (test_pdu_remaining_size    > 0) &&
                    (max_copy_payload_size      > 0) ){

                    if (max_copy_payload_size > test_pdu_remaining_size) {
658 659 660 661 662 663 664 665
                        LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] TRYING HIDDEN SIZE...CUT max_copy_payload_size with test_pdu_remaining_size %d -> %d\n",
                            frameP,
                            (rlc_pP->is_enb) ? "eNB" : "UE",
                            rlc_pP->enb_module_id,
                            rlc_pP->ue_module_id,
                            rlc_pP->rb_id,
                            max_copy_payload_size,
                            test_pdu_remaining_size);
666 667 668 669 670
                          max_copy_payload_size = test_pdu_remaining_size;
                    }
                    // remaining bytes to fill, redundant check, but ...
                    if  ((max_copy_payload_size + test_start_offset) >= (pdu_info.hidden_size + test_li_sum)) {
                        test_pdu_copy_size += (pdu_info.hidden_size  + test_li_sum - test_start_offset);
671 672 673 674 675 676 677 678 679 680 681 682
                        LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COPYING WHOLE REMAINING SIZE %d (max_copy_payload_size %d, test_start_offset %d, pdu_info.hidden_size %d test_li_sum %d test_pdu_copy_size %d)\n",
                              frameP,
                              (rlc_pP->is_enb) ? "eNB" : "UE",
                              rlc_pP->enb_module_id,
                              rlc_pP->ue_module_id,
                              rlc_pP->rb_id,
                              pdu_info.hidden_size  + test_li_sum - test_start_offset,
                              max_copy_payload_size,
                              test_start_offset,
                              pdu_info.hidden_size,
                              test_li_sum,
                              test_pdu_copy_size);
683 684 685
                        test_start_offset   = pdu_info.hidden_size   + test_li_sum;
                        not_test_fi = (not_test_fi & 0x2) | (not_fi_original & 0x1);  // set b0 idendical to the b0 of the non segmented PDU
                    } else {
686 687 688 689 690 691 692 693
                    	LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COPYING REMAINING SIZE %d (/%d)\n",
                              frameP,
                              (rlc_pP->is_enb) ? "eNB" : "UE",
                              rlc_pP->enb_module_id,
                              rlc_pP->ue_module_id,
                              rlc_pP->rb_id,
                              max_copy_payload_size,
                              pdu_info.hidden_size);
694 695 696 697 698 699
                        test_pdu_copy_size += max_copy_payload_size;
                        test_start_offset = test_start_offset + max_copy_payload_size;
                        not_test_fi = not_test_fi & 0x2;  // clear b0 because no SDU ending in this PDU
                    }
                }
            } else { // num_li == 0
700 701 702 703 704 705
                LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] (num_li == 0)\n",
                      frameP,
                      (rlc_pP->is_enb) ? "eNB" : "UE",
                      rlc_pP->enb_module_id,
                      rlc_pP->ue_module_id,
                      rlc_pP->rb_id);
706
                test_pdu_copy_size = max_copy_payload_size;
707
                if ((stop_offset ==  (start_offset + max_copy_payload_size - 1)) && (stop_offset == rlc_pP->pdu_retrans_buffer[snP].payload_size - 1)) {
708 709 710 711 712 713
                    not_test_fi = (not_test_fi & 0x2) | (not_fi_original & 0x1);  // set b0 idendical to the b0 of the non segmented PDU
                } else {
                    not_test_fi = not_test_fi & 0x2;  // clear b0 because no SDU ending in this PDU
                }
            }
            //---------------------------------------------------------------
714
            /*if (stop_offset == (rlc_pP->pdu_retrans_buffer[snP].payload_size - 1)) {
715 716 717 718 719
                    test_fi = (test_fi & 0x02) | (fi_original & 0x01);
            }*/
            //---------------------------------------------------------------
            // write FI field in header
            //---------------------------------------------------------------
720
            pdu_sub_segment_p->b1 = pdu_sub_segment_p->b1 | (((not_test_fi << 3) ^ 0x18) & 0x18);
721
            //---------------------------------------------------------------
722
            // fill the segment pdu_p with Lis and data
723
            //---------------------------------------------------------------
724 725
            LOG_T(RLC, "[FRAME XXXXX][RLC_AM][MOD XX][RB XX][RE-SEGMENT] fill the segment pdu_p with Lis and data, test_num_li %d\n",
                  test_num_li);
726
            if (test_num_li > 0) {
727
                pdu_sub_segment_p->b1 = pdu_sub_segment_p->b1 | 0x04; // set E bit
728 729 730 731 732 733 734 735 736 737 738 739
                test_li_length_in_bytes = 1;
                for (fill_num_li=0; fill_num_li < test_num_li; fill_num_li++) {
                    test_li_length_in_bytes = test_li_length_in_bytes ^ 3;

                    if (test_li_length_in_bytes  == 2) {
                        if (fill_num_li == (test_num_li - 1)) {
                            e_li_sub_segment->b1 = 0;
                        } else {
                            e_li_sub_segment->b1 =  0x80;
                        }
                        e_li_sub_segment->b1 = e_li_sub_segment->b1 | (test_li_list[fill_num_li] >> 4);
                        e_li_sub_segment->b2 = test_li_list[fill_num_li] << 4;
740
                        fill_payload_p         = (uint8_t*)(&e_li_sub_segment->b3);
741 742 743 744 745 746 747 748
                        *sizeP               = *sizeP - 2;
                    } else {
                        if (fill_num_li != (test_num_li - 1)) {
                            e_li_sub_segment->b2  = e_li_sub_segment->b2 | 0x08;
                        }
                        e_li_sub_segment->b2 = e_li_sub_segment->b2 | (test_li_list[fill_num_li] >> 8);
                        e_li_sub_segment->b3 = test_li_list[fill_num_li] & 0xFF;
                        e_li_sub_segment++;
749
                        fill_payload_p         = (uint8_t*)e_li_sub_segment;
750 751
                        *sizeP               = *sizeP - 1;
                    }
752 753 754 755 756 757 758
                    LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] ADD LI %d\n",
                          frameP,
                          (rlc_pP->is_enb) ? "eNB" : "UE",
                          rlc_pP->enb_module_id,
                          rlc_pP->ue_module_id,
                          rlc_pP->rb_id,
                          test_li_list[fill_num_li]);
759 760
                }
            } else {
761 762 763 764 765 766
                    LOG_T(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] ADD NO LI\n",
                          frameP,
                          (rlc_pP->is_enb) ? "eNB" : "UE",
                          rlc_pP->enb_module_id,
                          rlc_pP->ue_module_id,
                          rlc_pP->rb_id);
767
                fill_payload_p = (uint8_t*)e_li_sub_segment;
768 769 770 771
            }
            //---------------------------------------------------------------
            // copy payload to retransmit
            //---------------------------------------------------------------
772 773
            memcpy(fill_payload_p,
                   &rlc_pP->pdu_retrans_buffer[snP].payload[start_offset],
774 775
                   test_pdu_copy_size);

776
            ((struct mac_tb_req*)(mb_sub_segment_p->data))->tb_size  = (((uint64_t)fill_payload_p)+ test_pdu_copy_size) - ((uint64_t)(&pdu_sub_segment_p->b1));
777 778

            // set LSF
779 780
            if ((test_pdu_copy_size + start_offset) == rlc_pP->pdu_retrans_buffer[snP].payload_size) {
                pdu_sub_segment_p->data[0] = pdu_sub_segment_p->data[0] | 0x80;
781

782
                rlc_pP->pdu_retrans_buffer[snP].flags.retransmit = 0;
783

784 785 786 787 788 789
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] RE-SEND DATA PDU SN %04d SO %d %d BYTES PAYLOAD %d BYTES LSF!\n",
                        frameP,
                        (rlc_pP->is_enb) ? "eNB" : "UE",
                        rlc_pP->enb_module_id,
                        rlc_pP->ue_module_id,
                        rlc_pP->rb_id,
790 791
                        snP,
                        start_offset,
792
                        ((struct mac_tb_req*)(mb_sub_segment_p->data))->tb_size,
793
                        test_pdu_copy_size);
794 795
            }
              else {
796 797 798 799 800 801
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] RE-SEND DATA PDU SN %04d SO %d %d BYTES PAYLOAD %d BYTES\n",
                        frameP,
                        (rlc_pP->is_enb) ? "eNB" : "UE",
                        rlc_pP->enb_module_id,
                        rlc_pP->ue_module_id,
                        rlc_pP->rb_id,
802 803
                        snP,
                        start_offset,
804
                        ((struct mac_tb_req*)(mb_sub_segment_p->data))->tb_size,
805
                        test_pdu_copy_size);
806
            }
807 808 809
            LOG_T(RLC, "[FRAME XXXXX][RLC_AM][MOD XX][RB XX][RE-SEGMENT] *sizeP %d = *sizeP %d - test_pdu_copy_size %d\n",
                    *sizeP - test_pdu_copy_size, *sizeP,
                    test_pdu_copy_size);
810 811 812 813 814

            *sizeP = *sizeP - test_pdu_copy_size;
            //---------------------------------------------------------------
            // update nack_so_start
            //---------------------------------------------------------------
815 816 817 818 819 820 821 822 823 824 825 826 827 828
            rlc_am_remove_hole(rlc_pP, frameP, snP, start_offset, test_pdu_copy_size+start_offset - 1);
            //rlc_pP->pdu_retrans_buffer[snP].nack_so_start = rlc_pP->pdu_retrans_buffer[snP].nack_so_start + test_pdu_copy_size;
            LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] RE-SEND DATA PDU SN %04d NOW nack_so_start %d nack_so_stop %d\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id,
                  snP,
                  rlc_pP->pdu_retrans_buffer[snP].nack_so_start,
                  rlc_pP->pdu_retrans_buffer[snP].nack_so_stop);
            /*if (rlc_pP->pdu_retrans_buffer[snP].nack_so_start == rlc_pP->pdu_retrans_buffer[snP].nack_so_stop) {
                rlc_pP->pdu_retrans_buffer[snP].nack_so_start = 0;
                rlc_pP->pdu_retrans_buffer[snP].nack_so_stop  = 0x7FFF;
829 830
            }*/
        } else {
831 832 833 834 835 836
            LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] COULD NOT GET INFO FOR DATA PDU SN %04d -> RETURN NULL\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id, snP);
837 838
            return NULL;
        }
839
        rlc_am_pdu_polling(rlc_pP, frameP, pdu_sub_segment_p, test_pdu_copy_size);
840

841
        return mb_sub_segment_p;
842
    } else {
843 844 845 846 847 848 849
        LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] RE-SEND DATA PDU SN %04d BUT NO PDU AVAILABLE -> RETURN NULL\n",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              snP);
850 851 852 853 854
        assert(3==4);
        return NULL;
    }
}
//-----------------------------------------------------------------------------
855
void rlc_am_tx_buffer_display (rlc_am_entity_t* rlc_pP, frame_t frameP, char* message_pP)
856 857
//-----------------------------------------------------------------------------
{
858 859 860 861 862 863 864 865 866 867 868 869 870
    rlc_sn_t       sn = rlc_pP->vt_a;
    int            i, loop = 0;

    if (message_pP) {
        LOG_D(RLC, "\n[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u] Retransmission buffer %s VT(A)=%04d VT(S)=%04d:",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              message_pP,
              rlc_pP->vt_a,
              rlc_pP->vt_s);
871
    } else {
872 873 874 875 876 877 878 879
        LOG_D(RLC, "\n[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u] Retransmission buffer VT(A)=%04d VT(S)=%04d:",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              rlc_pP->vt_a,
              rlc_pP->vt_s);
880 881
    }

882 883
    while (rlc_pP->vt_s != sn) {
        if (rlc_pP->pdu_retrans_buffer[sn].mem_block) {
884 885 886
            if ((loop % 1) == 0) {
                LOG_D(RLC, "\nTX SN:\t");
            }
887 888
            if (rlc_pP->pdu_retrans_buffer[sn].flags.retransmit) {
                LOG_D(RLC, "%04d %d/%d Bytes (NACK RTX:%02d ",sn, rlc_pP->pdu_retrans_buffer[sn].header_and_payload_size, rlc_pP->pdu_retrans_buffer[sn].payload_size, rlc_pP->pdu_retrans_buffer[sn].retx_count);
889
            } else {
890
                LOG_D(RLC, "%04d %d/%d Bytes (RTX:%02d ",sn, rlc_pP->pdu_retrans_buffer[sn].header_and_payload_size, rlc_pP->pdu_retrans_buffer[sn].payload_size, rlc_pP->pdu_retrans_buffer[sn].retx_count);
891
            }
892 893
            if (rlc_pP->pdu_retrans_buffer[sn].num_holes == 0) {
               LOG_D(RLC, "SO:%04d->%04d)\t", rlc_pP->pdu_retrans_buffer[sn].nack_so_start, rlc_pP->pdu_retrans_buffer[sn].nack_so_stop);
894
            } else {
895
                for (i=0; i<rlc_pP->pdu_retrans_buffer[sn].num_holes;i++){
896
                    assert(i < RLC_AM_MAX_HOLES_REPORT_PER_PDU);
897
                    LOG_D(RLC, "SO:%04d->%04d)\t", rlc_pP->pdu_retrans_buffer[sn].hole_so_start[i], rlc_pP->pdu_retrans_buffer[sn].hole_so_stop[i]);
898 899 900 901 902 903 904 905 906
                }
            }
            loop++;
        }
        sn = (sn + 1) & RLC_AM_SN_MASK;
    }
   LOG_D(RLC, "\n");
}
//-----------------------------------------------------------------------------
907
void rlc_am_retransmit_any_pdu(rlc_am_entity_t* rlc_pP,frame_t frameP)
908 909
//-----------------------------------------------------------------------------
{
910 911 912 913 914 915 916 917 918 919 920 921 922
    rlc_sn_t             sn           = (rlc_pP->vt_s - 1) & RLC_AM_SN_MASK;
    rlc_sn_t             sn_end       = (rlc_pP->vt_a - 1) & RLC_AM_SN_MASK;
    int                  found_pdu    = 0;
    rlc_sn_t             found_pdu_sn = 0; // avoid warning
    mem_block_t*         pdu_p        = NULL;
    rlc_am_pdu_sn_10_t*  pdu_sn_10_p  = NULL;

    LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][FORCE-TRAFFIC] rlc_am_retransmit_any_pdu()\n",
          frameP,
          (rlc_pP->is_enb) ? "eNB" : "UE",
          rlc_pP->enb_module_id,
          rlc_pP->ue_module_id,
          rlc_pP->rb_id);
923
    while (sn != sn_end) {
924
        if (rlc_pP->pdu_retrans_buffer[sn].mem_block != NULL) {
925 926 927 928
            if (!found_pdu) {
                found_pdu = 1;
                found_pdu_sn = sn;
            }
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
            if (rlc_pP->pdu_retrans_buffer[sn].header_and_payload_size <= rlc_pP->nb_bytes_requested_by_mac) {
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][FORCE-TRAFFIC] RE-SEND DATA PDU SN %04d\n",
                      frameP,
                      (rlc_pP->is_enb) ? "eNB" : "UE",
                      rlc_pP->enb_module_id,
                      rlc_pP->ue_module_id,
                      rlc_pP->rb_id, sn);
                rlc_am_nack_pdu (rlc_pP, frameP, sn, 0, 0x7FFF);
                // no need for update rlc_pP->nb_bytes_requested_by_mac
                pdu_p = rlc_am_retransmit_get_copy(rlc_pP, frameP, sn);
                pdu_sn_10_p = (rlc_am_pdu_sn_10_t*) (&pdu_p->data[sizeof(struct mac_tb_req)]);
                rlc_am_pdu_polling(rlc_pP, frameP, pdu_sn_10_p, rlc_pP->pdu_retrans_buffer[sn].header_and_payload_size);
                pdu_sn_10_p->b1 = pdu_sn_10_p->b1 | 0x20;
                rlc_pP->c_pdu_without_poll     = 0;
                rlc_pP->c_byte_without_poll    = 0;
                //rlc_pP->poll_sn = (rlc_pP->vt_s -1) & RLC_AM_SN_MASK;
                rlc_am_start_timer_poll_retransmit(rlc_pP,frameP);
                rlc_pP->stat_tx_data_pdu                   += 1;
                rlc_pP->stat_tx_retransmit_pdu             += 1;
                rlc_pP->stat_tx_data_bytes                 += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
                rlc_pP->stat_tx_retransmit_bytes           += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
                list_add_tail_eurecom (pdu_p, &rlc_pP->pdus_to_mac_layer);
951 952 953 954 955
                return;
            }
        }
        sn = (sn - 1) & RLC_AM_SN_MASK;
    }
956 957
    // no pdu_p with correct size has been found
    // so re-segment a pdu_p if possible
958
    if (found_pdu) {
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
        LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][FORCE-TRAFFIC] SEND SEGMENT OF DATA PDU SN %04d\n",
              frameP,
              (rlc_pP->is_enb) ? "eNB" : "UE",
              rlc_pP->enb_module_id,
              rlc_pP->ue_module_id,
              rlc_pP->rb_id,
              found_pdu_sn);
        if (rlc_pP->nb_bytes_requested_by_mac > 4) {
            rlc_am_nack_pdu (rlc_pP, frameP, found_pdu_sn, 0, 0x7FFF);
            pdu_p = rlc_am_retransmit_get_subsegment(rlc_pP, frameP, found_pdu_sn, &rlc_pP->nb_bytes_requested_by_mac);
            pdu_sn_10_p = (rlc_am_pdu_sn_10_t*) (&pdu_p->data[sizeof(struct mac_tb_req)]);
            rlc_am_pdu_polling(rlc_pP, frameP, pdu_sn_10_p, rlc_pP->pdu_retrans_buffer[found_pdu_sn].header_and_payload_size);
            pdu_sn_10_p->b1 = pdu_sn_10_p->b1 | 0x20;
            rlc_pP->c_pdu_without_poll     = 0;
            rlc_pP->c_byte_without_poll    = 0;
            //rlc_pP->poll_sn = (rlc_pP->vt_s -1) & RLC_AM_SN_MASK;
            rlc_am_start_timer_poll_retransmit(rlc_pP,frameP);
            rlc_pP->stat_tx_data_pdu                   += 1;
            rlc_pP->stat_tx_retransmit_pdu             += 1;
            rlc_pP->stat_tx_data_bytes                 += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
            rlc_pP->stat_tx_retransmit_bytes           += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
            list_add_tail_eurecom (pdu_p, &rlc_pP->pdus_to_mac_layer);
981 982
            return;
        } else {
983 984 985 986 987 988 989
            LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][FORCE-TRAFFIC] ... BUT NOT ENOUGH BYTES ALLOWED BY MAC %0d\n",
                  frameP,
                  (rlc_pP->is_enb) ? "eNB" : "UE",
                  rlc_pP->enb_module_id,
                  rlc_pP->ue_module_id,
                  rlc_pP->rb_id,
                  rlc_pP->nb_bytes_requested_by_mac);
990 991 992
        }
    }
}