Skip to content

Commit

Permalink
Merge pull request #63 from pengjianzhang/main
Browse files Browse the repository at this point in the history
bond boardcast arp and icmp6(NS ND)
  • Loading branch information
pengjianzhang committed Mar 25, 2022
2 parents 52d2bab + 95852f2 commit 5045c3b
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 26 deletions.
17 changes: 13 additions & 4 deletions src/arp.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "port.h"
#include "work_space.h"
#include "kni.h"
#include "bond.h"

static struct eth_addr g_mac_zero = {.bytes = {0, 0, 0, 0, 0, 0}};
static struct eth_addr g_mac_full = {.bytes = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
Expand All @@ -45,6 +46,16 @@ static inline void arp_set_arphdr(struct arphdr *arp, uint16_t op, uint32_t sip,
eth_addr_copy(&arp->ar_tha, da);
}

void arp_send(struct work_space *ws, struct rte_mbuf *m)
{
if (port_is_bond4(ws->port)) {
bond_broadcast(ws, m);
}

work_space_tx_send(ws, m);
net_stats_arp_tx();
}

static void arp_set_request(struct rte_mbuf *m, struct eth_addr *smac, uint32_t dip, uint32_t sip)
{
struct eth_hdr *eth = NULL;
Expand All @@ -68,8 +79,7 @@ static void arp_request_gw2(struct work_space *ws, uint32_t local_ip)
}

arp_set_request(m, &port->local_mac, port->gateway_ip.ip, local_ip);
work_space_tx_send(ws, m);
net_stats_arp_tx();
arp_send(ws, m);
}

void arp_request_gw(struct work_space *ws)
Expand Down Expand Up @@ -111,7 +121,7 @@ static void arp_reply(struct work_space *ws, struct rte_mbuf *m)
eth_addr_copy(&dmac, &eth->s_addr);
eth_hdr_set(eth, ETHER_TYPE_ARP, &dmac, smac);
arp_set_arphdr(arph, ARP_REPLY, sip, dip, smac, &dmac);
work_space_tx_send(ws, m);
arp_send(ws, m);
}

static void arp_process_reply(struct work_space *ws, struct rte_mbuf *m)
Expand Down Expand Up @@ -147,7 +157,6 @@ static void arp_process_request(struct work_space *ws, struct rte_mbuf *m)
}

arp_reply(ws, m);
net_stats_arp_tx();
}

