Skip to content

Commit

Permalink
kni can only use interface IP
Browse files Browse the repository at this point in the history
  • Loading branch information
pengjianzhang committed May 21, 2022
1 parent 2147c07 commit fe6ec24
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 42 deletions.
4 changes: 1 addition & 3 deletions docs/configuration-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ TCP或者UDP协议或者HTTP协议。不论是TCP还是UDP协议,dperf客户
- mode: client, server

开启kni接口。我们给每个'port'创建一个kni接口。常见的接口名称是vEth/vnic等,默认名称是dperf。
kni接口的IP地址与路由需要手动配置,建议为kni接口分配独立的IP。
当只开启了1个CPU是,kni接口IP可以是流量IP;
如果使用了多个CPU,kni接口IP可以使用server的第一个流量IP。
kni接口的IP地址与路由需要手动配置, 只能配置为'port'的IP。

## tos
- syntax: tos Number(0x00-0xff or 0-255)
Expand Down
5 changes: 1 addition & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,7 @@ One 'vxlan' can be set per 'port'. 'innerSMAC' is the source MAC address of the
- mode: client, server

Enable the kni interface. We create a kni interface for each 'port'. Common interface Names are vEth/vnic, etc. The default name is dperf.
The IP address and routing of the kni interface need to be manually configured.
It is recommended to assign a separate IP to the kni interface.
When only one CPU is enabled, the kni interface IP can be the traffic IP;
If multiple CPUs are used, the kni interface IP can use the first traffic IP of the server.
The IP address and route of the kni interface need to be manually configured, and can only be configured as the 'port' IP.

## tos
- syntax: tos Number(0x00-0xff or 0-255)
Expand Down
12 changes: 5 additions & 7 deletions src/arp.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ static void arp_process_reply(struct work_space *ws, struct rte_mbuf *m)
work_space_update_gw(ws, &eth->s_addr);
}

if (ws->kni) {
return kni_recv(ws, m);
}

mbuf_free(m);
}

Expand All @@ -148,10 +144,8 @@ static void arp_process_request(struct work_space *ws, struct rte_mbuf *m)

arph = mbuf_arphdr(m);
dip = arph->ar_tip;

if (!work_space_ip_exist(ws, dip)) {
if (ws->kni) {
return kni_recv(ws, m);
}
mbuf_free(m);
return;
}
Expand All @@ -166,6 +160,10 @@ void arp_process(struct work_space *ws, struct rte_mbuf *m)
net_stats_arp_rx();
arph = mbuf_arphdr(m);

if (ws->kni) {
kni_broadcast(ws, m);
}

if (arph->ar_op == htons(ARP_REQUEST)) {
arp_process_request(ws, m);
} else if (arph->ar_op == htons(ARP_REPLY)) {
Expand Down
3 changes: 1 addition & 2 deletions src/bond.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ void bond_broadcast(struct work_space *ws, struct rte_mbuf *m)
port = ws->port;
for (i = 0; i < port->pci_num; i++) {
port_id = port->port_id_list[i];
m2 = work_space_alloc_mbuf(ws);
m2 = mbuf_dup(m);
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);
Expand Down
10 changes: 1 addition & 9 deletions src/icmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@ void icmp_process(struct work_space *ws, struct rte_mbuf *m)

net_stats_icmp_rx();
if (icmph->type != ICMP_ECHO) {
if (ws->kni) {
return kni_recv(ws, m);
}
mbuf_free(m);
return;
}

if (!work_space_ip_exist(ws, iph->daddr)) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
mbuf_free(m);
Expand Down
13 changes: 5 additions & 8 deletions src/icmp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ static void icmp6_ns_process(struct work_space *ws, struct rte_mbuf *m)
net_stats_icmp_rx();
if ((!work_space_ip6_exist(ws, (ipaddr_t *)&(ns->nd_ns_target)))
|| (ip6h->ip6_hops != ND_TTL)) {
if (ws->kni) {
return kni_recv(ws, m);
}
mbuf_free(m);
return;
}

kni_broadcast(ws, m);
eth_addr_copy(&eth->d_addr, &eth->s_addr);
eth_addr_copy(&eth->s_addr, smac);

Expand Down Expand Up @@ -107,9 +105,6 @@ static void icmp6_echo_process(struct work_space *ws, struct rte_mbuf *m)
const struct eth_addr *smac = &(ws->port->local_mac);

if (!work_space_ip6_exist(ws, (ipaddr_t *)&(ip6h->ip6_dst))) {
if (ws->kni) {
return kni_recv(ws, m);
}
mbuf_free(m);
return;
}
Expand Down Expand Up @@ -146,11 +141,13 @@ void icmp6_process(struct work_space *ws, struct rte_mbuf *m)
icmp6_ns_process(ws, m);
} else if (type == ND_NEIGHBOR_ADVERT) {
icmp6_na_process(ws, m);
} else {
if (ws->kni) {
} else if (type == ICMP6_ECHO_REPLY) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
mbuf_free(m);
} else {
mbuf_free(m);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/kni.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ void kni_recv(struct work_space *ws, struct rte_mbuf *m)
mbuf_free2(m);
}

