Skip to content
Snippets Groups Projects
lte-ue.c 94.1 KiB
Newer Older
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421

        //          usleep(3000);
        if(sub_frame == 0) {
          //UE->proc.proc_rxtx[0].frame_rx++;
          //UE->proc.proc_rxtx[1].frame_rx++;
          for (th_id=0; th_id < RX_NB_TH; th_id++) {
            UE->proc.proc_rxtx[th_id].frame_rx++;
          }
        }

        proc->subframe_rx=sub_frame;
        proc->subframe_tx=(sub_frame+4)%10;
        proc->frame_tx = proc->frame_rx + (proc->subframe_rx>5?1:0);
        proc->timestamp_tx = timestamp+
                             (4*UE->frame_parms.samples_per_tti)-
                             UE->frame_parms.ofdm_symbol_size-UE->frame_parms.nb_prefix_samples0;
        proc->instance_cnt_rxtx++;
        LOG_D( PHY, "[SCHED][UE %d] UE RX instance_cnt_rxtx %d subframe %d !!\n", UE->Mod_id, proc->instance_cnt_rxtx,proc->subframe_rx);
        T(T_UE_MASTER_TICK, T_INT(0), T_INT(proc->frame_rx%1024), T_INT(proc->subframe_rx));
        AssertFatal (pthread_cond_signal(&proc->cond_rxtx) ==0,"");
        AssertFatal(pthread_mutex_unlock(&proc->mutex_rxtx) ==0,"");
      } // start_rx_stream==1
    } // UE->is_synchronized==1
  } // while !oai_exit

  return NULL;
}


/*!
 * \brief Initialize the UE theads.
 * Creates the UE threads:
 * - UE_thread_rxtx0
 * - UE_thread_rxtx1
 * - UE_thread_synch
 * - UE_thread_fep_slot0
 * - UE_thread_fep_slot1
 * - UE_thread_dlsch_proc_slot0
 * - UE_thread_dlsch_proc_slot1
 * and the locking between them.
 */
void init_UE_threads(int inst) {
  struct rx_tx_thread_data *rtd;
  PHY_VARS_UE *UE;
  AssertFatal(PHY_vars_UE_g!=NULL,"PHY_vars_UE_g is NULL\n");
  AssertFatal(PHY_vars_UE_g[inst]!=NULL,"PHY_vars_UE_g[inst] is NULL\n");
  AssertFatal(PHY_vars_UE_g[inst][0]!=NULL,"PHY_vars_UE_g[inst][0] is NULL\n");
  UE = PHY_vars_UE_g[inst][0];
  pthread_attr_init (&UE->proc.attr_ue);
  pthread_attr_setstacksize(&UE->proc.attr_ue,8192);//5*PTHREAD_STACK_MIN);
  pthread_mutex_init(&UE->proc.mutex_synch,NULL);
  pthread_cond_init(&UE->proc.cond_synch,NULL);
  UE->proc.instance_cnt_synch = -1;
  UE->is_synchronized = 0;
  // the threads are not yet active, therefore access is allowed without locking
  int nb_threads=RX_NB_TH;

  for (int i=0; i<nb_threads; i++) {
    rtd = calloc(1, sizeof(struct rx_tx_thread_data));

    if (rtd == NULL) abort();

    rtd->UE = UE;
    rtd->proc = &UE->proc.proc_rxtx[i];
    pthread_mutex_init(&UE->proc.proc_rxtx[i].mutex_rxtx,NULL);
    pthread_cond_init(&UE->proc.proc_rxtx[i].cond_rxtx,NULL);
    UE->proc.proc_rxtx[i].instance_cnt_rxtx = -1;
    UE->proc.proc_rxtx[i].sub_frame_start=i;
    UE->proc.proc_rxtx[i].sub_frame_step=nb_threads;
    printf("Init_UE_threads rtd %d proc %d nb_threads %d i %d\n",rtd->proc->sub_frame_start, UE->proc.proc_rxtx[i].sub_frame_start,nb_threads, i);
    pthread_create(&UE->proc.proc_rxtx[i].pthread_rxtx, NULL, UE_thread_rxn_txnp4, rtd);
#ifdef UE_SLOT_PARALLELISATION
    //pthread_mutex_init(&UE->proc.proc_rxtx[i].mutex_slot0_dl_processing,NULL);
    //pthread_cond_init(&UE->proc.proc_rxtx[i].cond_slot0_dl_processing,NULL);
    //pthread_create(&UE->proc.proc_rxtx[i].pthread_slot0_dl_processing,NULL,UE_thread_slot0_dl_processing, rtd);
    pthread_mutex_init(&UE->proc.proc_rxtx[i].mutex_slot1_dl_processing,NULL);
    pthread_cond_init(&UE->proc.proc_rxtx[i].cond_slot1_dl_processing,NULL);
    pthread_create(&UE->proc.proc_rxtx[i].pthread_slot1_dl_processing,NULL,UE_thread_slot1_dl_processing, rtd);
#endif
  }

  pthread_create(&UE->proc.pthread_synch,NULL,UE_thread_synch,(void *)UE);
}


