rb_tool.c 12 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 24 25
/***************************************************************************
                          rb_tool.c  -  User-space utility for driving NASMESH IOCTL interface
                             -------------------
    copyright            : (C) 2008 by Eurecom
26
    email                : raymond.knopp@eurecom.fr and navid.nikaein@eurecom.fr
27
 ***************************************************************************
28

29 30 31 32 33 34 35 36 37 38
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
39 40
#include <sys/ioctl.h>
#include <ctype.h>
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

#include <netinet/in.h>
#include <arpa/inet.h>


//

//#include "ral_cdma_variables.h"
//#include "ral_cdma_proto.h"

#include "rrc_nas_primitives.h"
#include "ioctl.h"
#include "constant.h"

//#include "nasmt_constant.h"
//#include "nasmt_iocontrol.h"



// Global variables
//int sd_graal;
int fd;
//char  myIPAddr[16]; // local IP Address
//int meas_counter;
//struct ralu_priv ru_priv;
//struct ralu_priv *ralupriv;
//ioctl
char dummy_buffer[1024];
struct nas_ioctl gifr;
//int wait_start_nas;

/*
//---------------------------------------------------------------------------
int NAS_RALconnect(void)
//---------------------------------------------------------------------------
{
    struct sockaddr_un remote;
    int len,s;

    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        perror("NAS_RALconnect - socket");
        exit(1);
    }
    //
    printf("Trying to connect to NAS ...\n");
    remote.sun_family = AF_UNIX;
    strcpy(remote.sun_path, SOCKET_RAL_TD_CDMA_NAME);
    len = strlen(remote.sun_path) + sizeof(remote.sun_family);

    while (wait_start_graal){
        if (connect(s, (struct sockaddr *)&remote, len) == -1) {
            perror("NAS_RALconnect - waiting for connection - ");
    //        exit(1);
            wait_start_graal = 1;
            sleep(5);
        } else {
            wait_start_graal =0;
            printf("RAL connected to NAS.\n");
        }

    }
    return s;
}

*/

//---------------------------------------------------------------------------
void IAL_NAS_ioctl_init(int inst)
//---------------------------------------------------------------------------
{

  struct nas_msg_statistic_reply *msgrep;
113
  int err;
114 115 116 117

  sprintf(gifr.name, "oai%d",inst);

  // Get an UDP IPv6 socket ??
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  fd=socket(AF_INET6, SOCK_DGRAM, 0);

  if (fd<0) {
    printf("Error opening socket\n");
    exit(1);
  }

  sprintf(gifr.name, "oai%d",inst);

  gifr.type =  NAS_MSG_STATISTIC_REQUEST;
  memset ((void *)dummy_buffer,0,800);
  gifr.msg= &(dummy_buffer[0]);
  msgrep=(struct nas_msg_statistic_reply *)(gifr.msg);
  printf("ioctl :Statistics requested\n");
  err=ioctl(fd, NAS_IOCTL_RRM, &gifr);

  if (err<0) {
    printf("IOCTL error, err=%d\n",err);
  }

  printf("tx_packets = %u, rx_packets = %u\n", msgrep->tx_packets, msgrep->rx_packets);
  printf("tx_bytes = %u, rx_bytes = %u\n", msgrep->tx_bytes, msgrep->rx_bytes);
  printf("tx_errors = %u, rx_errors = %u\n", msgrep->tx_errors, msgrep->rx_errors);
  printf("tx_dropped = %u, rx_dropped = %u\n", msgrep->tx_dropped, msgrep->rx_dropped);


}
145 146 147 148 149 150 151 152 153


#define ADD_RB 0
#define DEL_RB 1