void kni_broadcast(struct work_space *ws, struct rte_mbuf *m)
{
struct rte_mbuf *m2 = NULL;

m2 = mbuf_dup(m);
if (m2) {
kni_recv(ws, m);
}
}

static void kni_send_mbuf(struct work_space *ws, struct rte_mbuf *m)
{
if (port_is_bond4(ws->port) && mbuf_is_neigh(m)) {
Expand Down
1 change: 1 addition & 0 deletions src/kni.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int kni_start(struct config *cfg);
void kni_stop(struct config *cfg);
void kni_recv(struct work_space *ws, struct rte_mbuf *m);
void kni_send(struct work_space *ws);
void kni_broadcast(struct work_space *ws, struct rte_mbuf *m);
int kni_link_up(struct config *cfg);

#define KNI_NAME_DEFAULT "dperf"
Expand Down
14 changes: 13 additions & 1 deletion src/mbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void mbuf_print(struct rte_mbuf *m, const char *tag)
g_work_space->log = log;
}

void mbuf_copy(struct rte_mbuf *dst, struct rte_mbuf *src)
static inline void mbuf_copy(struct rte_mbuf *dst, struct rte_mbuf *src)
{
uint8_t *data = NULL;
uint8_t *data2 = NULL;
Expand All @@ -139,6 +139,18 @@ void mbuf_copy(struct rte_mbuf *dst, struct rte_mbuf *src)
memcpy(data2, data, len);
}

struct rte_mbuf *mbuf_dup(struct rte_mbuf *m)
{
struct rte_mbuf *m2 = NULL;
struct work_space *ws = g_work_space;
m2 = work_space_alloc_mbuf(ws);
if (m2 != NULL) {
mbuf_copy(m2, m);
}

return m2;
}

bool mbuf_is_neigh(struct rte_mbuf *m)
{
uint8_t proto = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/mbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,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);
struct rte_mbuf *mbuf_dup(struct rte_mbuf *m);

#endif
6 changes: 3 additions & 3 deletions src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ static inline void tcp_server_process(struct work_space *ws, struct rte_mbuf *m)

sk = socket_server_lookup(&ws->socket_table, iph, th);
if (unlikely(sk == NULL)) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
MBUF_LOG(m, "drop-no-socket");
Expand Down Expand Up @@ -780,7 +780,7 @@ static inline void tcp_client_process(struct work_space *ws, struct rte_mbuf *m)

sk = socket_client_lookup(&ws->socket_table, iph, th);
if (unlikely(sk == NULL)) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
MBUF_LOG(m, "drop-no-socket");
Expand Down Expand Up @@ -975,7 +975,7 @@ int tcp_init(struct work_space *ws)
void tcp_drop(__rte_unused struct work_space *ws, struct rte_mbuf *m)
{
if (m) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
net_stats_tcp_drop();
Expand Down
6 changes: 3 additions & 3 deletions src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static void udp_client_process(struct work_space *ws, struct rte_mbuf *m)

sk = socket_client_lookup(&ws->socket_table, iph, th);
if (unlikely(sk == NULL)) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
goto out;
Expand Down Expand Up @@ -212,7 +212,7 @@ static void udp_server_process(struct work_space *ws, struct rte_mbuf *m)

sk = socket_server_lookup(&ws->socket_table, iph, th);
if (unlikely(sk == NULL)) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
goto out;
Expand Down Expand Up @@ -332,7 +332,7 @@ int udp_init(struct work_space *ws)
void udp_drop(__rte_unused struct work_space *ws, struct rte_mbuf *m)
{
if (m) {
if (ws->kni) {
if (ws->kni && work_space_is_local_addr(ws, m)) {
return kni_recv(ws, m);
}
MBUF_LOG(m, "drop");
Expand Down
2 changes: 1 addition & 1 deletion src/work_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static void work_space_get_port(struct work_space *ws)
ws->vni = VXLAN_HTON(vxlan->vni);
}

if (ws->port->kni && (ws->queue_id == 0)) {
if (ws->port->kni) {
ws->kni = true;
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/work_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,24 @@ struct rte_mbuf *work_space_alloc_mbuf(struct work_space *ws);
void work_space_set_launch_interval(uint64_t launch_interval);
void work_space_wait_start(void);

/*
* <m> is a ip packet
* */
static inline bool work_space_is_local_addr(const struct work_space *ws, const struct rte_mbuf *m)
{
uint32_t daddr = 0;
const struct iphdr *iph = NULL;
const struct ip6_hdr *ip6h = NULL;

iph = (const struct iphdr *)mbuf_ip_hdr(m);
ip6h = (const struct ip6_hdr *)iph;
if (iph->version == 4) {
daddr = iph->daddr;
} else {
daddr = ip6h->ip6_dst.s6_addr32[3];
}

return (daddr == ws->port->local_ip.ip);
}

#endif

0 comments on commit fe6ec24

Please sign in to comment.