Exemplo n.º 1
0
static void init(struct client_state* cs, const char* interface,
                 const char* server, const char* port)
{
  cs->alarm = false;
  cs->alarm_usec = 20000;
  cs->msg_len = cfg_tx_size;
  cs->msg_buf = cs->pkt_buf + MAX_ETH_HEADERS + MAX_IP_TCP_HEADERS;

  TRY( zf_init() );
  struct zf_attr* attr;
  TRY( zf_attr_alloc(&attr) );
  TRY( zf_attr_set_str(attr, "interface", interface) );
  TRY( zf_attr_set_str(attr, "ctpio_mode", "ct") );
  TRY( zf_stack_alloc(attr, &(cs->stack)) );

  struct addrinfo hints, *ai;
  memset(&hints, 0, sizeof(hints));
  int rc = getaddrinfo(server, port, &hints, &ai);
  if( rc != 0 ) {
    fprintf(stderr, "ERROR: failed to lookup address '%s:%s': %s\n",
            server, port, gai_strerror(rc));
    exit(2);
  }

  /* Create TCP socket, connect to server, give it configuration. */
  struct zft_handle* tcp_handle;
  TRY( zft_alloc(cs->stack, attr, &tcp_handle) );
  TRY( zft_connect(tcp_handle, ai->ai_addr, ai->ai_addrlen,
                      &(cs->tcp_sock)) );
  freeaddrinfo(ai);
  while( zft_state(cs->tcp_sock) == TCP_SYN_SENT )
    zf_reactor_perform(cs->stack);
  TEST( zft_state(cs->tcp_sock) == TCP_ESTABLISHED );
  zock_put_int(cs->tcp_sock, cfg_tx_size);
  zock_put_int(cs->tcp_sock, cfg_rx_size);

  /* Create UDP socket, bind, join multicast group. */
  rc = getaddrinfo(cfg_mcast_addr, cfg_port, &hints, &ai);
  if( rc != 0 ) {
    fprintf(stderr, "ERROR: failed to lookup address '%s:%s': %s\n",
            cfg_mcast_addr, cfg_port, gai_strerror(rc));
    exit(2);
  }
  TRY( zfur_alloc(&cs->udp_sock, cs->stack, attr) );
  TRY( zfur_addr_bind(cs->udp_sock, ai->ai_addr, ai->ai_addrlen,
                         NULL, 0, 0) );
  freeaddrinfo(ai);

  ef_vi_init(cs, interface);
}
Exemplo n.º 2
0
int __ef_vi_alloc(ef_vi* vi, ef_driver_handle vi_dh,
		  efch_resource_id_t pd_or_vi_set_id,
		  ef_driver_handle pd_or_vi_set_dh,
		  int index_in_vi_set, int ifindex, int evq_capacity,
		  int rxq_capacity, int txq_capacity,
		  ef_vi* evq, ef_driver_handle evq_dh,
		  int vi_clustered, enum ef_vi_flags vi_flags)
{
  struct ef_vi_nic_type nic_type;
  ci_resource_alloc_t ra;
  char *mem_mmap_ptr_orig, *mem_mmap_ptr;
  char *io_mmap_ptr, *io_mmap_base;
  ef_vi_state* state;
  int rc;
  const char* s;
  uint32_t* ids;
  void* p;
  int q_label;

  EF_VI_BUG_ON((evq == NULL) != (evq_capacity != 0));
  EF_VI_BUG_ON(! evq_capacity && ! rxq_capacity && ! txq_capacity);

  /* Ensure ef_vi_free() only frees what we allocate. */
  io_mmap_ptr = NULL;
  io_mmap_base = NULL;
  mem_mmap_ptr = mem_mmap_ptr_orig = NULL;

  if( evq == NULL )
    q_label = 0;
  else if( (q_label = evq->vi_qs_n) == EF_VI_MAX_QS )
    return -EBUSY;

  if( ifindex < 0 && (s = getenv("EF_VI_IFINDEX")) )
    ifindex = atoi(s);
  if( evq_capacity == -1 )
    evq_capacity = (s = getenv("EF_VI_EVQ_SIZE")) ? atoi(s) : -1;
  if( txq_capacity == -1 )
    txq_capacity = (s = getenv("EF_VI_TXQ_SIZE")) ? atoi(s) : -1;
  if( rxq_capacity == -1 )
    rxq_capacity = (s = getenv("EF_VI_RXQ_SIZE")) ? atoi(s) : -1;
  if( evq_capacity == -1 && (vi_flags & EF_VI_RX_PACKED_STREAM) )
    /* At time of writing we're doing this at user-level as well as in
     * driver.  Utimately we want this default to be applied in the driver
     * so we don't have to know this magic number (which may change in
     * future).  For now we also apply it here so that the default will be
     * applied when running against a 201405-u1 driver.  This can be
     * removed once the driver ABI changes.
     */
    evq_capacity = 32768;

  /* Allocate resource and mmap. */
  memset(&ra, 0, sizeof(ra));
  ef_vi_set_intf_ver(ra.intf_ver, sizeof(ra.intf_ver));
  ra.ra_type = EFRM_RESOURCE_VI;
  ra.u.vi_in.ifindex = ifindex;
  ra.u.vi_in.pd_or_vi_set_fd = pd_or_vi_set_dh;
  ra.u.vi_in.pd_or_vi_set_rs_id = pd_or_vi_set_id;
  ra.u.vi_in.vi_set_instance = index_in_vi_set;
  ra.u.vi_in.ps_buf_size_kb = (vi_flags & EF_VI_RX_PS_BUF_SIZE_64K) ? 64 : 1024;
  if( evq != NULL ) {
    ra.u.vi_in.evq_fd = evq_dh;
    ra.u.vi_in.evq_rs_id = efch_make_resource_id(evq->vi_resource_id);
  }
  else {
    ra.u.vi_in.evq_fd = -1;
    evq = vi;
  }
  ra.u.vi_in.evq_capacity = evq_capacity;
  ra.u.vi_in.txq_capacity = txq_capacity;
  ra.u.vi_in.rxq_capacity = rxq_capacity;
  ra.u.vi_in.tx_q_tag = q_label;
  ra.u.vi_in.rx_q_tag = q_label;
  ra.u.vi_in.flags = vi_flags_to_efab_flags(vi_flags);
  rc = ci_resource_alloc(vi_dh, &ra);
  if( rc < 0 ) {
    LOGVV(ef_log("%s: ci_resource_alloc %d", __FUNCTION__, rc));
    goto fail1;
  }

  evq_capacity = ra.u.vi_out.evq_capacity;
  txq_capacity = ra.u.vi_out.txq_capacity;
  rxq_capacity = ra.u.vi_out.rxq_capacity;

  rc = -ENOMEM;
  state = malloc(ef_vi_calc_state_bytes(rxq_capacity, txq_capacity));
  if( state == NULL )
    goto fail1;

  if( ra.u.vi_out.io_mmap_bytes ) {
    rc = ci_resource_mmap(vi_dh, ra.out_id.index, EFCH_VI_MMAP_IO,
			  ra.u.vi_out.io_mmap_bytes, &p);
    if( rc < 0 ) {
      LOGVV(ef_log("%s: ci_resource_mmap (io) %d", __FUNCTION__, rc));
      goto fail2;
    }
    { /* On systems with large pages, multiple VI windows are mapped into
       * each system page.  Therefore the VI window may not appear at the
       * start of the I/O mapping.
       */
      int inst_in_iopage = 0;
      int vi_windows_per_page = CI_PAGE_SIZE / 8192;
      if( vi_windows_per_page > 1 )
        inst_in_iopage = ra.u.vi_out.instance & (vi_windows_per_page - 1);
      io_mmap_base = (char*) p;
      io_mmap_ptr = io_mmap_base + inst_in_iopage * 8192;
    }
  }

  if( ra.u.vi_out.mem_mmap_bytes ) {
    rc = ci_resource_mmap(vi_dh, ra.out_id.index, EFCH_VI_MMAP_MEM,
			  ra.u.vi_out.mem_mmap_bytes, &p);
    if( rc < 0 ) {
      LOGVV(ef_log("%s: ci_resource_mmap (mem) %d", __FUNCTION__, rc));
      goto fail3;
    }
    mem_mmap_ptr = mem_mmap_ptr_orig = (char*) p;
  }

  rc = ef_vi_arch_from_efhw_arch(ra.u.vi_out.nic_arch);
  EF_VI_BUG_ON(rc < 0);
  nic_type.arch = (unsigned char) rc;
  nic_type.variant = ra.u.vi_out.nic_variant;
  nic_type.revision = ra.u.vi_out.nic_revision;

  rc = check_nic_compatibility(vi_flags, nic_type.arch);
  if( rc != 0 )
    goto fail4;

  ids = (void*) (state + 1);

  ef_vi_init(vi, nic_type.arch, nic_type.variant, nic_type.revision,
	     vi_flags, state);
  ef_vi_init_out_flags(vi, (ra.u.vi_out.out_flags & EFHW_VI_CLOCK_SYNC_STATUS) ?
                       EF_VI_OUT_CLOCK_SYNC_STATUS : 0);
  ef_vi_init_io(vi, io_mmap_ptr);
  if( evq_capacity ) {
    ef_vi_init_evq(vi, evq_capacity, mem_mmap_ptr);
    mem_mmap_ptr += ((evq_capacity * sizeof(efhw_event_t) + CI_PAGE_SIZE - 1)
		     & CI_PAGE_MASK);
  }
  if( rxq_capacity ) {
    ef_vi_init_rxq(vi, rxq_capacity, mem_mmap_ptr, ids,
		   ra.u.vi_out.rx_prefix_len);
    mem_mmap_ptr += (ef_vi_rx_ring_bytes(vi) + CI_PAGE_SIZE-1) & CI_PAGE_MASK;
    ids += rxq_capacity;
    if( vi_flags & EF_VI_RX_TIMESTAMPS ) {
      int rx_ts_correction;
      rc = get_ts_correction(vi_dh, ra.out_id.index, &rx_ts_correction);
      if( rc < 0 )
        goto fail4;
      ef_vi_init_rx_timestamping(vi, rx_ts_correction);
    }
  }
  if( txq_capacity )
    ef_vi_init_txq(vi, txq_capacity, mem_mmap_ptr, ids);

  vi->vi_io_mmap_ptr = io_mmap_base;
  vi->vi_mem_mmap_ptr = mem_mmap_ptr_orig;
  vi->vi_io_mmap_bytes = ra.u.vi_out.io_mmap_bytes;
  vi->vi_mem_mmap_bytes = ra.u.vi_out.mem_mmap_bytes;
  vi->vi_resource_id = ra.out_id.index;
  if( ra.u.vi_out.out_flags & EFHW_VI_PS_BUF_SIZE_SET )
    vi->vi_ps_buf_size = ra.u.vi_out.ps_buf_size;
  else
    vi->vi_ps_buf_size = 1024 * 1024;
  BUG_ON(vi->vi_ps_buf_size != 64*1024 &&
         vi->vi_ps_buf_size != 1024*1024);
  vi->vi_clustered = vi_clustered;
  vi->vi_i = ra.u.vi_out.instance;
  ef_vi_init_state(vi);
  rc = ef_vi_add_queue(evq, vi);
  BUG_ON(rc != q_label);
  vi->vi_is_packed_stream = !! (vi_flags & EF_VI_RX_PACKED_STREAM);

  if( vi->vi_is_packed_stream )
    ef_vi_packed_stream_update_credit(vi);

  return q_label;

 fail4:
  if( mem_mmap_ptr != NULL )
    ci_resource_munmap(vi_dh, mem_mmap_ptr, ra.u.vi_out.mem_mmap_bytes);
 fail3:
  if( io_mmap_base != NULL )
    ci_resource_munmap(vi_dh, io_mmap_base, ra.u.vi_out.io_mmap_bytes);
 fail2:
  free(state);
 fail1:
  --evq->vi_qs_n;
  return rc;
}