Skip to content

Instantly share code, notes, and snippets.

@kisel
Created December 11, 2017 16:37
Show Gist options
  • Save kisel/4e4352da9117a36eabc331d89406cac4 to your computer and use it in GitHub Desktop.
Save kisel/4e4352da9117a36eabc331d89406cac4 to your computer and use it in GitHub Desktop.

ANS TCP STACK

echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

cd $DPDK modprobe uio insmod x86_64-native-linuxapp-gcc/kmod/igb_uio.ko

./usertools/dpdk-devbind.py --bind=igb_uio 00:08.0 ./usertools/dpdk-devbind.py --bind=igb_uio 00:09.0 ./usertools/dpdk-devbind.py -s

Network devices using DPDK-compatible driver

0000:00:08.0 '82540EM Gigabit Ethernet Controller 100e' drv=igb_uio unused=vfio-pci,uio_pci_generic 0000:00:09.0 '82540EM Gigabit Ethernet Controller 100e' drv=igb_uio unused=vfio-pci,uio_pci_generic

#./usertools/dpdk-setup.sh 512 non-numa HUGEPAGES

cd $ANS

./build/ans -c 0x2 -n 1 --base-virtaddr=0x2aaa2aa0000 -- -p 0x1 --config="(0,0,1)"

/*
Quick and dirty blocking http1.0 client
can be built with the same script as http_server
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>
#include <sys/times.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <netinet/in.h>
#include <termios.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifndef __linux__
#ifdef __FreeBSD__
#include <sys/socket.h>
#else
#include <net/socket.h>
#endif
#endif
#include <sys/time.h>
#include "anssock_intf.h"
#include "ans_errno.h"
#define BUFFER_SIZE 5000
#define BUFSIZE 5000
#define MAX_EVENTS 512
#define MAX_CPUS 8
#define read anssock_read
#define write anssock_write
#define socket anssock_socket
#define connect anssock_connect
#define epoll_create anssock_epoll_create
#define epoll_ctl anssock_epoll_ctl
#define error(x) printf(x)
char *http_req = "GET / HTTP/1.0\r\n"
"Connection: close\r\n"
"User-Agent: test-tcp-stack\r\n"
"Accept: */*\r\n"
"\r\n";
int waitfd(int sockfd) {
int epoll_fd;
int nfds;
epoll_fd = epoll_create(MAX_EVENTS);
if (epoll_fd == -1)
{
printf("epoll_create failed \n");
return 1;
}
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = sockfd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd, &ev) == -1)
{
printf("epll_ctl:server_sockfd register failed");
close(sockfd);
close(epoll_fd);
return 1;
}
nfds = anssock_epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (nfds == -1) {
printf("start epoll_wait failed");
close(sockfd);
close(epoll_fd);
return 1;
}
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serveraddr;
struct hostent *server;
char *hostname;
char buf[BUFSIZE];
int ret;
ret = anssock_init(NULL);
if (ret != 0)
printf("init sock failed \n");
int core =0;
/*initialize thread bind cpu*/
cpu_set_t cpus;
CPU_ZERO(&cpus);
CPU_SET((unsigned)core, &cpus);
sched_setaffinity(0, sizeof(cpus), &cpus);
/* check command line arguments */
if (argc != 3) {
fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
exit(0);
}
hostname = argv[1];
portno = atoi(argv[2]);
/* socket: create the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf("ERROR opening socket");
/* gethostbyname: get the server's DNS entry */
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host as %s\n", hostname);
exit(0);
}
/* build the server's Internet address */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
/* connect: create a connection with the server */
if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0)
error("ERROR connecting");
strcpy(buf, http_req);
/* send the message line to the server */
n = write(sockfd, buf, strlen(buf));
if (n < 0)
error("ERROR writing to socket");
/* print the server's reply */
bzero(buf, BUFSIZE);
waitfd(sockfd);
n = read(sockfd, buf, 100);
if (n < 0)
error("ERROR reading from socket");
printf("Echo from server: %s", buf);
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment