Skip to content

Commit

Permalink
Added KNI module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Scholz committed May 4, 2016
1 parent aac5303 commit 9542cf7
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SET(FILES
src/namespaces.cpp
src/ring.c
src/rate_limiter.cpp
src/kni.c
)

SET(DPDK_LIBS
Expand Down
68 changes: 68 additions & 0 deletions lua/include/kni.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local ffi = require "ffi"
local dpdkc = require "dpdkc"
local dpdk = require "dpdk"

local mod = {}

local mg_kni = {}
mod.mg_kni = mg_kni
mg_kni.__index = mg_kni

ffi.cdef[[
struct rte_kni;
struct rte_kni * mg_create_kni(uint8_t port_id, uint8_t core_id, void* mempool_ptr, const char name[]);
unsigned rte_kni_tx_burst ( struct rte_kni * kni,
struct rte_mbuf ** mbufs,
unsigned num
);
unsigned rte_kni_rx_burst ( struct rte_kni * kni,
struct rte_mbuf ** mbufs,
unsigned num
);
int rte_kni_handle_request ( struct rte_kni * kni );
unsigned mg_kni_tx_single(struct rte_kni * kni, struct rte_mbuf * mbuf);
void rte_kni_close ( void );
int rte_kni_release ( struct rte_kni * kni );
]]

function mod.createKNI(core, device, mempool, name)
--printf("kni C pointer print:")
printPtr(mempool)
core = core or 0
--printf("port id %d", device.id)
--printf("in KNI ptr memp %p", mempool)
--printPtr(mempool)
local kni = ffi.C.mg_create_kni(device.id, core, mempool, name)
--printPtr(mempool)
--printf("KNI should be nil = %p ,, mempool = %p", kni, mempool)
--if(kni == nil)then
-- printf("KNI == NIL !!!")
--else
-- printf("KNI not nil")
--end
return setmetatable({
kni = kni,
core = core,
device = device
}, mg_kni)
end

function mg_kni:rxBurst(bufs, nmax)
return ffi.C.rte_kni_rx_burst(self.kni, bufs.array, nmax)
end
function mg_kni:txSingle(mbuf)
ffi.C.mg_kni_tx_single(self.kni, mbuf)
end
function mg_kni:handleRequest()
ffi.C.rte_kni_handle_request(self.kni)
end

function mg_kni:release()
return ffi.C.rte_kni_release(self.kni)
end

function mod.close()
ffi.C.rte_kni_close()
end

return mod
13 changes: 11 additions & 2 deletions lua/include/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local bor, band, bnot, rshift, lshift, bswap = bit.bor, bit.band, bit.bnot, bit.
local write = io.write
local format = string.format
local random, log, floor = math.random, math.log, math.floor
local ffi = require "ffi"

--- Print a formatted string.
--- @todo docu
Expand All @@ -20,6 +21,16 @@ function printf(str, ...)
return print(str:format(...))
end

ffi.cdef[[
void print_ptr(void* ptr);
]]

--- Print a C pointer
-- @param ptr The pointer to print
function printPtr(ptr)
ffi.Cprint_ptr(ptr)
end

--- Print a formatted error string.
--- @todo docu
--- @param str
Expand Down Expand Up @@ -142,8 +153,6 @@ _G.bswap = bswap -- export bit.bswap to global namespace to be consistent with b
hton = bswap
ntoh = hton

local ffi = require "ffi"

ffi.cdef [[
typedef int clockid_t;
struct timeval {
Expand Down
72 changes: 72 additions & 0 deletions src/kni.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <stdio.h>
#include <string.h>
#include <rte_config.h>
#include <rte_common.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_kni.h>
#include <rte_pci.h>
#include <rte_ethdev.h>
#include <stdint.h>

/* Max size of a single packet */
#define MAX_PACKET_SZ 2048

static int kni_change_mtu(uint8_t port_id, unsigned new_mtu){
printf("change mtu\n");
return 0;
}
static int kni_config_network_interface(uint8_t port_id, uint8_t if_up){
printf("interface config\n");
return 0;
}

//struct rte_kni * mg_create_kni(uint8_t port_id, uint8_t core_id, struct rte_mempool* pktmbuf_pool){
struct rte_kni * mg_create_kni(uint8_t port_id, uint8_t core_id, void* mempool_ptr, const char name[]){
//printf("create kni\n");
//printf("mempool ptr 1 : %p\n", mempool_ptr);
struct rte_kni *kni;
struct rte_kni_conf conf;
/* Clear conf at first */
memset(&conf, 0, sizeof(conf));
snprintf(conf.name, RTE_KNI_NAMESIZE, name);
conf.core_id = core_id;
conf.force_bind = 1;
conf.group_id = (uint16_t)port_id;
conf.mbuf_size = MAX_PACKET_SZ;

struct rte_eth_dev_info dev_info;

memset(&dev_info, 0, sizeof(dev_info));
//printf("get dev info\n");
rte_eth_dev_info_get(port_id, &dev_info);
//printf("dev done\n");
//printf("pci dev: %p\n", dev_info.pci_dev);
conf.addr = dev_info.pci_dev->addr;
//printf("a\n");
conf.id = dev_info.pci_dev->id;
//printf("b\n");

struct rte_kni_ops ops;
//printf("c\n");
memset(&ops, 0, sizeof(ops));
//printf("d\n");
ops.port_id = port_id;
//printf("e\n");
ops.change_mtu = kni_change_mtu;
//printf("f\n");
ops.config_network_if = kni_config_network_interface;

//printf("alloc\n");
struct rte_mempool * pktmbuf_pool = (struct rte_mempool *)(mempool_ptr);
//printf("mempool pointer: %p\n", pktmbuf_pool);
kni = rte_kni_alloc(pktmbuf_pool, &conf, &ops);
//printf("done, kni = %p\n", kni);

//rte_eth_dev_start(port_id);
return kni;
}

unsigned mg_kni_tx_single(struct rte_kni * kni, struct rte_mbuf * mbuf){
return rte_kni_tx_burst(kni, &mbuf, 1);
}
4 changes: 4 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ uint64_t rte_rdtsc() {
return tsc.tsc_64;
}

void print_ptr(void* ptr) {
printf("ptr = %p\n", ptr);
}


static inline uint16_t get_ipv4_psd_sum (struct ipv4_hdr* ip_hdr) {
uint16_t len = ip_hdr->total_length;
Expand Down

0 comments on commit 9542cf7

Please sign in to comment.