Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PI unique packet generation #1945

Merged
merged 5 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/server/packet_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (pi *PacketInjectorAPI) validateRequest(ppr *types.PacketInjection) error {
}

ipField := "IPV4"
if ppr.Type == "icmp6" || ppr.Type == "tcp6" || ppr.Type == "udp6" {
if ppr.Type == types.PITypeICMP6 || ppr.Type == types.PITypeTCP6 || ppr.Type == types.PITypeUDP6 {
ipField = "IPV6"
}

Expand Down
35 changes: 33 additions & 2 deletions api/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,26 @@ func (n *NodeRule) Validate() error {
return nil
}

const (
// PIModeUniqPerNode use a unique packet identifier per source node
PIModeUniqPerNode = "unique"
// PIModeRandom use random packet identifier for each packet
PIModeRandom = "random"

// PITypeICMP4 icmpv4 packet
PITypeICMP4 = "icmp4"
// PITypeICMP6 icmpv6 packet
PITypeICMP6 = "icmp6"
// PITypeTCP4 ipv4 + tcp packet
PITypeTCP4 = "tcp4"
// PITypeTCP6 ipv6 + tcp packet
PITypeTCP6 = "tcp6"
// PITypeUDP4 ipv4 + udp packet
PITypeUDP4 = "udp4"
// PITypeUDP6 ipv6 + udp packet
PITypeUDP6 = "udp6"
)

// PacketInjection packet injector API parameters
// easyjson:json
// swagger:model
Expand All @@ -256,7 +276,7 @@ type PacketInjection struct {
ICMPID uint16 `yaml:"ICMPID"`
Count uint64 `yaml:"Count"`
Interval uint64 `yaml:"Interval"`
Increment bool `yaml:"Increment"`
Mode string `yaml:"Mode"`
IncrementPayload int64 `yaml:"IncrementPayload"`
StartTime time.Time
Pcap []byte `yaml:"Pcap"`
Expand All @@ -270,10 +290,21 @@ func (pi *PacketInjection) GetName() string {

// Validate verifies the packet injection type is supported
func (pi *PacketInjection) Validate() error {
allowedTypes := map[string]bool{"icmp4": true, "icmp6": true, "tcp4": true, "tcp6": true, "udp4": true, "udp6": true}
allowedTypes := map[string]bool{
PITypeICMP4: true,
PITypeICMP6: true,
PITypeTCP4: true,
PITypeTCP6: true,
PITypeUDP4: true,
PITypeUDP6: true,
}
if _, ok := allowedTypes[pi.Type]; !ok {
return errors.New("given type is not supported")
}

if pi.Mode != "" && pi.Mode != PIModeUniqPerNode && pi.Mode != PIModeRandom {
return errors.New("given mode is not supported")
}
return nil
}

Expand Down
109 changes: 8 additions & 101 deletions api/types/types_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/client/packet_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var PacketInjectionCreate = &cobra.Command{
ICMPID: request.ICMPID,
Count: request.Count,
Interval: request.Interval,
Increment: request.Increment,
Mode: request.Mode,
IncrementPayload: request.IncrementPayload,
TTL: request.TTL,
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/injector/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ var (
id uint16
count uint64
interval uint64
increment bool
mode string
incrementPayload int64
ttl uint8
)

// AddInjectPacketInjectFlags add the command line flags for a packet injection
func AddInjectPacketInjectFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&srcIP, "srcIP", "", "", "source node IP")
cmd.Flags().StringVarP(&dstIP, "dstIP", "", "", "destination node IP")
cmd.Flags().StringVarP(&srcMAC, "srcMAC", "", "", "source node MAC")
cmd.Flags().StringVarP(&dstMAC, "dstMAC", "", "", "destination node MAC")
cmd.Flags().Uint16VarP(&srcPort, "srcPort", "", 0, "source port for TCP packet")
cmd.Flags().Uint16VarP(&dstPort, "dstPort", "", 0, "destination port for TCP packet")
cmd.Flags().StringVarP(&srcIP, "src-ip", "", "", "source node IP")
cmd.Flags().StringVarP(&dstIP, "dst-ip", "", "", "destination node IP")
cmd.Flags().StringVarP(&srcMAC, "src-mac", "", "", "source node MAC")
cmd.Flags().StringVarP(&dstMAC, "dst-mac", "", "", "destination node MAC")
cmd.Flags().Uint16VarP(&srcPort, "src-port", "", 0, "source port for TCP packet")
cmd.Flags().Uint16VarP(&dstPort, "dst-port", "", 0, "destination port for TCP packet")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is these changes needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really but make command line more consistent, this doesn't break the API

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

cmd.Flags().StringVarP(&packetType, "type", "", "icmp4", "packet type: icmp4, icmp6, tcp4, tcp6, udp4 and udp6")
cmd.Flags().StringVarP(&payload, "payload", "", "", "payload")
cmd.Flags().StringVar(&pcap, "pcap", "", "PCAP file")
cmd.Flags().Uint16VarP(&id, "id", "", 0, "ICMP identification")
cmd.Flags().BoolVarP(&increment, "increment", "", false, "increment ICMP id for each packet")
cmd.Flags().Int64VarP(&incrementPayload, "incrementPayload", "", 0, "increase payload for each packet")
cmd.Flags().StringVarP(&mode, "mode", "", "unique", "specify mode of packet generation, `unique` or `random`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to check if the specified value is either "unique" or "random"

cmd.Flags().Int64VarP(&incrementPayload, "inc-payload", "", 0, "increase payload size each packet")
cmd.Flags().Uint64VarP(&count, "count", "", 1, "number of packets to be generated")
cmd.Flags().Uint64VarP(&interval, "interval", "", 0, "wait interval milliseconds between sending each packet")
cmd.Flags().Uint8VarP(&ttl, "ttl", "", 64, "IP time-to-live header")
Expand Down Expand Up @@ -110,7 +110,7 @@ func GetPacketInjectRequest() (*pi.PacketInjectionRequest, error) {
Count: count,
ICMPID: id,
Interval: interval,
Increment: increment,
Mode: mode,
IncrementPayload: incrementPayload,
Payload: payload,
Pcap: pcapContent,
Expand Down Expand Up @@ -199,8 +199,8 @@ var InjectPacketCmd = &cobra.Command{
}

func init() {
InjectPacketCmd.Flags().StringVarP(&ifName, "ifName", "", "", "interface to inject packets into")
InjectPacketCmd.Flags().StringVarP(&encapType, "encapType", "", "ether", "encapsulation type")
InjectPacketCmd.Flags().StringVarP(&ifName, "if-name", "", "", "interface to inject packets into")
InjectPacketCmd.Flags().StringVarP(&encapType, "encap-type", "", "ether", "encapsulation type")
AddInjectPacketInjectFlags(InjectPacketCmd)
}

Expand Down
2 changes: 1 addition & 1 deletion contrib/python/api/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_install_requires():


setup(name='skydive-client',
version='0.5.0',
version='0.6.0',
description='Skydive Python client library',
url='http://github.com/skydive-project/skydive',
author='Sylvain Afchain',
Expand Down
10 changes: 5 additions & 5 deletions contrib/python/api/skydive/packet_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PacketInjection(object):
def __init__(self, uuid="", src="", dst="", srcip="", dstip="", srcmac="",
dstmac="", srcport=0, dstport=0, type="icmp4", payload="",
trackingid="", icmpid=0, count=1, interval=0,
increment=False, starttime="", ttl=64):
mode="unique", starttime="", ttl=64):
self.uuid = uuid
self.src = src
self.dst = dst
Expand All @@ -39,7 +39,7 @@ def __init__(self, uuid="", src="", dst="", srcip="", dstip="", srcmac="",
self.icmpid = icmpid
self.count = count
self.interval = interval
self.increment = increment
self.mode = mode
self.starttime = starttime
self.ttl = ttl

Expand Down Expand Up @@ -76,8 +76,8 @@ def repr_json(self):
obj["Count"] = self.count
if self.interval:
obj["Interval"] = self.interval
if self.increment:
obj["Increment"] = self.increment
if self.mode:
obj["Mode"] = self.mode
if self.ttl:
obj["TTL"] = self.ttl
return obj
Expand All @@ -99,5 +99,5 @@ def from_object(self, obj):
icmpid=obj.get("ICMPID"),
count=obj.get("Count"),
interval=obj.get("Interval"),
increment=obj.get("Increment"),
mode=obj.get("Mode"),
ttl=obj.get("TTL"))
22 changes: 20 additions & 2 deletions flow/ondemand/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,33 @@ func (h *onDemandFlowHandler) ResourceName() string {
return "Capture"
}

func (h *onDemandFlowHandler) GetNodes(resource types.Resource) []interface{} {
func (h *onDemandFlowHandler) GetNodeResources(resource types.Resource) []client.OnDemandNodeResource {
var nrs []client.OnDemandNodeResource

capture := resource.(*types.Capture)

query := capture.GremlinQuery
query += fmt.Sprintf(".Dedup().Has('Captures.ID', NEE('%s'))", resource.ID())
if capture.Type != "" && !common.CheckProbeCapabilities(capture.Type, common.MultipleOnSameNodeCapability) {
query += fmt.Sprintf(".Has('Captures.Type', NEE('%s'))", capture.Type)
}
query += h.nodeTypeQuery
return h.applyGremlinExpr(query)

if nodes := h.applyGremlinExpr(query); len(nodes) > 0 {
for _, i := range nodes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be done in applyGremlinExpr ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applyGremlinExpr is called somewhere else so I would prefer to keep this as it is

switch i.(type) {
case *graph.Node:
nrs = append(nrs, client.OnDemandNodeResource{Node: i.(*graph.Node), Resource: capture})
case []*graph.Node:
// case of shortestpath that returns a list of nodes
for _, node := range i.([]*graph.Node) {
nrs = append(nrs, client.OnDemandNodeResource{Node: node, Resource: capture})
}
}
}
}

return nrs
}

func (h *onDemandFlowHandler) applyGremlinExpr(query string) []interface{} {
Expand Down
Loading