Commit 6a0e67f3 authored by Leonardo Lai's avatar Leonardo Lai

support INADDR_ANY and binding to specific IP

parent ceba7d84
Ping host: Ping host:
sudo ./pingpong -- -f ping sudo ./pingpong -c ../../config.ini -f ping
Pong host: Pong host:
sudo ./pingpong -- -f pong sudo ./pingpong -c ../../config.ini -f pong
...@@ -8,6 +8,7 @@ n_mem_channels=2 ...@@ -8,6 +8,7 @@ n_mem_channels=2
[port0] [port0]
mac_addr=68:05:ca:95:f8:ec mac_addr=68:05:ca:95:f8:ec
ip_addr=172.31.100.2
[port0_dst] [port0_dst]
mac_addr=68:05:ca:95:fa:64 mac_addr=68:05:ca:95:fa:64
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Created by leoll2 on 10/6/20. // Created by leoll2 on 10/6/20.
// Copyright (c) 2020 Leonardo Lai. All rights reserved. // Copyright (c) 2020 Leonardo Lai. All rights reserved.
// //
#include <arpa/inet.h> // for inet_addr
#include <rte_ether.h> #include <rte_ether.h>
...@@ -17,15 +18,19 @@ extern char *primary_argv[MAX_ARGC]; ...@@ -17,15 +18,19 @@ extern char *primary_argv[MAX_ARGC];
extern char *secondary_argv[MAX_ARGC]; extern char *secondary_argv[MAX_ARGC];
static char *progname; static char *progname;
static int parse_handler(void* configuration, const char* section, const char* name, const char* value) static int parse_handler(void* configuration, const char* section, const char* name, const char* value) {
{ #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("port0", "mac_addr")) { if (MATCH("port0", "mac_addr")) {
if (rte_ether_unformat_addr(value, &config.src_mac_addr) < 0) { if (rte_ether_unformat_addr(value, &config.src_mac_addr) < 0) {
fprintf(stderr, "Can't parse MAC address: %s\n", value); fprintf(stderr, "Can't parse MAC address: %s\n", value);
return 0; return 0;
} }
} else if (MATCH("port0_dst", "mac_addr")) { } else if (MATCH("port0", "ip_addr")) {
config.src_ip_addr.s_addr = inet_addr(value);
if (config.src_ip_addr.s_addr == (in_addr_t)(-1)) {
fprintf(stderr, "Can't parse IPv4 address: %s\n", value);
}
}else if (MATCH("port0_dst", "mac_addr")) {
if (rte_ether_unformat_addr(value, &config.dst_mac_addr) < 0) { if (rte_ether_unformat_addr(value, &config.dst_mac_addr) < 0) {
fprintf(stderr, "Can't parse MAC address: %s\n", value); fprintf(stderr, "Can't parse MAC address: %s\n", value);
return 0; return 0;
......
...@@ -125,15 +125,21 @@ int udpdk_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) ...@@ -125,15 +125,21 @@ int udpdk_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
return -1; return -1;
} }
// Mark the slot as bound // Mark the slot as bound, and store the corresponding IP and port
exch_zone_desc->slots[sockfd].bound = 1; exch_zone_desc->slots[sockfd].bound = 1;
exch_zone_desc->slots[sockfd].udp_port = (int)port; exch_zone_desc->slots[sockfd].udp_port = (int)port;
if (addr_in->sin_addr.s_addr == INADDR_ANY) {
// If INADDR_ANY, use the address from the configuration file
exch_zone_desc->slots[sockfd].ip_addr = config.src_ip_addr;
} else {
// If the address is explicitly set, bind to that
exch_zone_desc->slots[sockfd].ip_addr = addr_in->sin_addr;
}
// Insert in the hashtable (port, sock_id) // Insert in the hashtable (port, sock_id)
htable_insert(udp_port_table, (int)port, sockfd); htable_insert(udp_port_table, (int)port, sockfd);
RTE_LOG(INFO, SYSCALL, "Binding port %d to sock_id %d\n", port, sockfd); RTE_LOG(INFO, SYSCALL, "Binding port %d to sock_id %d\n", port, sockfd);
// TODO must bind the IP address too (if INADDR_ANY, pick one)
return 0; return 0;
} }
...@@ -195,8 +201,6 @@ ssize_t udpdk_sendto(int sockfd, const void *buf, size_t len, int flags, ...@@ -195,8 +201,6 @@ ssize_t udpdk_sendto(int sockfd, const void *buf, size_t len, int flags,
void *udp_data; void *udp_data;
const struct sockaddr_in *dest_addr_in = (struct sockaddr_in *)dest_addr; const struct sockaddr_in *dest_addr_in = (struct sockaddr_in *)dest_addr;
static uint32_t src_ip_addr = RTE_IPV4(2, 100, 31, 172); // TODO from bind (reversed for endianness)
// Validate the arguments // Validate the arguments
if (sendto_validate_args(sockfd, buf, len, flags, dest_addr, addrlen) < 0) { if (sendto_validate_args(sockfd, buf, len, flags, dest_addr, addrlen) < 0) {
return -1; return -1;
...@@ -238,7 +242,7 @@ ssize_t udpdk_sendto(int sockfd, const void *buf, size_t len, int flags, ...@@ -238,7 +242,7 @@ ssize_t udpdk_sendto(int sockfd, const void *buf, size_t len, int flags,
ip_hdr->time_to_live = IP_DEFTTL; ip_hdr->time_to_live = IP_DEFTTL;
ip_hdr->next_proto_id = IPPROTO_UDP; ip_hdr->next_proto_id = IPPROTO_UDP;
ip_hdr->packet_id = 0; ip_hdr->packet_id = 0;
ip_hdr->src_addr = src_ip_addr; // TODO this should be determined by bind() ip_hdr->src_addr = exch_zone_desc->slots[sockfd].ip_addr.s_addr;
ip_hdr->dst_addr = dest_addr_in->sin_addr.s_addr; ip_hdr->dst_addr = dest_addr_in->sin_addr.s_addr;
ip_hdr->total_length = rte_cpu_to_be_16(len + sizeof(*ip_hdr) + sizeof(*udp_hdr)); ip_hdr->total_length = rte_cpu_to_be_16(len + sizeof(*ip_hdr) + sizeof(*udp_hdr));
ip_hdr->hdr_checksum = rte_ipv4_cksum(ip_hdr); ip_hdr->hdr_checksum = rte_ipv4_cksum(ip_hdr);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <rte_memory.h> #include <rte_memory.h>
#include <rte_memzone.h> #include <rte_memzone.h>
#include <netinet/in.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
...@@ -29,6 +30,7 @@ struct exch_slot_info { ...@@ -29,6 +30,7 @@ struct exch_slot_info {
int bound; // used by a socket that did 'bind' int bound; // used by a socket that did 'bind'
int sockfd; // TODO redundant because it matches the slot index in this implementation int sockfd; // TODO redundant because it matches the slot index in this implementation
int udp_port; // UDP port associated to the socket (only if bound) int udp_port; // UDP port associated to the socket (only if bound)
struct in_addr ip_addr; // IPv4 address associated to the socket (only if bound)
} __rte_cache_aligned; } __rte_cache_aligned;
struct exch_zone_info { struct exch_zone_info {
...@@ -46,6 +48,7 @@ struct exch_slot { ...@@ -46,6 +48,7 @@ struct exch_slot {
typedef struct { typedef struct {
struct rte_ether_addr src_mac_addr; struct rte_ether_addr src_mac_addr;
struct rte_ether_addr dst_mac_addr; struct rte_ether_addr dst_mac_addr;
struct in_addr src_ip_addr;
char lcores_primary[MAX_ARG_LEN]; char lcores_primary[MAX_ARG_LEN];
char lcores_secondary[MAX_ARG_LEN]; char lcores_secondary[MAX_ARG_LEN];
int n_mem_channels; int n_mem_channels;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment