Skip to content

Commit

Permalink
Working port add, daemonset
Browse files Browse the repository at this point in the history
  • Loading branch information
stano45 committed Sep 1, 2024
1 parent 1250acf commit 2c00a7e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 110 deletions.
82 changes: 34 additions & 48 deletions bmv2-cni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
Expand Down Expand Up @@ -166,61 +167,46 @@ func cmdDel(args *skel.CmdArgs) error {

func connectToBMv2Switch(ifName, thriftPort string) error {
logger.Printf("Connecting veth %s to BMv2 switch on thrift port %s", ifName, thriftPort)
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
randomPortNum := rng.Intn(65535) + 1

// Run show_ports command to get currently used port numbers
cmd := exec.Command("simple_switch_CLI", "--thrift-port", thriftPort)
cmd.Stdin = strings.NewReader("show_ports\n")
cmd.Stdin = strings.NewReader(fmt.Sprintf("port_add %s %d\n", ifName, randomPortNum))
output, err := cmd.CombinedOutput()
if err != nil {
logger.Printf("Error running show_ports: %v. Output: %s", err, output)
return fmt.Errorf("failed to run show_ports: %s", output)
}

// Parse the output to find the highest port number
nextPortNum, err := getNextPortNumber(string(output))
if err != nil {
logger.Printf("Error determining next port number: %v", err)
return err
}

// Use the next available port number
cmd = exec.Command("simple_switch_CLI", "--thrift-port", thriftPort)
cmd.Stdin = strings.NewReader(fmt.Sprintf("port_add %s %d\n", ifName, nextPortNum))
output, err = cmd.CombinedOutput()
if err != nil {
logger.Printf("Error adding port to BMv2 switch: %v. Output: %s", err, output)
return fmt.Errorf("failed to add port to BMv2 switch: %s", output)
}
logger.Printf("Port %d added to BMv2 switch successfully", nextPortNum)
logger.Printf("Port %d added to BMv2 switch successfully", randomPortNum)
return nil
}

// Helper function to parse the show_ports output and determine the next available port number
func getNextPortNumber(output string) (int, error) {
lines := strings.Split(output, "\n")
for i, line := range lines {
logger.Printf("Line %d: %s", i, line)
}
numLines := len(lines)

if numLines < 3 {
// If there are fewer than 2 lines, default to port 0
return 0, nil
}

// Get the second-to-last line
secondToLastLine := lines[numLines-3]
logger.Printf("Second-to-last line: %s", secondToLastLine)
fields := strings.Fields(secondToLastLine)

// Attempt to parse the first field as the port number
if len(fields) > 0 {
portNum, err := strconv.Atoi(fields[0])
if err == nil {
return portNum + 1, nil // Return the next available port number
}
}

// Default to port 0 if parsing fails
return 0, nil
}
// func getNextPortNumber(output string) (int, error) {
// lines := strings.Split(output, "\n")
// for i, line := range lines {
// logger.Printf("Line %d: %s", i, line)
// }
// numLines := len(lines)

// if numLines < 3 {
// // If there are fewer than 2 lines, default to port 0
// return 0, nil
// }

// // Get the second-to-last line
// secondToLastLine := lines[numLines-3]
// logger.Printf("Second-to-last line: %s", secondToLastLine)
// fields := strings.Fields(secondToLastLine)

// // Attempt to parse the first field as the port number
// if len(fields) > 0 {
// portNum, err := strconv.Atoi(fields[0])
// if err == nil {
// return portNum + 1, nil // Return the next available port number
// }
// }

// // Default to port 0 if parsing fails
// return 0, nil
// }
64 changes: 13 additions & 51 deletions examples/container_migration_in_kubernetes/Containerfile
Original file line number Diff line number Diff line change
@@ -1,58 +1,20 @@
ARG PARENT_VERSION=latest
FROM p4lang/pi:${PARENT_VERSION}
LABEL maintainer="P4 Developers <[email protected]>"
FROM debian:bullseye-slim

ARG CC=gcc
ARG CXX=g++
ARG GCOV=
ARG sswitch_grpc=yes

ENV BM_DEPS automake \
build-essential \
clang-8 \
clang-10 \
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
curl \
git \
lcov \
libgmp-dev \
libpcap-dev \
libboost-dev \
libboost-program-options-dev \
libboost-system-dev \
libboost-filesystem-dev \
libboost-thread-dev \
libtool \
pkg-config \
ca-certificates

ENV BM_RUNTIME_DEPS libboost-program-options1.71.0 \
libboost-system1.71.0 \
libboost-filesystem1.71.0 \
libboost-thread1.71.0 \
libgmp10 \
libpcap0.8 \
python3 \
python-is-python3

# Install dependencies
RUN apt-get update && \
apt-get install -y --no-install-recommends $BM_DEPS $BM_RUNTIME_DEPS

# Clone the repository
RUN git clone https://github.com/p4lang/behavioral-model.git /behavioral-model

# Set working directory
WORKDIR /behavioral-model/
gnupg2 \
&& rm -rf /var/lib/apt/lists/*

RUN ./autogen.sh
RUN echo "Acquire::ForceIPv4 \"true\";" > /etc/apt/apt.conf.d/99force-ipv4

RUN ./configure --with-thrift --with-pdfixed --with-pi
RUN echo 'deb http://download.opensuse.org/repositories/home:/p4lang/Debian_11/ /' > /etc/apt/sources.list.d/home:p4lang.list \
&& curl -fsSL https://download.opensuse.org/repositories/home:p4lang/Debian_11/Release.key | gpg --dearmor -o /etc/apt/trusted.gpg.d/home_p4lang.gpg

RUN make -j2
RUN apt-get update && apt-get install -y --no-install-recommends \
p4lang-bmv2 \
&& rm -rf /var/lib/apt/lists/*

RUN make install-strip && ldconfig
ENTRYPOINT ["simple_switch_grpc"]

RUN apt-get purge $BM_DEPS && \
apt-get autoremove -y --purge && \
rm -rf /behavioral-model /var/cache/apt/* /var/lib/apt/lists/* && \
echo 'Image done'
CMD ["--help"]
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ spec:
- key: "node-role.kubernetes.io/master"
effect: "NoSchedule"
containers:
- name: s1-container
image: docker.io/p4lang/p4c:1.2.4.13
- name: bmv2-switch
image: docker.io/skosorin/bmv2
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c"]
args:
- |
simple_switch_grpc \
--device-id 0 \
/load_balance.json \
--log-console \
--thrift-port 9090 \
-- \
--grpc-server-addr 0.0.0.0:50051
- "--device-id"
- "0"
- "/load_balance.json"
- "--log-console"
- "--thrift-port"
- "9090"
- "--"
- "--grpc-server-addr"
- "0.0.0.0:50051"
securityContext:
privileged: true
volumeMounts:
Expand Down
13 changes: 13 additions & 0 deletions examples/container_migration_in_kubernetes/rst.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sudo kubeadm reset -f
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --cri-socket=unix:///var/run/crio/crio.sock

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl taint nodes --all node-role.kubernetes.io/master-
kubectl taint nodes --all node-role.kubernetes.io/control-plane-

kubectl apply -f manifests/kube-router-daemonset.yaml

kubectl get pods --all-namespaces

0 comments on commit 2c00a7e

Please sign in to comment.