-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #482 from ESWZY/traffic-mgr/base-map
Implement kernel abstraction of Service and Pod, and use maps for storage and information transfer
- Loading branch information
Showing
10 changed files
with
1,565 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
// Copyright 2023 The LMP Authors. | ||
// | ||
// 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 | ||
// | ||
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE | ||
// | ||
// 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. | ||
// | ||
// author: Woa <[email protected]> | ||
|
||
package bpf | ||
|
||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang -target bpf -cflags "-D__TARGET_ARCH_x86" bpf_connect connect.c -- -I./headers | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cilium/cilium/pkg/loadbalancer" | ||
"github.com/cilium/cilium/pkg/u8proto" | ||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/link" | ||
"github.com/cilium/ebpf/rlimit" | ||
|
@@ -99,6 +118,18 @@ func (p *Programs) Close() { | |
p.connectCgroup.Close() | ||
} | ||
|
||
if p.connectObj.bpf_connectMaps.LB4SERVICES_MAP_V2 != nil { | ||
p.connectObj.bpf_connectMaps.LB4SERVICES_MAP_V2.Unpin() | ||
p.connectObj.bpf_connectMaps.LB4SERVICES_MAP_V2.Close() | ||
fmt.Println("Unpin and close") | ||
} | ||
|
||
if p.connectObj.bpf_connectMaps.LB4BACKEND_MAP_V2 != nil { | ||
p.connectObj.bpf_connectMaps.LB4BACKEND_MAP_V2.Unpin() | ||
p.connectObj.bpf_connectMaps.LB4BACKEND_MAP_V2.Close() | ||
fmt.Println("Unpin and close") | ||
} | ||
|
||
_ = os.Remove(MapsPinPath) | ||
} | ||
|
||
|
@@ -109,13 +140,59 @@ func Sample() { | |
return | ||
} | ||
|
||
// set service | ||
serviceIP := "1.1.1.1" | ||
servicePort := 80 | ||
backendNumber := 2 | ||
svcKey := NewService4Key(net.ParseIP(serviceIP), uint16(servicePort), u8proto.ANY, 0, 0) | ||
// use index 0 to indicate service item | ||
svcValue := NewService4Value(Backend4Key{0}, uint16(backendNumber)) | ||
err = progs.connectObj.bpf_connectMaps.LB4SERVICES_MAP_V2.Update(svcKey.ToNetwork(), svcValue.ToNetwork(), ebpf.UpdateAny) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
podIp1 := "1.1.1.1" | ||
backendPort1 := 80 | ||
backendID1 := 0 | ||
slotIndex1 := 1 | ||
backendKey1 := Backend4Key{uint32(backendID1)} | ||
backendServiceKey := NewService4Key(net.ParseIP(serviceIP), uint16(servicePort), u8proto.ANY, 0, uint16(slotIndex1)) | ||
backendServiceValue := NewService4Value(backendKey1, 0) | ||
err = progs.connectObj.bpf_connectMaps.LB4SERVICES_MAP_V2.Update(backendServiceKey.ToNetwork(), backendServiceValue.ToNetwork(), ebpf.UpdateAny) | ||
if err != nil { | ||
panic(err) | ||
} | ||
backendValue, _ := NewBackend4Value(net.ParseIP(podIp1), uint16(backendPort1), u8proto.ANY, loadbalancer.BackendStateActive) | ||
err = progs.connectObj.bpf_connectMaps.LB4BACKEND_MAP_V2.Update(backendKey1, backendValue.ToNetwork(), ebpf.UpdateAny) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// python3 -m http.server 8888 | ||
podIp2 := "127.0.0.1" | ||
backendPort2 := 8888 | ||
backendID2 := 1 | ||
slotIndex2 := 2 | ||
backendKey2 := Backend4Key{uint32(backendID2)} | ||
backendServiceKey2 := NewService4Key(net.ParseIP(serviceIP), uint16(servicePort), u8proto.ANY, 0, uint16(slotIndex2)) | ||
backendServiceValue2 := NewService4Value(backendKey2, 0) | ||
err = progs.connectObj.bpf_connectMaps.LB4SERVICES_MAP_V2.Update(backendServiceKey2.ToNetwork(), backendServiceValue2.ToNetwork(), ebpf.UpdateAny) | ||
if err != nil { | ||
panic(err) | ||
} | ||
backendValue2, _ := NewBackend4Value(net.ParseIP(podIp2), uint16(backendPort2), u8proto.ANY, loadbalancer.BackendStateActive) | ||
err = progs.connectObj.bpf_connectMaps.LB4BACKEND_MAP_V2.Update(backendKey2, backendValue2.ToNetwork(), ebpf.UpdateAny) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = progs.Attach() | ||
if err != nil { | ||
fmt.Println("[ERROR] Attaching failed:", err) | ||
} | ||
defer progs.Close() | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c) | ||
<-c | ||
time.Sleep(time.Minute) | ||
fmt.Println("[INFO] Time is up...") | ||
} |
Oops, something went wrong.