/*!
 * \brief Initialize the UE theads.
 * Creates the UE threads:
 * - UE_thread_rxtx0
 * - UE_thread_synch
 * - UE_thread_fep_slot0
 * - UE_thread_fep_slot1
 * - UE_thread_dlsch_proc_slot0
 * - UE_thread_dlsch_proc_slot1
 * and the locking between them.
 */
void init_UE_single_thread_stub(int nb_inst) {
  struct rx_tx_thread_data *rtd;
  PHY_VARS_UE *UE;

  for (int i=0; i<nb_inst; i++) {
    AssertFatal(PHY_vars_UE_g!=NULL,"PHY_vars_UE_g is NULL\n");
    AssertFatal(PHY_vars_UE_g[i]!=NULL,"PHY_vars_UE_g[inst] is NULL\n");
    AssertFatal(PHY_vars_UE_g[i][0]!=NULL,"PHY_vars_UE_g[inst][0] is NULL\n");

    if(NFAPI_MODE==NFAPI_UE_STUB_PNF || NFAPI_MODE==NFAPI_MODE_STANDALONE_PNF) {
#ifdef NAS_UE
      MessageDef *message_p;
      message_p = itti_alloc_new_message(TASK_NAS_UE, 0, INITIALIZE_MESSAGE);
      itti_send_msg_to_task (TASK_NAS_UE, i + NB_eNB_INST, message_p);
#endif
    }
  }

  UE = PHY_vars_UE_g[0][0];
  pthread_attr_init (&UE->proc.attr_ue);
  pthread_attr_setstacksize(&UE->proc.attr_ue,8192);//5*PTHREAD_STACK_MIN);
  // Don't need synch for phy_stub mode
  //pthread_mutex_init(&UE->proc.mutex_synch,NULL);
  //pthread_cond_init(&UE->proc.cond_synch,NULL);
  // the threads are not yet active, therefore access is allowed without locking
  // In phy_stub_UE mode due to less heavy processing operations we don't need two threads
  //int nb_threads=RX_NB_TH;
  int nb_threads=1;
  void *(*task_func)(void *);

  if (NFAPI_MODE == NFAPI_MODE_STANDALONE_PNF) {
    task_func = UE_phy_stub_standalone_pnf_task;
  } else {
    task_func = UE_phy_stub_single_thread_rxn_txnp4;
  }

  for(uint16_t ue_thread_id = 0; ue_thread_id < NB_THREAD_INST; ue_thread_id++) {
    UE = PHY_vars_UE_g[ue_thread_id][0];

    for (int i=0; i<nb_threads; i++) {
      rtd = calloc(1, sizeof(struct rx_tx_thread_data));

      if (rtd == NULL) abort();

      rtd->UE = UE;
      rtd->proc = &UE->proc.proc_rxtx[i];
      rtd->ue_thread_id = ue_thread_id;
      pthread_mutex_init(&UE->proc.proc_rxtx[i].mutex_rxtx,NULL);
      pthread_cond_init(&UE->proc.proc_rxtx[i].cond_rxtx,NULL);
      UE->proc.proc_rxtx[i].sub_frame_start=i;
      UE->proc.proc_rxtx[i].sub_frame_step=nb_threads;
      printf("Init_UE_threads rtd %d proc %d nb_threads %d i %d\n",rtd->proc->sub_frame_start, UE->proc.proc_rxtx[i].sub_frame_start,nb_threads, i);
      pthread_create(&UE->proc.proc_rxtx[i].pthread_rxtx, NULL, task_func, rtd);
      pthread_setname_np(UE->proc.proc_rxtx[i].pthread_rxtx, "oai:ue-phy");
    }
  }

  // Remove thread for UE_sync in phy_stub_UE mode.
  //pthread_create(&UE->proc.pthread_synch,NULL,UE_thread_synch,(void*)UE);
}


