transport_split.c 6.53 KB
Newer Older
1
/*
laurent's avatar
laurent committed
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.1  (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
*
* Author and copyright: Laurent Thomas, open-cells.com
*
* 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
*/
laurent's avatar
laurent committed
23 24 25



laurent's avatar
laurent committed
26 27 28 29 30
#include <executables/split_headers.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
laurent's avatar
laurent committed
31
#include <netdb.h>
32
#include <targets/RT/USER/lte-softmodem.h>
laurent's avatar
laurent committed
33 34 35 36 37 38 39 40 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

bool createUDPsock (char *sourceIP, char *sourcePort, char *destIP, char *destPort, UDPsock_t *result) {
  struct addrinfo hints= {0}, *servinfo, *p;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  int status;

  if ((status = getaddrinfo(sourceIP, sourcePort, &hints, &servinfo)) != 0) {
    LOG_E(GTPU,"getaddrinfo error: %s\n", gai_strerror(status));
    return false;
  }

  // loop through all the results and bind to the first we can
  for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((result->sockHandler = socket(p->ai_family, p->ai_socktype,
                                      p->ai_protocol)) == -1) {
      LOG_W(GTPU,"socket: %s\n", strerror(errno));
      continue;
    }

    if (bind(result->sockHandler, p->ai_addr, p->ai_addrlen) == -1) {
      close(result->sockHandler);
      LOG_W(GTPU,"bind: %s\n", strerror(errno));
      continue;
    }

    break; // if we get here, we must have connected successfully
  }

  if (p == NULL) {
    // looped off the end of the list with no successful bind
    LOG_E(GTPU,"failed to bind socket: %s %s \n",sourceIP,sourcePort);
    return false;
  }

  freeaddrinfo(servinfo); // all done with this structure

  if ((status = getaddrinfo(destIP, destPort, &hints, &servinfo)) != 0) {
    LOG_E(GTPU,"getaddrinfo error: %s\n", gai_strerror(status));
    return false;
  }

  if (servinfo) {
    result->destAddr=servinfo;
  } else {
    LOG_E(PHY,"No valid UDP addr: %s:%s\n",destIP, destPort);
    return false;
  }
laurent's avatar
laurent committed
82

laurent's avatar
laurent committed
83
  int enable=1;
laurent's avatar
laurent committed
84
  AssertFatal(setsockopt(result->sockHandler, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable))==0,"");
85
  struct timeval tv= {0,UDP_TIMEOUT};
86

87 88
  if (IS_SOFTMODEM_RFSIM)
    tv.tv_sec=2; //debug: wait 2 seconds for human understanding
89

laurent's avatar
laurent committed
90
  AssertFatal(setsockopt(result->sockHandler, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) ==0,"");
laurent's avatar
laurent committed
91 92 93
  // Make a send/recv buffer larger than a a couple of subframe
  // so the kernel will store for us in and out paquets
  int buff=1000*1000*10;
laurent's avatar
laurent committed
94 95 96
  AssertFatal ( setsockopt(result->sockHandler, SOL_SOCKET, SO_SNDBUF, &buff, sizeof(buff)) == 0, "");
  AssertFatal ( setsockopt(result->sockHandler, SOL_SOCKET, SO_RCVBUF, &buff, sizeof(buff)) == 0, "");
  return true;
laurent's avatar
laurent committed
97 98 99 100
}

