NFF-GO作为基于dpdk开发框架,业务层面可以使用GO语言开发;
主要应用有网关,安全等业务开发。
先看一个nff-go demo
在examples目录创建一个目录cc
新建四个文件:example.go,common.go,makefile, config.json
makefile实现:
# Copyright 2017 Intel Corporation.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
PATH_TO_MK = ../../mk
IMAGENAME = steps
EXECUTABLES = example
COMMON_FILES = common.go
include $(PATH_TO_MK)/leaf.mk
example.go实现,主要使用nff-go中flow.go的库
package main
import (
"github.com/intel-go/nff-go/flow"
)
func main() {
config := flow.Config{
CPUList: "0-3",
}
flow.SystemInit(&config)
InitCommonState()
flow.SystemStart()
}
common.go实现con
package main
import (
"encoding/json"
"flag"
"log"
"net"
"os"
"github.com/intel-go/nff-go/flow"
"github.com/intel-go/nff-go/packet"
"github.com/intel-go/nff-go/types"
)
var config map[string][]string
var dstMac0 [types.EtherAddrLen]uint8
var srcMac0 [types.EtherAddrLen]uint8
var dstMac1 [types.EtherAddrLen]uint8
var srcMac1 [types.EtherAddrLen]uint8
var modifyPacket = []func(pkt *packet.Packet, ctx flow.UserContext){modifyPacket0, modifyPacket1}
var direct = "direct"
const flowN = 3
// readConfig function reads and parses config file
func readConfig(fileName string) error {
file, err := os.Open(fileName)
if err != nil {
return err
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return err
}
return nil
}
func printMAC(prompt string, mac [types.EtherAddrLen]uint8) {
log.Printf("%s: %02x:%02x:%02x:%02x:%02x:%02x\n", prompt, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
}
func initCommonState() {
// Parse arguments
configFile := flag.String("config", "config.json", "Specify config file name")
target := flag.String("target", "direct", "Target host name from config file or \"direct\" reserved word")
flag.Parse()
// Get source MAC addresses for port 0 and 1
srcMac0 = flow.GetPortMACAddress(0)
printMAC("Source MAC 0", srcMac0)
srcMac1 = flow.GetPortMACAddress(1)
printMAC("Source MAC 1", srcMac1)
// Read config
err := readConfig(*configFile)
if err != nil {
log.Fatal(err)
}
// Get destination MAC addresses for port 0 and 1
if hw, err := net.ParseMAC(config[*target][0]); err == nil {
copy(dstMac0[:], hw)
} else {
log.Fatal(err)
}
printMAC("Destination MAC 0", dstMac0)
if hw, err := net.ParseMAC(config[*target][1]); err == nil {
copy(dstMac1[:], hw)
} else {
log.Fatal(err)
}
printMAC("Destination MAC 1", dstMac1)
}
func modifyPacket0(pkt *packet.Packet, ctx flow.UserContext) {
pkt.Ether.DAddr = dstMac0
pkt.Ether.SAddr = srcMac0
}
func modifyPacket1(pkt *packet.Packet, ctx flow.UserContext) {
pkt.Ether.DAddr = dstMac1
pkt.Ether.SAddr = srcMac1
}
config.json实现 ,为examle提供配置文件使用
{
"direct": [
"11:22:33:44:55:66",
"77:88:99:aa:bb:cc"
],
"dcomp01": [
"3c:fd:fe:9d:6a:c2",
"3c:fd:fe:9d:6a:c3"
],
"dcomp02": [
"3c:fd:fe:9d:67:ea",
"3c:fd:fe:9d:67:eb"
],
"dcomp03": [
"3c:fd:fe:9d:6a:ba",
"3c:fd:fe:9d:6a:bb"
],
"dcomp10": [
"3c:fd:fe:9d:69:2a",
"3c:fd:fe:9d:69:2b"
],
"dcomp11": [
"3c:fd:fe:9d:63:1a",
"3c:fd:fe:9d:63:1b"
],
"dcomp12": [
"3c:fd:fe:9d:6a:fa",
"3c:fd:fe:9d:6a:fb"
],
"dbdw04": [
"90:e2:ba:af:c5:70",
"90:e2:ba:af:c5:71"
],
"dbdw05": [
"90:e2:ba:af:b0:80",
"90:e2:ba:af:b0:81"
],
"dbdw06": [
"90:e2:ba:af:d7:f8",
"90:e2:ba:af:d7:f9"
],
"dbdw07": [
"90:e2:ba:c6:7a:74",
"90:e2:ba:c6:7a:75"
],
"dbdw08": [
"90:e2:ba:c6:7e:00",
"90:e2:ba:c6:7e:01"
],
"dbdw09": [
"90:e2:ba:af:d7:f0",
"90:e2:ba:af:d7:f1"
],
"dbdw10": [
"90:e2:ba:b1:e1:f4",
"90:e2:ba:b1:e1:f5"
],
"dbdw11": [
"90:e2:ba:b1:e2:00",
"90:e2:ba:b1:e2:01"
],
"dbdw12": [
"90:e2:ba:b1:dd:c8",
"90:e2:ba:b1:dd:c9"
],
"dbdw13": [
"90:e2:ba:b1:e1:d4",
"90:e2:ba:b1:e1:d5"
],
"dbdw14": [
"90:e2:ba:b1:e1:d8",
"90:e2:ba:b1:e1:d9"
],
"dbdw15": [
"90:e2:ba:b1:dd:bc",
"90:e2:ba:b1:dd:bd"
],
"dbdw16": [
"90:e2:ba:d3:70:dc",
"90:e2:ba:d3:70:dd"
],
"dbdw17": [
"90:e2:ba:b1:07:e8",
"90:e2:ba:b1:07:e9"
]
}