//---------------------------------------------------------------------------
int main(int argc,char **argv)
//---------------------------------------------------------------------------
{
154 155 156
  int err = 0;
  int c = 0;
  int action=0,rbset=0,cxset=0,instset=0,saddr_ipv4set=0,saddr_ipv6set=0,daddr_ipv4set=0,daddr_ipv6set=0,dscpset=0,mpls_outlabelset=0,mpls_inlabelset=0;
157
  char rb[100],cx[100],dscp[100],inst[100],mpls_outgoinglabel[100],mpls_incominglabel[100];
158 159
  struct nas_msg_rb_establishment_request *msgreq = NULL;
  struct nas_msg_class_add_request *msgreq_class = NULL;
Cedric Roux's avatar
Cedric Roux committed
160
  in_addr_t saddr_ipv4 = 0,daddr_ipv4 = 0;
161
  struct in6_addr saddr_ipv6,daddr_ipv6;
162
  unsigned int mpls_outlabel=0,mpls_inlabel=0;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

  char addr_str[46];



  // scan options
  rb[0] = '\0';
  cx[0] = '\0';
  dscp[0] = '\0';
  mpls_incominglabel[0] = '\0';
  mpls_outgoinglabel[0] = '\0';

  while ((c = getopt (argc, argv, "adr:i:c:l:m:s:t:x:y:z:")) != -1)
    switch (c) {
    case 'a':
      action = ADD_RB;
      break;

    case 'd':
      action = DEL_RB;
      break;

    case 'r':
      strcpy(rb,optarg);
      rbset = 1;
      break;

    case 'i':
      strcpy(inst,optarg);
      instset = 1;
      break;

    case 'c':
      strcpy(cx,optarg);
      cxset = 1;
      break;

    case 'l':
      strcpy(mpls_outgoinglabel,optarg);
      mpls_outlabelset=1;
      break;

    case 'm':
      strcpy(mpls_incominglabel,optarg);
      mpls_inlabelset=1;
      break;

210 211 212 213
    case 's': {
      struct in_addr a;
      inet_aton(optarg,&a);
      saddr_ipv4 = a.s_addr;
214 215
      saddr_ipv4set = 1;
      break;
216
    }
217

218 219 220 221
    case 't': {
      struct in_addr a;
      inet_aton(optarg,&a);
      daddr_ipv4 = a.s_addr;
222 223
      daddr_ipv4set = 1;
      break;
224
    }
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    case 'x':
      printf("IPv6: %s\n",optarg);
      inet_pton(AF_INET6,optarg,(void *)&saddr_ipv6);
      saddr_ipv6set = 1;
      break;

    case 'y':
      inet_pton(AF_INET6,optarg,(void *)&daddr_ipv6);
      daddr_ipv6set = 1;
      break;

    case 'z':
      dscpset=1;
      strcpy(dscp,optarg);
      break;

    case '?':
      if (isprint (optopt))
        fprintf (stderr, "Unknown option `-%c'.\n", optopt);
      else
        fprintf (stderr,
                 "Unknown option character `\\x%x'.\n",
                 optopt);

      return 1;

    default:
      abort ();
254 255 256
    }


257
  printf ("action = %d, rb = %s,cx = %s\n", action, rb, cx);
258 259


260 261 262 263
  if (rbset==0) {
    printf("ERROR: Specify a RAB id!!\n");
    exit(-1);
  }
264

265 266 267 268
  if (cxset==0) {
    printf("ERROR: Specify an LCR !!\n");
    exit(-1);
  }
269

270 271 272 273
  if (instset==0) {
    printf("ERROR: Specify an interface !!\n");
    exit(-1);
  }
274

275 276 277 278
  if ((mpls_outlabelset == 0) && (saddr_ipv4set==0) && (saddr_ipv6set==0)) {
    printf("ERROR: Specify a source IP address\n");
    exit(-1);
  }
279

280 281 282 283
  if ((mpls_outlabelset == 0) && (daddr_ipv4set==0) && (daddr_ipv6set==0)) {
    printf("ERROR: Specify a destination IP address\n");
    exit(-1);
  }
284

285 286 287 288
  if ((mpls_outlabelset == 1) && (mpls_inlabelset == 0)) {
    printf("ERROR: Specify an incoming MPLS label\n");
    exit(-1);
  }
289

290 291 292 293
  if ((mpls_inlabelset == 1) && (mpls_outlabelset == 0)) {
    printf("ERROR: Specify an outgoing MPLS label\n");
    exit(-1);
  }
294

295
  IAL_NAS_ioctl_init(atoi(inst));
296

297 298 299 300
  msgreq = (struct nas_msg_rb_establishment_request *)(gifr.msg);
  msgreq->rab_id = atoi(rb);
  msgreq->lcr = atoi(cx);
  msgreq->qos = 0;
301 302


303 304 305
  if (action == ADD_RB) {
    gifr.type =  NAS_MSG_RB_ESTABLISHMENT_REQUEST;
    err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
306
    if (err == -1) perror("ioctl");
307 308


309 310 311 312 313
    if (saddr_ipv4set == 1) {
      msgreq_class = (struct nas_msg_class_add_request *)(gifr.msg);
      msgreq_class->rab_id = atoi(rb);
      msgreq_class->lcr = atoi(cx);
      msgreq_class->version = 4;
314

315 316 317 318 319
      msgreq_class->classref = 0 + (msgreq_class->lcr<<3);
      msgreq_class->dir = NAS_DIRECTION_SEND;
      msgreq_class->fct = NAS_FCT_QOS_SEND;
      msgreq_class->saddr.ipv4 = saddr_ipv4;
      msgreq_class->daddr.ipv4 = daddr_ipv4;
320

321 322
      // TO BE FIXED WHEN WE CAN SPECIFY A PROTOCOL-based rule
      msgreq_class->protocol = NAS_PROTOCOL_DEFAULT;
323

324 325 326 327
      if (dscpset==0)
        msgreq_class->dscp=0;
      else
        msgreq_class->dscp=atoi(dscp);
328 329


330 331
      gifr.type =  NAS_MSG_CLASS_ADD_REQUEST;
      err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
332
      if (err == -1) perror("ioctl");
333 334
      msgreq_class->rab_id = atoi(rb);
      msgreq_class->lcr = atoi(cx);
335

336 337
      msgreq_class->classref = 1+(msgreq_class->lcr<<3);
      msgreq_class->dir = NAS_DIRECTION_RECEIVE;
338 339


340 341 342 343
      msgreq_class->daddr.ipv4 = saddr_ipv4;
      msgreq_class->saddr.ipv4 = daddr_ipv4;
      gifr.type =  NAS_MSG_CLASS_ADD_REQUEST;
      err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
344
      if (err == -1) perror("ioctl");
345
    }
346

347 348 349 350 351
    if (saddr_ipv6set == 1) {
      msgreq_class = (struct nas_msg_class_add_request *)(gifr.msg);
      msgreq_class->rab_id = atoi(rb);
      msgreq_class->lcr = atoi(cx);
      msgreq_class->version = 6;
352

353 354 355 356
      if (dscpset==0)
        msgreq_class->dscp=0;
      else
        msgreq_class->dscp=atoi(dscp);
357

358 359 360
      msgreq_class->classref = 2+(msgreq_class->lcr<<3);
      msgreq_class->dir=NAS_DIRECTION_SEND;
      msgreq_class->fct=NAS_FCT_QOS_SEND;
361

362 363
      // TO BE FIXED WHEN WE CAN SPECIFY A PROTOCOL-based rule
      msgreq_class->protocol = NAS_PROTOCOL_DEFAULT;
364

365 366
      memcpy(&msgreq_class->saddr.ipv6,&saddr_ipv6,16);
      memcpy(&msgreq_class->daddr.ipv6,&daddr_ipv6,16);
367

368 369 370 371
      inet_ntop(AF_INET6,(void *)&saddr_ipv6,addr_str,46);
      printf("IPV6: Source %s\n",addr_str);
      inet_ntop(AF_INET6,(void *)&daddr_ipv6,addr_str,46);
      printf("IPV6: Dest %s\n",addr_str);
372

373 374
      gifr.type =  NAS_MSG_CLASS_ADD_REQUEST;
      err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
375
      if (err == -1) perror("ioctl");
376

377 378
      msgreq_class->rab_id = atoi(rb);
      msgreq_class->lcr = atoi(cx);
379

380 381 382 383 384 385
      msgreq_class->classref = 3+(msgreq_class->lcr<<3);
      msgreq_class->dir=NAS_DIRECTION_RECEIVE;
      memcpy(&msgreq_class->daddr.ipv6,&saddr_ipv6,16);
      memcpy(&msgreq_class->saddr.ipv6,&daddr_ipv6,16);
      gifr.type =  NAS_MSG_CLASS_ADD_REQUEST;
      err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
386
      if (err == -1) perror("ioctl");
387 388


389
    }
390

391
    if (mpls_inlabelset == 1) {
392

393 394 395 396
      msgreq_class = (struct nas_msg_class_add_request *)(gifr.msg);
      msgreq_class->rab_id = atoi(rb);
      msgreq_class->lcr = atoi(cx);
      msgreq_class->version = NAS_MPLS_VERSION_CODE;
397

398 399 400 401
      if (dscpset==0)
        msgreq_class->dscp=0;
      else
        msgreq_class->dscp=atoi(dscp);
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      msgreq_class->classref = 4 + (msgreq_class->lcr<<3);
      msgreq_class->dir=NAS_DIRECTION_SEND;
      msgreq_class->fct=NAS_FCT_QOS_SEND;

      // TO BE FIXED WHEN WE CAN SPECIFY A PROTOCOL-based rule
      msgreq_class->protocol = NAS_PROTOCOL_DEFAULT;

      mpls_outlabel = atoi(mpls_outgoinglabel);

      printf("Setting MPLS outlabel %d with exp %d\n",mpls_outlabel,msgreq_class->dscp);

      msgreq_class->daddr.mpls_label = mpls_outlabel;

      gifr.type =  NAS_MSG_CLASS_ADD_REQUEST;
      err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
418
      if (err == -1) perror("ioctl");
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

      msgreq_class->rab_id = atoi(rb);
      msgreq_class->lcr = atoi(cx);

      msgreq_class->classref = 5 + (msgreq_class->lcr<<3);
      msgreq_class->dir=NAS_DIRECTION_RECEIVE;


      // TO BE FIXED WHEN WE CAN SPECIFY A PROTOCOL-based rule
      msgreq_class->protocol = NAS_PROTOCOL_DEFAULT;

      mpls_inlabel  = atoi(mpls_incominglabel);

      printf("Setting MPLS inlabel %d with exp %d\n",mpls_inlabel,msgreq_class->dscp);

      msgreq_class->daddr.mpls_label = mpls_inlabel;

      gifr.type =  NAS_MSG_CLASS_ADD_REQUEST;
437
      err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
438
      if (err == -1) perror("ioctl");
439 440


441
    }
442 443 444
  } else if (action == DEL_RB) {
    gifr.type =  NAS_MSG_RB_RELEASE_REQUEST;
    err=ioctl(fd, NAS_IOCTL_RRM, &gifr);
445
    if (err == -1) perror("ioctl");
446 447
  }

448 449 450 451 452




}