rlc_am_reassembly.c 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22 23
#define RLC_AM_MODULE 1
#define RLC_AM_REASSEMBLY_C 1
24 25
#include "platform_types.h"
//-----------------------------------------------------------------------------
26
#if ENABLE_ITTI
Lionel Gauthier's avatar
Lionel Gauthier committed
27 28
# include "intertask_interface.h"
#endif
29
#include "assertions.h"
30 31 32 33 34
#include "rlc.h"
#include "rlc_am.h"
#include "list.h"
#include "LAYER2/MAC/extern.h"
#include "UTIL/LOG/log.h"
35
#include "msc.h"
36 37

//-----------------------------------------------------------------------------
38 39
inline void
rlc_am_clear_rx_sdu (
40 41
  const protocol_ctxt_t* const ctxt_pP,
  rlc_am_entity_t * const      rlc_pP)
42
{
43
  rlc_pP->output_sdu_size_to_write = 0;
44 45
}
//-----------------------------------------------------------------------------
46 47
void
rlc_am_reassembly (
48 49 50 51
  const protocol_ctxt_t* const ctxt_pP,
  rlc_am_entity_t * const rlc_pP,
  uint8_t * src_pP,
  const int32_t lengthP)
52
{
53 54
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PAYLOAD] reassembly()  %d bytes\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
55
        lengthP);
56

57
  if (rlc_pP->output_sdu_in_construction == NULL) {
58
    rlc_pP->output_sdu_in_construction = get_free_mem_block (RLC_SDU_MAX_SIZE, __func__);
59 60
    rlc_pP->output_sdu_size_to_write = 0;
    assert(rlc_pP->output_sdu_in_construction != NULL);
61
  }
62