/*!
 * \brief Initialize the UE theads.
 * Creates the UE threads:
 * - UE_thread_rxtx0
 * - UE_thread_synch
 * - UE_thread_fep_slot0
 * - UE_thread_fep_slot1
 * - UE_thread_dlsch_proc_slot0
 * - UE_thread_dlsch_proc_slot1
 * and the locking between them.
 */
void init_UE_threads_stub(int inst) {
  struct rx_tx_thread_data *rtd;
  PHY_VARS_UE *UE;
  AssertFatal(PHY_vars_UE_g!=NULL,"PHY_vars_UE_g is NULL\n");
  AssertFatal(PHY_vars_UE_g[inst]!=NULL,"PHY_vars_UE_g[inst] is NULL\n");
  AssertFatal(PHY_vars_UE_g[inst][0]!=NULL,"PHY_vars_UE_g[inst][0] is NULL\n");
  UE = PHY_vars_UE_g[inst][0];
  pthread_attr_init (&UE->proc.attr_ue);
  pthread_attr_setstacksize(&UE->proc.attr_ue,8192);//5*PTHREAD_STACK_MIN);
  // Don't need synch for phy_stub mode
  //pthread_mutex_init(&UE->proc.mutex_synch,NULL);
  //pthread_cond_init(&UE->proc.cond_synch,NULL);
  // the threads are not yet active, therefore access is allowed without locking
  // In phy_stub_UE mode due to less heavy processing operations we don't need two threads
  //int nb_threads=RX_NB_TH;
  int nb_threads=1;

  for (int i=0; i<nb_threads; i++) {
    rtd = calloc(1, sizeof(struct rx_tx_thread_data));

    if (rtd == NULL) abort();

    rtd->UE = UE;
    rtd->proc = &UE->proc.proc_rxtx[i];
    pthread_mutex_init(&UE->proc.proc_rxtx[i].mutex_rxtx,NULL);
    pthread_cond_init(&UE->proc.proc_rxtx[i].cond_rxtx,NULL);
    UE->proc.proc_rxtx[i].sub_frame_start=i;
    UE->proc.proc_rxtx[i].sub_frame_step=nb_threads;
    printf("Init_UE_threads rtd %d proc %d nb_threads %d i %d\n",rtd->proc->sub_frame_start, UE->proc.proc_rxtx[i].sub_frame_start,nb_threads, i);
    pthread_create(&UE->proc.proc_rxtx[i].pthread_rxtx, NULL, UE_phy_stub_thread_rxn_txnp4, rtd);
  }

  // Remove thread for UE_sync in phy_stub_UE mode.
  //pthread_create(&UE->proc.pthread_synch,NULL,UE_thread_synch,(void*)UE);
}


void fill_ue_band_info(void) {
  LTE_UE_EUTRA_Capability_t *UE_EUTRA_Capability = UE_rrc_inst[0].UECap->UE_EUTRA_Capability;
  int i,j;
  bands_to_scan.nbands = UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.count;

  for (i=0; i<bands_to_scan.nbands; i++) {
    for (j=0; j<sizeof (eutra_bands) / sizeof (eutra_bands[0]); j++)
      if (eutra_bands[j].band == UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.array[i]->bandEUTRA) {
        memcpy(&bands_to_scan.band_info[i],
               &eutra_bands[j],
               sizeof(eutra_band_t));
        printf("Band %d (%lu) : DL %u..%u Hz, UL %u..%u Hz, Duplex %s \n",
               bands_to_scan.band_info[i].band,
               UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.array[i]->bandEUTRA,
               bands_to_scan.band_info[i].dl_min,
               bands_to_scan.band_info[i].dl_max,
               bands_to_scan.band_info[i].ul_min,
               bands_to_scan.band_info[i].ul_max,
               (bands_to_scan.band_info[i].frame_type==FDD) ? "FDD" : "TDD");
        break;
      }
  }
}