void arp_process(struct work_space *ws, struct rte_mbuf *m)
Expand Down
1 change: 1 addition & 0 deletions src/arp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ struct arphdr {
struct work_space;
void arp_request_gw(struct work_space *ws);
void arp_process(struct work_space *ws, struct rte_mbuf *m);
void arp_send(struct work_space *ws, struct rte_mbuf *m);

#endif
33 changes: 27 additions & 6 deletions src/bond.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
#include "bond.h"

#include <unistd.h>
#include <stdbool.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_ether.h>
#include <rte_ethdev.h>

#include "port.h"
#include "mbuf.h"
#include "net_stats.h"
#include "work_space.h"

int bond_create(struct netif_port *port)
{
Expand Down Expand Up @@ -53,12 +57,6 @@ int bond_config_slaves(struct netif_port *port)
slave_id = port->port_id_list[i];
port_slave.id = slave_id;
port_config(&port_slave);

if (rte_eth_dev_start(slave_id) < 0) {
printf("startslave %d error\n", slave_id);
return -1;
}

if (rte_eth_bond_slave_add(port->id, slave_id) == -1) {
printf("add slave %d error\n", slave_id);
return -1;
Expand Down Expand Up @@ -87,3 +85,26 @@ int bond_wait(struct netif_port *port)

return -1;
}

void bond_broadcast(struct work_space *ws, struct rte_mbuf *m)
{
int i = 0;
uint16_t port_id = 0;
struct rte_mbuf *m2 = NULL;
struct netif_port *port = NULL;

port = ws->port;
for (i = 0; i < port->pci_num; i++) {
port_id = port->port_id_list[i];
m2 = work_space_alloc_mbuf(ws);
if (m2 == NULL) {
break;
}

mbuf_copy(m2, m);
net_stats_tx(m2);
if (rte_eth_tx_burst(port_id, ws->queue_id, &m2, 1) != 1) {
mbuf_free(m2);
}
}
}
3 changes: 3 additions & 0 deletions src/bond.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
#ifndef __BOND_H
#define __BOND_H

#include <stdbool.h>
#include <rte_eth_bond.h>

struct netif_port;
struct work_space;
int bond_create(struct netif_port *port);
int bond_config_slaves(struct netif_port *port);
int bond_wait(struct netif_port *port);
void bond_broadcast(struct work_space *ws, struct rte_mbuf *m);

#endif
19 changes: 14 additions & 5 deletions src/icmp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "mbuf.h"
#include "work_space.h"
#include "kni.h"
#include "bond.h"

static void icmp6_send(struct work_space *ws, struct rte_mbuf *m);

struct icmp6_nd_opt {
uint8_t type;
Expand Down Expand Up @@ -75,9 +78,7 @@ static void icmp6_ns_process(struct work_space *ws, struct rte_mbuf *m)

icmp6h->icmp6_cksum = 0;
icmp6h->icmp6_cksum = RTE_IPV6_UDPTCP_CKSUM(ip6h, icmp6h);

work_space_tx_send(ws, m);
net_stats_icmp_tx();
icmp6_send(ws, m);
}

static void icmp6_na_process(struct work_space *ws, struct rte_mbuf *m)
Expand Down Expand Up @@ -124,8 +125,7 @@ static void icmp6_echo_process(struct work_space *ws, struct rte_mbuf *m)
icmp6h->icmp6_cksum = 0;
icmp6h->icmp6_cksum = RTE_IPV6_UDPTCP_CKSUM(ip6h, icmp6h);

net_stats_icmp_tx();
work_space_tx_send(ws, m);
icmp6_send(ws, m);
}

void icmp6_process(struct work_space *ws, struct rte_mbuf *m)
Expand Down Expand Up @@ -243,6 +243,15 @@ void icmp6_ns_request(struct work_space *ws)
icmp6_ns_eth_hdr_push(ws->port, m);
icmp6_ns_ip6_hdr_push(ws->port, m);
icmp6_ns_hdr_push(ws->port, m);
icmp6_send(ws, m);
}

static void icmp6_send(struct work_space *ws, struct rte_mbuf *m)
{
if (icmp6_is_neigh(m) && port_is_bond4(ws->port)) {
bond_broadcast(ws, m);
}

net_stats_icmp_tx();
work_space_tx_send(ws, m);
}
16 changes: 16 additions & 0 deletions src/icmp6.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@
#ifndef __ICMP6_H_
#define __ICMP6_H_

#include <stdbool.h>
#include <rte_mbuf.h>
#include <netinet/icmp6.h>
#include "mbuf.h"

struct work_space;
void icmp6_process(struct work_space *ws, struct rte_mbuf *m);
void icmp6_ns_request(struct work_space *ws);

static inline bool icmp6_is_neigh(struct rte_mbuf *m)
{
uint8_t type = 0;
struct icmp6_hdr *icmp6h = NULL;

icmp6h = mbuf_icmp6_hdr(m);
type = icmp6h->icmp6_type;
if ((type == ND_NEIGHBOR_SOLICIT) || (type == ND_NEIGHBOR_ADVERT)) {
return true;
}

return false;
}

#endif
14 changes: 12 additions & 2 deletions src/kni.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "port.h"
#include "mbuf.h"
#include "work_space.h"
#include "bond.h"

static int kni_set_mtu(uint16_t port_id, struct rte_kni_conf *conf)
{
Expand Down Expand Up @@ -226,6 +227,16 @@ void kni_recv(struct work_space *ws, struct rte_mbuf *m)
mbuf_free2(m);
}

static void kni_send_mbuf(struct work_space *ws, struct rte_mbuf *m)
{
if (port_is_bond4(ws->port) && mbuf_is_neigh(m)) {
bond_broadcast(ws, m);
}

work_space_tx_send(ws, m);
net_stats_kni_tx();
}

