Skip to content

Commit

Permalink
Added nologic vale-bpf program
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaroHayakawa committed Aug 4, 2018
1 parent e381913 commit 72aedc1
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
7 changes: 7 additions & 0 deletions apps/nologic/GNUMakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BASE= ../..
include ${BASE}/Makefile.inc
include Makefile.common
clean: afterclean
afterclean:
for D in ${SUBDIR}; do (cd $$D && make clean); done
.PHONY: afterclean
9 changes: 9 additions & 0 deletions apps/nologic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BASE= ../..
.include "${BASE}/Makefile.inc"
.include "Makefile.common"
.if defined(SUBDIR) && !empty(SUBDIR)
clean: afterclean
afterclean:
for D in ${SUBDIR}; do (cd $$D && make clean); done
.PHONY: afterclean
.endif
23 changes: 23 additions & 0 deletions apps/nologic/Makefile.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
PROG= nologic_loader
SRCS= nologic_loader.c
OBJS= ${SRCS:.c=.o}
EBPFSRCS= nologic.bpf.c
EBPFOBJS= ${EBPFSRCS:.c=.o}
CFLAGS+= \
-O3 \
-I ${BASE}/sys \
-Wall
LIBS+= \
-lgbpf \
-lelf \

all: ${PROG} ${EBPFOBJS}

${EBPFOBJS}: ${EBPFSRCS}
clang $(CFLAGS) -target bpf -c nologic.bpf.c

${PROG}: ${OBJS}
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS)
CLEANFILES+= ${PROG} ${OBJS} ${EBPFOBJS}

install:
26 changes: 26 additions & 0 deletions apps/nologic/nologic.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*-
* SPDX-License-Identifier: Apache License 2.0
*
* Copyright 2017-2018 Yutaro Hayakawa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdint.h>
#include <net/vale_bpf_uapi.h>

uint32_t
no_logic(struct vale_bpf_md *md)
{
return 1;
}
133 changes: 133 additions & 0 deletions apps/nologic/nologic_loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*-
* SPDX-License-Identifier: Apache License 2.0
*
* Copyright 2017-2018 Yutaro Hayakawa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/ioctl.h>

#include <sys/ebpf.h>
#include <sys/ebpf_uapi.h>
#include <sys/ebpf_inst.h>
#include <sys/ebpf_dev.h>
#include <net/vale_bpf.h>
#include <gbpf/gbpf.h>

#include "../utils/vale_bpf_elf_loader.h"

static int end = 0;
static char *vale_name = NULL;

static void
on_sigint(int sig)
{
end = 1;
}

#define PROG_NAME "nologic.bpf.o"

static void
usage(void) {
printf("Usage: ./nologic_loader -v <vale_name>\n");
exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
int opt, nmfd, error;
size_t len;

while ((opt = getopt(argc, argv, "v:")) != -1) {
switch (opt) {
case 'v':
len = strlen(optarg);
vale_name = malloc(len + 2);
if (vale_name == NULL) {
perror("malloc");
usage();
}
strcpy(vale_name, optarg);
vale_name[len] = ':';
vale_name[len + 1] = '\0';
break;
default:
usage();
}
}

if (vale_name == NULL) {
usage();
}

nmfd = open("/dev/netmap", O_RDWR);
if (nmfd < 0) {
perror("open /dev/netmap");
exit(EXIT_FAILURE);
}

struct vale_bpf_info *info;
info = vale_bpf_load_elf_start("./" PROG_NAME);
if (!info) {
printf("Failed to load " PROG_NAME "\n");
exit(EXIT_FAILURE);
}

if (info->nprog == 0) {
printf("There is no program in " PROG_NAME "\n");
exit(EXIT_FAILURE);
}

int progfd = gbpf_load_prog((GBPFDriver *)info->driver,
EBPF_PROG_TYPE_TEST, info->progs[0].prog,
info->progs[0].prog_len);
if (progfd < 0) {
perror("gbpf_load_prog");
exit(EXIT_FAILURE);
}

error = vale_bpf_load_prog(nmfd, vale_name, progfd);
if (error) {
printf("Failed to load program to %s\n", vale_name);
exit(EXIT_FAILURE);
}

signal(SIGINT, on_sigint);

printf("Running nologic program on vale0, press Ctrl-C to finish\n");
while (!end) {
sleep(1);
}

error = vale_bpf_unload_prog(nmfd, vale_name);
if (error) {
printf("Failed to unload program from %s\n", vale_name);
exit(EXIT_FAILURE);
}

vale_bpf_load_elf_done(info);
close(nmfd);
free(vale_name);

return EXIT_SUCCESS;
}

0 comments on commit 72aedc1

Please sign in to comment.