63
  if (rlc_pP->output_sdu_in_construction != NULL) {
64

65 66 67 68 69
    // check if no overflow in size
    if ((rlc_pP->output_sdu_size_to_write + lengthP) <= RLC_SDU_MAX_SIZE) {
      memcpy (&rlc_pP->output_sdu_in_construction->data[rlc_pP->output_sdu_size_to_write], src_pP, lengthP);
      rlc_pP->output_sdu_size_to_write += lengthP;
    } else {
70 71
      LOG_E(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PAYLOAD] ERROR  SDU SIZE OVERFLOW SDU GARBAGED\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
72
#if STOP_ON_IP_TRAFFIC_OVERLOAD
73 74
      AssertFatal(0, PROTOCOL_RLC_AM_CTXT_FMT" RLC_AM_DATA_IND, SDU SIZE OVERFLOW SDU GARBAGED\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
75
#endif
76 77 78
      // erase  SDU
      rlc_pP->output_sdu_size_to_write = 0;
    }
79
  } else {
80 81
    LOG_E(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PAYLOAD] ERROR  OUTPUT SDU IS NULL\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
82
#if STOP_ON_IP_TRAFFIC_OVERLOAD
83 84
    AssertFatal(0, PROTOCOL_RLC_AM_CTXT_FMT" RLC_AM_DATA_IND, SDU DROPPED, OUT OF MEMORY\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
85
#endif
86 87 88
  }
}
//-----------------------------------------------------------------------------
89 90
void
rlc_am_send_sdu (
91 92
  const protocol_ctxt_t* const ctxt_pP,
  rlc_am_entity_t * const      rlc_pP)
93
{
Wilson Thong's avatar
Wilson Thong committed
94
#   if TRACE_RLC_AM_PDU
Lionel Gauthier's avatar
Lionel Gauthier committed
95 96
  char                 message_string[7000];
  size_t               message_string_size = 0;
97
#if ENABLE_ITTI
Lionel Gauthier's avatar
Lionel Gauthier committed
98
  MessageDef          *msg_p;
99
#endif
Lionel Gauthier's avatar
Lionel Gauthier committed
100 101 102
  int                  octet_index, index;
#endif

103
  if ((rlc_pP->output_sdu_in_construction)) {
104 105
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND_SDU] %d bytes sdu %p\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
106 107 108 109 110 111
          rlc_pP->output_sdu_size_to_write,
          rlc_pP->output_sdu_in_construction);

    if (rlc_pP->output_sdu_size_to_write > 0) {
      rlc_pP->stat_rx_pdcp_sdu   += 1;
      rlc_pP->stat_rx_pdcp_bytes += rlc_pP->output_sdu_size_to_write;
112
#if TEST_RLC_AM
113 114 115 116
      rlc_am_v9_3_0_test_data_ind (rlc_pP->module_id,
                                   rlc_pP->rb_id,
                                   rlc_pP->output_sdu_size_to_write,
                                   rlc_pP->output_sdu_in_construction);
117
#else
118
#   if TRACE_RLC_AM_PDU
119 120 121 122 123 124 125 126 127 128 129 130
      message_string_size += sprintf(&message_string[message_string_size], "Bearer      : %u\n", rlc_pP->rb_id);
      message_string_size += sprintf(&message_string[message_string_size], "SDU size    : %u\n", rlc_pP->output_sdu_size_to_write);

      message_string_size += sprintf(&message_string[message_string_size], "\nPayload  : \n");
      message_string_size += sprintf(&message_string[message_string_size], "------+-------------------------------------------------|\n");
      message_string_size += sprintf(&message_string[message_string_size], "      |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |\n");
      message_string_size += sprintf(&message_string[message_string_size], "------+-------------------------------------------------|\n");

      for (octet_index = 0; octet_index < rlc_pP->output_sdu_size_to_write; octet_index++) {
        if ((octet_index % 16) == 0) {
          if (octet_index != 0) {
            message_string_size += sprintf(&message_string[message_string_size], " |\n");
Lionel Gauthier's avatar
Lionel Gauthier committed
131
          }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

          message_string_size += sprintf(&message_string[message_string_size], " %04d |", octet_index);
        }

        /*
         * Print every single octet in hexadecimal form
         */
        message_string_size += sprintf(&message_string[message_string_size],
                                       " %02x",
                                       rlc_pP->output_sdu_in_construction->data[octet_index]);
        /*
         * Align newline and pipes according to the octets in groups of 2
         */
      }

      /*
       * Append enough spaces and put final pipe
       */
      for (index = octet_index; index < 16; ++index) {
        message_string_size += sprintf(&message_string[message_string_size], "   ");
      }

      message_string_size += sprintf(&message_string[message_string_size], " |\n");
Lionel Gauthier's avatar
Lionel Gauthier committed
155

Lionel Gauthier's avatar
Lionel Gauthier committed
156

157

158
#      if ENABLE_ITTI
159 160 161 162 163 164
      msg_p = itti_alloc_new_message_sized (ctxt_pP->enb_flag > 0 ? TASK_RLC_ENB:TASK_RLC_UE ,
                                            RLC_AM_SDU_IND,
                                            message_string_size + sizeof (IttiMsgText));
      msg_p->ittiMsg.rlc_am_sdu_ind.size = message_string_size;
      memcpy(&msg_p->ittiMsg.rlc_am_sdu_ind.text, message_string, message_string_size);

165
      itti_send_msg_to_task(TASK_UNKNOWN, ctxt_pP->instance, msg_p);
166

Lionel Gauthier's avatar
Lionel Gauthier committed
167
#      else
168
      LOG_T(RLC, "%s", message_string);
Lionel Gauthier's avatar
Lionel Gauthier committed
169 170
#      endif
#   endif
171
#if !ENABLE_ITTI
Raymond Knopp's avatar
Raymond Knopp committed
172
      RLC_AM_MUTEX_UNLOCK(&rlc_pP->lock_input_sdus);
173
#endif
Lionel Gauthier's avatar
Lionel Gauthier committed
174 175 176 177 178 179 180 181 182 183 184
      MSC_LOG_TX_MESSAGE(
        (ctxt_pP->enb_flag == ENB_FLAG_YES) ? MSC_RLC_ENB:MSC_RLC_UE,
        (ctxt_pP->enb_flag == ENB_FLAG_YES) ? MSC_PDCP_ENB:MSC_PDCP_UE,
        (const char*)(rlc_pP->output_sdu_in_construction->data),
        rlc_pP->output_sdu_size_to_write,
        MSC_AS_TIME_FMT" "PROTOCOL_RLC_AM_MSC_FMT" DATA-IND size %u",
        MSC_AS_TIME_ARGS(ctxt_pP),
        PROTOCOL_RLC_AM_MSC_ARGS(ctxt_pP,rlc_pP),
        rlc_pP->output_sdu_size_to_write
      );

185 186 187 188 189 190
      rlc_data_ind (ctxt_pP,
                    BOOL_NOT(rlc_pP->is_data_plane),
                    MBMS_FLAG_NO,
                    rlc_pP->rb_id,
                    rlc_pP->output_sdu_size_to_write,
                    rlc_pP->output_sdu_in_construction);
191
#if !ENABLE_ITTI
Raymond Knopp's avatar
Raymond Knopp committed
192
      RLC_AM_MUTEX_LOCK(&rlc_pP->lock_input_sdus, ctxt_pP, rlc_pP);
193
#endif
194
#endif
195 196
      rlc_pP->output_sdu_in_construction = NULL;
    } else {
197 198
      LOG_E(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND_SDU] ERROR SIZE <= 0 ... DO NOTHING, SET SDU SIZE TO 0\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
199
      //msg("[RLC_AM][MOD %d] Freeing mem_block ...\n", rlc_pP->module_id);
200
      //free_mem_block (rlc_pP->output_sdu_in_construction, __func__);
201
      AssertFatal(3==4,
202 203
                  PROTOCOL_RLC_AM_CTXT_FMT" SEND SDU REQUESTED %d bytes",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
Lionel Gauthier's avatar
Lionel Gauthier committed
204
                  rlc_pP->output_sdu_size_to_write);
205 206 207
    }

    rlc_pP->output_sdu_size_to_write = 0;
208 209 210
  }
}
//-----------------------------------------------------------------------------
211 212
void
rlc_am_reassemble_pdu(
213 214
  const protocol_ctxt_t* const ctxt_pP,
  rlc_am_entity_t * const      rlc_pP,
215 216
  mem_block_t * const          tb_pP,
  boolean_t free_rlc_pdu)