void kni_send(struct work_space *ws)
{
int i = 0;
Expand All @@ -239,7 +250,6 @@ void kni_send(struct work_space *ws)
rte_kni_handle_request(kni);
num = rte_kni_rx_burst(kni, mbufs, NB_RXD);
for (i = 0; i < num; i++) {
work_space_tx_send(ws, mbufs[i]);
net_stats_kni_tx();
kni_send_mbuf(ws, mbufs[i]);
}
}
33 changes: 33 additions & 0 deletions src/mbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "port.h"
#include "work_space.h"
#include "icmp6.h"

#define NB_MBUF (8192 * 8)

Expand Down Expand Up @@ -113,3 +114,35 @@ void mbuf_log(struct rte_mbuf *m, const char *tag)
fprintf(log, "muf: %s -> %s type %x\n", smac, dmac, ntohs(eh->type));
}
}

void mbuf_copy(struct rte_mbuf *dst, struct rte_mbuf *src)
{
uint8_t *data = NULL;
uint8_t *data2 = NULL;
uint32_t len = 0;

data = (uint8_t *)mbuf_eth_hdr(src);
len = rte_pktmbuf_data_len(src);
data2 = mbuf_push_data(dst, len);
memcpy(data2, data, len);
}

bool mbuf_is_neigh(struct rte_mbuf *m)
{
uint8_t proto = 0;
struct eth_hdr *eth = NULL;
struct ip6_hdr *ip6h = NULL;

eth = mbuf_eth_hdr(m);
if (eth->type == htons(ETHER_TYPE_ARP)) {
return true;
} else if (eth->type == htons(ETHER_TYPE_IPv6)) {
ip6h = mbuf_ip6_hdr(m);
proto = ip6h->ip6_nxt;
if (proto == IPPROTO_ICMPV6) {
return icmp6_is_neigh(m);
}
}

return false;
}
3 changes: 3 additions & 0 deletions src/mbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,7 @@ static inline void mbuf_free2(struct rte_mbuf *m)
}
}

void mbuf_copy(struct rte_mbuf *dst, struct rte_mbuf *src);
bool mbuf_is_neigh(struct rte_mbuf *m);

#endif
13 changes: 7 additions & 6 deletions src/neigh.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int neigh_check_gateway(struct work_space *ws)
int i = 0;
int j = 0;

for (i = 0; i < DELAY_SEC * 1000; i++) {
for (i = 0; i < DELAY_SEC; i++) {
neigh_resolve_gateway_mac_address(ws);
for (j = 0; j < 1000; j++) {
if (ws->vxlan) {
Expand All @@ -73,12 +73,13 @@ int neigh_check_gateway(struct work_space *ws)
} else {
server_recv_mbuf(ws, ipv4_input, tcp_drop, udp_drop);
}
}

/* sleep 1ms */
usleep(1000);
if (neigh_gateway_is_enable(ws)) {
return 0;
work_space_tx_flush(ws);
if (neigh_gateway_is_enable(ws)) {
return 0;
}
/* sleep 1ms */
usleep(1000);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,17 @@ static int port_start(struct netif_port *port)
ret = rte_eth_dev_start(port_id);
if (ret == 0) {
RTE_ETH_MACADDR_GET(port_id, &port->local_mac);

if (port->bond) {
if (bond_wait(port) < 0) {
return -1;
}
}

rte_eth_allmulticast_enable(port_id);
rte_eth_promiscuous_enable(port_id);
rte_eth_stats_reset(port_id);

if (port->bond) {
return bond_wait(port);
}
return 0;
} else {
printf("port start error: %s\n", rte_strerror(rte_errno));
Expand Down
5 changes: 5 additions & 0 deletions src/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ void port_clear(uint16_t port_id, uint16_t queue_id);
int port_config(struct netif_port *port);
struct rte_mempool *port_get_mbuf_pool(struct netif_port *p, int queue_id);

static inline bool port_is_bond4(struct netif_port *port)
{
return (port->bond && (port->bond_mode == 4));
}

#endif

0 comments on commit 5045c3b

Please sign in to comment.