// sock: udp socket
// bufferZone: a reception area of bufferSize
101
int receiveSubFrame(UDPsock_t *sock, void *bufferZone,  int bufferSize, uint16_t contentType) {
laurent's avatar
laurent committed
102
  int rcved=0;
laurent's avatar
laurent committed
103
  commonUDP_t *bufOrigin=(commonUDP_t *)bufferZone;
104 105 106 107 108 109 110 111 112 113
  static uint8_t crossData[65536];
  static int crossDataSize=0;

  if (crossDataSize) {
    LOG_D(HW,"copy a block received in previous subframe\n");
    memcpy(bufferZone, crossData, crossDataSize);
    rcved=1;
    bufferZone+=crossDataSize;
    crossDataSize=0;
  }
laurent's avatar
laurent committed
114

laurent's avatar
laurent committed
115 116
  do {
    //read all subframe data from the control unit
laurent's avatar
laurent committed
117
    int ret=recv(sock->sockHandler, bufferZone, bufferSize, 0);
laurent's avatar
laurent committed
118

laurent's avatar
laurent committed
119 120
    if ( ret==-1) {
      if ( errno == EWOULDBLOCK || errno== EINTR ) {
121 122
        LOG_I(HW,"Received: Timeout, subframe incomplete\n");
        return  rcved;
laurent's avatar
laurent committed
123
      } else {
laurent's avatar
laurent committed
124 125
        LOG_E(HW,"Critical issue in socket: %s\n", strerror(errno));
        return -1;
laurent's avatar
laurent committed
126 127
      }
    } else {
laurent's avatar
laurent committed
128 129 130
      if (hUDP(bufferZone)->contentType != contentType)
        abort();

131 132 133 134 135 136 137 138
      if (rcved && bufOrigin->timestamp != hUDP(bufferZone)->timestamp ) {
        if ( hUDP(bufferZone)->timestamp > bufOrigin->timestamp ) {
          LOG_W(HW,"Received data for TS: %lu before end of TS : %lu completion\n",
                hUDP(bufferZone)->timestamp,
                bufOrigin->timestamp);
          memcpy(crossData, bufferZone, ret );
          crossDataSize=ret;
          return rcved;
laurent's avatar
laurent committed
139
        } else {
140 141
          LOG_W(HW,"Dropping late packet\n");
          continue;
laurent's avatar
laurent committed
142
        }
laurent's avatar
laurent committed
143
      }
144 145 146

      rcved++;
      bufferZone+=ret;
laurent's avatar
laurent committed
147
    }
148

149 150
    LOG_D(HW,"Received: blocks: %d/%d, size %d, TS: %lu\n",
          rcved, bufOrigin->nbBlocks, ret, bufOrigin->timestamp);
151
  } while ( rcved == 0 || rcved < bufOrigin->nbBlocks );
laurent's avatar
laurent committed
152

laurent's avatar
laurent committed
153
  return rcved;
laurent's avatar
laurent committed
154 155
}

laurent's avatar
laurent committed
156
int sendSubFrame(UDPsock_t *sock, void *bufferZone, ssize_t secondHeaderSize, uint16_t contentType) {
laurent's avatar
laurent committed
157
  commonUDP_t *UDPheader=(commonUDP_t *)bufferZone ;
laurent's avatar
laurent committed
158
  UDPheader->contentType=contentType;
159
  UDPheader->senderClock=rdtsc();
laurent's avatar
laurent committed
160 161
  int nbBlocks=UDPheader->nbBlocks;
  int blockId=0;
laurent's avatar
laurent committed
162

laurent's avatar
laurent committed
163 164 165 166 167
  if (nbBlocks <= 0 ) {
    LOG_E(PHY,"FS6: can't send blocks: %d\n", nbBlocks);
    return 0;
  }

laurent's avatar
laurent committed
168
  do {
laurent's avatar
laurent committed
169 170
    if (blockId > 0 ) {
      commonUDP_t *currentHeader=(commonUDP_t *)bufferZone;
171 172
      currentHeader->timestamp=UDPheader->timestamp;
      currentHeader->nbBlocks=UDPheader->nbBlocks;
laurent's avatar
laurent committed
173
      currentHeader->blockID=blockId;
174
      currentHeader->contentType=UDPheader->contentType;
laurent's avatar
laurent committed
175 176 177
      memcpy(commonUDPdata((void *)currentHeader), commonUDPdata(bufferZone), secondHeaderSize);
    }

178
    blockId++;
laurent's avatar
laurent committed
179
    int sz=alignedSize(bufferZone);
laurent's avatar
laurent committed
180 181 182
    // Let's use the first address returned by getaddrinfo()
    int ret=sendto(sock->sockHandler, bufferZone, sz, 0,
                   sock->destAddr->ai_addr, sock->destAddr->ai_addrlen);
laurent's avatar
laurent committed
183

laurent's avatar
laurent committed
184
    if ( ret != sz )
laurent's avatar
laurent committed
185 186
      LOG_W(HW,"Wrote socket doesn't return size %d (val: %d, errno:%d, %s)\n",
            sz, ret, errno, strerror(errno));
laurent's avatar
laurent committed
187

188
    LOG_D(HW,"Sent: TS: %lu, blocks %d/%d, block size : %d \n",
189
          UDPheader->timestamp, UDPheader->nbBlocks-nbBlocks, UDPheader->nbBlocks, sz);
laurent's avatar
laurent committed
190 191 192
    bufferZone+=sz;
    nbBlocks--;
  } while (nbBlocks);
laurent's avatar
laurent committed
193

194
  return 0;
laurent's avatar
laurent committed
195
}