217
{
218
  int i,j;
219

220
  rlc_am_pdu_info_t* pdu_info        = &((rlc_am_rx_pdu_management_t*)(tb_pP->data))->pdu_info;
221 222
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU SN=%03d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
223
        pdu_info->sn);
224
#if TRACE_RLC_AM_RX_DECODE
225
  rlc_am_display_data_pdu_infos(ctxt_pP, rlc_pP, pdu_info);
226 227
#endif

228
  if (pdu_info->e == RLC_E_FIXED_PART_DATA_FIELD_FOLLOW) {
229 230
    switch (pdu_info->fi) {
    case RLC_FI_1ST_BYTE_DATA_IS_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_LAST_BYTE_SDU:
231 232
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU NO E_LI FI=11 (00)\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
233 234 235 236 237 238 239 240
      // one complete SDU
      rlc_am_send_sdu(ctxt_pP, rlc_pP); // may be not necessary
      rlc_am_reassembly (ctxt_pP, rlc_pP, pdu_info->payload, pdu_info->payload_size);
      rlc_am_send_sdu(ctxt_pP, rlc_pP); // may be not necessary
      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;

    case RLC_FI_1ST_BYTE_DATA_IS_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_NOT_LAST_BYTE_SDU:
241 242
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU NO E_LI FI=10 (01)\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
243 244 245 246 247 248 249
      // one beginning segment of SDU in PDU
      rlc_am_send_sdu(ctxt_pP, rlc_pP); // may be not necessary
      rlc_am_reassembly (ctxt_pP, rlc_pP,pdu_info->payload, pdu_info->payload_size);
      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;

    case RLC_FI_1ST_BYTE_DATA_IS_NOT_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_LAST_BYTE_SDU:
250 251
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU NO E_LI FI=01 (10)\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
252 253 254 255 256 257 258 259 260
      // one last segment of SDU
      //if (rlc_pP->reassembly_missing_sn_detected == 0) {
      rlc_am_reassembly (ctxt_pP, rlc_pP, pdu_info->payload, pdu_info->payload_size);
      rlc_am_send_sdu(ctxt_pP, rlc_pP);
      //} // else { clear sdu already done
      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;

    case RLC_FI_1ST_BYTE_DATA_IS_NOT_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_NOT_LAST_BYTE_SDU:
261 262
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU NO E_LI FI=00 (11)\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
263 264 265 266 267 268 269 270
      //if (rlc_pP->reassembly_missing_sn_detected == 0) {
      // one whole segment of SDU in PDU
      rlc_am_reassembly (ctxt_pP, rlc_pP, pdu_info->payload, pdu_info->payload_size);
      //} else {
      //    rlc_pP->reassembly_missing_sn_detected = 1; // not necessary but for readability of the code
      //}

      break;
271
#if USER_MODE
272 273 274

    default:
      assert(0 != 0);
275
#endif
276
    }
277
  } else {
278 279
    switch (pdu_info->fi) {
    case RLC_FI_1ST_BYTE_DATA_IS_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_LAST_BYTE_SDU:
280 281
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU FI=11 (00) Li=",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
282 283 284 285 286 287 288 289 290 291 292 293 294

      for (i=0; i < pdu_info->num_li; i++) {
        LOG_D(RLC, "%d ",pdu_info->li_list[i]);
      }

      LOG_D(RLC, "\n");
      //msg(" remaining size %d\n",size);
      // N complete SDUs
      rlc_am_send_sdu(ctxt_pP, rlc_pP);
      j = 0;

      for (i = 0; i < pdu_info->num_li; i++) {
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->li_list[i]);
295
        rlc_am_send_sdu(ctxt_pP, rlc_pP);
296 297 298 299 300 301 302 303 304 305 306 307 308
        j = j + pdu_info->li_list[i];
      }

      if (pdu_info->hidden_size > 0) { // normally should always be > 0 but just for help debug
        // data is already ok, done by last loop above
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->hidden_size);
        rlc_am_send_sdu(ctxt_pP, rlc_pP);
      }

      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;

    case RLC_FI_1ST_BYTE_DATA_IS_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_NOT_LAST_BYTE_SDU:
