Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit db2fd90

Browse files
authored
C library wrapper (#71)
* feat: Created the go C Wrapper files * Made changes to README instructions. --------- Signed-off-by: phansGithub <[email protected]>
1 parent 4be1142 commit db2fd90

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
99
github.com/google/gofuzz v1.1.0
1010
github.com/google/uuid v1.3.0
11+
github.com/intel/afxdp-plugins-for-kubernetes/pkg/goclient v0.0.0
1112
github.com/intel/afxdp-plugins-for-kubernetes/pkg/subfunctions v0.0.0
1213
github.com/pkg/errors v0.9.1
1314
github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1
@@ -22,3 +23,4 @@ require (
2223
)
2324

2425
replace github.com/intel/afxdp-plugins-for-kubernetes/pkg/subfunctions => ./pkg/subfunctions
26+
replace github.com/intel/afxdp-plugins-for-kubernetes/pkg/goclient => ./pkg/goclient

pkg/cclient/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Compiling the C Library
2+
3+
## Creating the shared library and header files:
4+
5+
```cmd
6+
go build -o lib_udsclient.so -buildmode=c-shared cclient.go
7+
```
8+
- this creates the header and shared library files from the cclient.go file.
9+
10+
## Creating the the static library and header files:
11+
12+
```cmd
13+
go build -o lib_udsclient.a -buildmode=c-archive ./cclient.go
14+
```
15+
- this creates the shared library and header files
16+
17+
## Usage
18+
19+
After creating the library and header files as described in the previous steps,
20+
this is how to use the functions generated by cgo
21+
22+
```c
23+
char* GetUdsClientVersion()
24+
```
25+
This returns the version of the handshake on your local machine/client.
26+
27+
```c
28+
char* GetUdsServerVersion()
29+
```
30+
This returns the version of the handshake on the server/host.
31+
32+
```c
33+
int RequestXskMapFd(char* device)
34+
```
35+
This requests an xskmap Fd for a specified device.
36+
37+
```c
38+
int RequestBusyPoll(int busyTimeout, int busyBudget, int fd)
39+
```
40+
This requests a busy poll for a specific device by using it's fd, and specifying a timeout and a budget.
41+
42+
```c
43+
void CleanUpConnection()
44+
```
45+
After all the other functions are called, this needs to be called after all the work you need to do is done,
46+
so that the connection to the UDS can be cleaned up.

pkg/cclient/cclient.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"C"
5+
"fmt"
6+
"os"
7+
8+
"github.com/intel/afxdp-plugins-for-kubernetes/internal/uds"
9+
"github.com/intel/afxdp-plugins-for-kubernetes/pkg/goclient"
10+
)
11+
12+
func main() {
13+
// Needed for cgo to generate the .h
14+
}
15+
16+
var cleaner uds.CleanupFunc
17+
18+
/*
19+
GetClientVersion is an exported version for c of the goclient GetClientVersion()
20+
*/
21+
//export GetUdsClientVersion
22+
func GetUdsClientVersion() *C.char {
23+
return C.CString(goclient.GetClientVersion())
24+
}
25+
26+
/*
27+
ServerVersion is an exported version for c of the goclient GetServerVersion()
28+
*/
29+
//export GetUdsServerVersion
30+
func GetUdsServerVersion() *C.char {
31+
response, function, err := goclient.GetServerVersion()
32+
if err != nil {
33+
fmt.Fprintln(os.Stderr, err.Error())
34+
function()
35+
return C.CString("-1")
36+
}
37+
38+
cleaner = function
39+
40+
return C.CString(response)
41+
}
42+
43+
/*
44+
GetXskMapFd is an exported version for c of the goclient XskMapFd()
45+
*/
46+
//export RequestXskMapFd
47+
func RequestXskMapFd(device *C.char) (fd C.int) {
48+
if device != nil {
49+
fdVal, function, err := goclient.RequestXSKmapFD(C.GoString(device))
50+
fd = C.int(fdVal)
51+
if err != nil {
52+
fmt.Fprintln(os.Stderr, err.Error())
53+
function()
54+
return -1
55+
}
56+
57+
cleaner = function
58+
return fd
59+
}
60+
61+
return -1
62+
}
63+
64+
/*
65+
RequestBusyPoll is an exported version for c of the goclient RequestBusyPoll()
66+
*/
67+
//export RequestBusyPoll
68+
func RequestBusyPoll(busyTimeout, busyBudget, fd C.int) C.int {
69+
timeout, budget, fdInt := int(busyTimeout), int(busyBudget), int(fd)
70+
if timeout > -1 && budget > -1 && fdInt > -1 {
71+
function, err := goclient.RequestBusyPoll(timeout, budget, fdInt)
72+
if err != nil {
73+
fmt.Fprintln(os.Stderr, err.Error())
74+
function()
75+
return -1
76+
}
77+
cleaner = function
78+
return 0
79+
}
80+
return -1
81+
}
82+
83+
/*
84+
CleanUpConnection an explicit exported cgo function to cleanup a connection after calling any of the other functions.
85+
Pass in one of the available function names to clean up the connection after use.
86+
*/
87+
//export CleanUpConnection
88+
func CleanUpConnection() {
89+
if cleaner == nil {
90+
fmt.Println("No cleanup function available to call")
91+
} else {
92+
cleaner()
93+
}
94+
}

0 commit comments

Comments
 (0)