packet mmap send packet format

Posted by SeregASM on Stack Overflow See other posts from Stack Overflow or by SeregASM
Published on 2011-11-29T01:47:58Z Indexed on 2011/11/29 1:49 UTC
Read the original article Hit count: 351

I want to improve packet transmitting performance. Before that I used raw sockets and now I study packet_mmap. I have packets(frames) which I already captured from kernel module from another PC, put to current PC and now I want to retransmit them to local interface with following forwarding. I have got example of packet_mmap, integrated it to my project, but I send

fd_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
memset(&my_addr, 0, sizeof(struct sockaddr_ll));
my_addr.sll_family = PF_PACKET;
my_addr.sll_protocol = htons(ETH_P_ALL);
strcpy(str_devname, "eth0");
strncpy(s_ifr.ifr_name, str_devname, sizeof(s_ifr.ifr_name));
ec = ioctl(fd_socket, SIOCGIFINDEX, &s_ifr);
i_ifindex = s_ifr.ifr_ifindex;
memset(&my_addr, 0, sizeof(struct sockaddr_ll));
my_addr.sll_family = AF_PACKET;
my_addr.sll_protocol = ETH_P_ALL;
my_addr.sll_ifindex = i_ifindex;
bind(fd_socket, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_ll)
s_packet_req.tp_block_size = c_buffer_sz;
s_packet_req.tp_frame_size = c_buffer_sz;
s_packet_req.tp_block_nr = c_buffer_nb;
s_packet_req.tp_frame_nr = c_buffer_nb;
size = s_packet_req.tp_block_size * s_packet_req.tp_block_nr;
if (setsockopt(fd_socket, SOL_PACKET, PACKET_TX_RING, (char *) &s_packet_req, sizeof(s_packet_req)) < 0) {
 perror("setsockopt: PACKET_TX_RING");
 return;
}

if (c_sndbuf_sz) {
 printf("send buff size = %d\n", c_sndbuf_sz);
 if (setsockopt(fd_socket, SOL_SOCKET, SO_SNDBUF, &c_sndbuf_sz, sizeof(c_sndbuf_sz)) < 0){
  perror("getsockopt: SO_SNDBUF");
  exit(1);
 }
}

data_offset = TPACKET_HDRLEN - sizeof(struct sockaddr_ll);
printf("data offset = %d bytes\n", data_offset);
ps_header_start = (tpacket_hdr *) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_socket, 0);
if (ps_header_start == (void*) -1) {
 perror("mmap");
 exit(1);
}

Then I fill data

ps_header = ((struct tpacket_hdr *) ((char *) ps_header_start + (c_buffer_sz * i_index)));
if (!ps_header) {
 perror("ps_header") ;
 return NULL;
}
data = ((char*) ps_header) + data_offset;
switch ((volatile uint32_t) ps_header->tp_status) {
  case TP_STATUS_AVAILABLE:
      printf("TP_STATUS_AVAILABLE, index=%d\n",i_index) ;
      memcpy(data, packet_data, size);

pthread_mutex_lock(&index_locker) ;
i_index++;
pthread_mutex_unlock(&index_locker) ;

if (i_index >= c_buffer_nb) {
    i_index = 0;
    first_loop = 0;
}

/* update packet len */
ps_header->tp_len = size;
/* set header flag to USER (trigs xmit)*/
ps_header->tp_status = TP_STATUS_SEND_REQUEST;

then I send

ec_send = sendto(fd_socket, NULL, 0, 0, (struct sockaddr *) ps_sockaddr, sizeof(struct sockaddr_ll));

I have got no errors, ec_send=not null size of sended data. But there are no data routed to destination host.

So, I ask - what data I should pass to ring buffer, now I include headers ip,tcp, should I include MAC header? - May be I have to set additional flags to route my packets.

© Stack Overflow or respective owner

Related posts about linux

Related posts about sockets