int setup_ue_buffers(PHY_VARS_UE **phy_vars_ue,
                     openair0_config_t *openair0_cfg) {
  int i, CC_id;
  LTE_DL_FRAME_PARMS *frame_parms;

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    AssertFatal( phy_vars_ue[CC_id] !=0, "");
    frame_parms = &(phy_vars_ue[CC_id]->frame_parms);
    // replace RX signal buffers with mmaped HW versions
    for (i=0; i<frame_parms->nb_antennas_rx; i++) {
      LOG_I(PHY, "Mapping UE CC_id %d, rx_ant %d, freq %lu on card %d, chain %d\n",
            CC_id, i, downlink_frequency[CC_id][i], phy_vars_ue[CC_id]->rf_map.card, (phy_vars_ue[CC_id]->rf_map.chain)+i );
      free( phy_vars_ue[CC_id]->common_vars.rxdata[i] );
      phy_vars_ue[CC_id]->common_vars.rxdata[i] = malloc16_clear( 307200*sizeof(int32_t) ); // what about the "-N_TA_offset" ? // N_TA offset for TDD
    }

    for (i=0; i<frame_parms->nb_antennas_tx; i++) {
      LOG_I(PHY, "Mapping UE CC_id %d, tx_ant %d, freq %lu on card %d, chain %d\n",
            CC_id, i, downlink_frequency[CC_id][i], phy_vars_ue[CC_id]->rf_map.card, (phy_vars_ue[CC_id]->rf_map.chain)+i );
      free( phy_vars_ue[CC_id]->common_vars.txdata[i] );
      phy_vars_ue[CC_id]->common_vars.txdata[i] = malloc16_clear( 307200*sizeof(int32_t) );
    }

    // rxdata[x] points now to the same memory region as phy_vars_ue[CC_id]->common_vars.rxdata[x]
    // txdata[x] points now to the same memory region as phy_vars_ue[CC_id]->common_vars.txdata[x]
    // be careful when releasing memory!
    // because no "release_ue_buffers"-function is available, at least rxdata and txdata memory will leak (only some bytes)
  }

  return 0;
}


// Panos: This timer thread is used only in the phy_stub mode as an independent timer
// which will be ticking and provide the SFN/SF values that will be used from the UE threads
// playing the role of nfapi-pnf.