309 310
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU FI=10 (01) Li=",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
311 312 313 314 315 316 317 318 319 320 321 322 323

      for (i=0; i < pdu_info->num_li; i++) {
        LOG_D(RLC, "%d ",pdu_info->li_list[i]);
      }

      LOG_D(RLC, "\n");
      //msg(" remaining size %d\n",size);
      // N complete SDUs + one segment of SDU in PDU
      rlc_am_send_sdu(ctxt_pP, rlc_pP);
      j = 0;

      for (i = 0; i < pdu_info->num_li; i++) {
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->li_list[i]);
324
        rlc_am_send_sdu(ctxt_pP, rlc_pP);
325 326 327 328 329 330 331 332 333 334 335 336
        j = j + pdu_info->li_list[i];
      }

      if (pdu_info->hidden_size > 0) { // normally should always be > 0 but just for help debug
        // data is already ok, done by last loop above
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->hidden_size);
      }

      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;

    case RLC_FI_1ST_BYTE_DATA_IS_NOT_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_LAST_BYTE_SDU:
337 338
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU FI=01 (10) Li=",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

      for (i=0; i < pdu_info->num_li; i++) {
        LOG_D(RLC, "%d ",pdu_info->li_list[i]);
      }

      LOG_D(RLC, "\n");
      //msg(" remaining size %d\n",size);
      // one last segment of SDU + N complete SDUs in PDU
      j = 0;

      for (i = 0; i < pdu_info->num_li; i++) {
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->li_list[i]);
        rlc_am_send_sdu(ctxt_pP, rlc_pP);
        j = j + pdu_info->li_list[i];
      }

      if (pdu_info->hidden_size > 0) { // normally should always be > 0 but just for help debug
        // data is already ok, done by last loop above
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->hidden_size);
        rlc_am_send_sdu(ctxt_pP, rlc_pP);
      }

      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;

    case RLC_FI_1ST_BYTE_DATA_IS_NOT_1ST_BYTE_SDU_LAST_BYTE_DATA_IS_NOT_LAST_BYTE_SDU:
365 366
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[REASSEMBLY PDU] TRY REASSEMBLY PDU FI=00 (11) Li=",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

      for (i=0; i < pdu_info->num_li; i++) {
        LOG_D(RLC, "%d ",pdu_info->li_list[i]);
      }

      LOG_D(RLC, "\n");
      //msg(" remaining size %d\n",size);
      j = 0;

      for (i = 0; i < pdu_info->num_li; i++) {
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->li_list[i]);
        rlc_am_send_sdu(ctxt_pP, rlc_pP);
        j = j + pdu_info->li_list[i];
      }

      if (pdu_info->hidden_size > 0) { // normally should always be > 0 but just for help debug
        // data is already ok, done by last loop above
        rlc_am_reassembly (ctxt_pP, rlc_pP, &pdu_info->payload[j], pdu_info->hidden_size);
      } else {
386
#if USER_MODE
387
        //assert (5!=5);
388
#endif
389 390 391 392
      }

      //rlc_pP->reassembly_missing_sn_detected = 0;
      break;
393
#if USER_MODE
394 395 396

    default:
      assert(1 != 1);
397
#endif
398
    }
399
  }
400

401 402 403
  if (free_rlc_pdu) {
	  free_mem_block(tb_pP, __func__);
  }
404
}