//02/02/2018
static void *timer_thread( void *param ) {
  thread_top_init("timer_thread",1,870000L,1000000L,1000000L);
  timer_subframe =9;
  timer_frame    =1023;
  //phy_stub_ticking = (SF_ticking*)malloc(sizeof(SF_ticking));
  phy_stub_ticking->ticking_var = -1;
  PHY_VARS_UE *UE;
  UE = PHY_vars_UE_g[0][0];
  //double t_diff;
  int external_timer = 0;
  wait_sync("timer_thread");
  opp_enabled = 1;

  // first check if we are receiving timing indications
  if(NFAPI_MODE==NFAPI_UE_STUB_OFFNET) {
    usleep(10000);

    if (UE->instance_cnt_timer > 0) {
      external_timer = 1;
      int absSFm1 = ((emulator_absSF+10239)%10240);
      timer_frame = absSFm1/10;
      timer_subframe = absSFm1%10;
      pthread_mutex_lock(&UE->timer_mutex);
      UE->instance_cnt_timer = -1;
      pthread_mutex_unlock(&UE->timer_mutex);
      LOG_I(PHY,"Running with external timer\n");
    } else LOG_I(PHY,"Running with internal timer\n");
  }

  struct timespec t_start;

  struct timespec t_now;

  struct timespec t_sleep;

  uint64_t T_0;

  uint64_t T_now;

  uint64_t T_next_SF;

  uint64_t T_sleep;

  uint64_t sf_cnt = 0; //Total Subframe counter

  clock_gettime(CLOCK_MONOTONIC, &t_start);

  T_0 = (uint64_t) t_start.tv_sec*1000000000 + t_start.tv_nsec;

  LOG_D(MAC, "timer_thread(), T_0 value: %" PRId64 "\n", T_0);

  while (!oai_exit) {
    // these are local subframe/frame counters to check that we are in synch with the fronthaul timing.
    // They are set on the first rx/tx in the underly FH routines.
    if (timer_subframe==9) {
      timer_subframe=0;
      timer_frame++;
      timer_frame&=1023;
    } else {
      timer_subframe++;
    }

    //AssertFatal( 0 == pthread_cond_signal(&phy_stub_ticking->cond_ticking), "");
    AssertFatal(pthread_mutex_lock(&phy_stub_ticking->mutex_ticking) ==0,"");
    phy_stub_ticking->ticking_var++;

    // This should probably be a call to pthread_cond_broadcast when we introduce support for multiple UEs (threads)
    if(phy_stub_ticking->ticking_var == 0) {
      //AssertFatal(phy_stub_ticking->ticking_var == 0,"phy_stub_ticking->ticking_var = %d",
      //phy_stub_ticking->ticking_var);
      if (pthread_cond_signal(&phy_stub_ticking->cond_ticking) != 0) {
        //LOG_E( PHY, "[SCHED][UE %d] ERROR pthread_cond_signal for UE RX thread\n", UE->Mod_id);
        LOG_E( PHY, "timer_thread ERROR pthread_cond_signal for UE_thread\n");
        exit_fun("nothing to add");
      }
    } else
      LOG_D(MAC, "timer_thread() Timing problem! ticking_var value:%d \n \n \n", phy_stub_ticking->ticking_var);

    AssertFatal(pthread_mutex_unlock(&phy_stub_ticking->mutex_ticking) ==0,"");
    start_meas(&UE->timer_stats);

    //clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); // get initial time-stamp
    if (external_timer == 0) {
      clock_gettime(CLOCK_MONOTONIC, &t_now);
      sf_cnt++;
      T_next_SF = T_0 + sf_cnt*1000000;
      T_now =(uint64_t) t_now.tv_sec*1000000000 + t_now.tv_nsec;

      if(T_now > T_next_SF) {
        t_sleep.tv_sec =0;
        t_sleep.tv_nsec =0;
      } else {
        T_sleep = T_next_SF - T_now;
        t_sleep.tv_sec =0;
        t_sleep.tv_nsec = (__syscall_slong_t) T_sleep;
      }

      nanosleep(&t_sleep, (struct timespec *)NULL);
      UE_tport_t pdu;
      pdu.header.packet_type = TTI_SYNC;
      pdu.header.absSF = (timer_frame*10)+timer_subframe;

      if (NFAPI_MODE != NFAPI_UE_STUB_PNF && NFAPI_MODE != NFAPI_MODE_STANDALONE_PNF) {
        multicast_link_write_sock(0,
                                  (char *)&pdu,
                                  sizeof(UE_tport_header_t));
      }
    } else {
      wait_on_condition(&UE->timer_mutex,&UE->timer_cond,&UE->instance_cnt_timer,"timer_thread");
      release_thread(&UE->timer_mutex,&UE->instance_cnt_timer,"timer_thread");
    }

    /*stop_meas(&UE->timer_stats);
    t_diff = get_time_meas_us(&UE->timer_stats);

    stop_meas(&UE->timer_stats);
    t_diff = get_time_meas_us(&UE->timer_stats);*/
  }

  free(phy_stub_ticking);
  pthread_cond_destroy(&phy_stub_ticking->cond_ticking);
  pthread_mutex_destroy(&phy_stub_ticking->mutex_ticking);
  return 0;
}


int init_timer_thread(void) {
  //PHY_VARS_UE *UE=PHY_vars_UE_g[0];
  PHY_VARS_UE *UE=PHY_vars_UE_g[0][0];
  phy_stub_ticking = (SF_ticking *)malloc(sizeof(SF_ticking));
  pthread_mutex_init(&UE->timer_mutex,NULL);
  pthread_cond_init(&UE->timer_cond,NULL);
  UE->instance_cnt_timer = -1;
  memset(&phy_stub_ticking->num_single_thread[0],0,sizeof(int)*NB_THREAD_INST);
  pthread_mutex_init(&phy_stub_ticking->mutex_ticking,NULL);
  pthread_cond_init(&phy_stub_ticking->cond_ticking,NULL);
  pthread_mutex_init(&phy_stub_ticking->mutex_single_thread,NULL);
  pthread_cond_init(&phy_stub_ticking->cond_single_thread,NULL);
  pthread_create(&phy_stub_ticking->pthread_timer, NULL, &timer_thread, NULL);
  return 0;
}


/* HACK: this function is needed to compile the UE
 * fix it somehow
 */
int8_t find_dlsch(uint16_t rnti,
                  PHY_VARS_eNB *eNB,
                  find_type_t type) {
  printf("you cannot read this\n");
  abort();
}