Commit fad778d0 authored by kesnar's avatar kesnar
Browse files

wip: inband telemetry

parent 71ba4e3d
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
import sys
import struct

from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import PacketListField, ShortField, IntField, LongField, BitField, FieldListField, FieldLenField
from scapy.all import IP, UDP, Raw
from scapy.layers.inet import _IPOption_HDR


def get_if():
    ifs=get_if_list()
    iface=None
    for i in get_if_list():
        if "eth0" in i:
            iface=i
            break
    if not iface:
        print("Cannot find eth0 interface")
        exit(1)
    return iface


class SwitchTrace(Packet):
    fields_desc = [ BitField("swid", 0, 13),
                    BitField("qdepth", 0,13),
                    BitField("portid",0,6)]
    def extract_padding(self, p):
                return "", p


class IPOption_INT(IPOption):
    name = "INT"
    option = 31
    fields_desc = [ _IPOption_HDR,
                    FieldLenField("length", None, fmt="B",
                                  length_of="int_headers",
                                  adjust=lambda pkt,l:l*2+4),
                    ShortField("count", 0),
                    PacketListField("int_headers",
                                   [],
                                   SwitchTrace,
                                   count_from=lambda pkt:(pkt.count*1)) ]


def handle_pkt(pkt):
    print("got a packet")
    pkt.show2()
    sys.stdout.flush()


def main():
    iface = 'server-eth0'
    print("sniffing on %s" % iface)
    sys.stdout.flush()
    sniff(filter="udp and port 4321", iface = iface,
          prn = lambda x: handle_pkt(x))


if __name__ == '__main__':
    main()
+77 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

import argparse
import sys
import socket
import random
import struct

from scapy.all import sendp, send, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import Ether, IP, UDP
from scapy.all import IntField, FieldListField, FieldLenField, ShortField, PacketListField, BitField
from scapy.layers.inet import _IPOption_HDR

from time import sleep


def get_if():
    ifs=get_if_list()
    iface=None
    for i in get_if_list():
        if "eth0" in i:
            iface=i
            break
    if not iface:
        print("Cannot find eth0 interface")
        exit(1)
    return iface


class SwitchTrace(Packet):
    fields_desc = [ BitField("swid", 0, 13),
                    BitField("qdepth", 0,13),
                    BitField("portid",0,6)]
    def extract_padding(self, p):
                return "", p

class IPOption_INT(IPOption):
    name = "INT"
    option = 31
    fields_desc = [ _IPOption_HDR,
                    FieldLenField("length", None, fmt="B",
                                  length_of="int_headers",
                                  adjust=lambda pkt,l:l*2+4),
                    ShortField("count", 0),
                    PacketListField("int_headers",
                                   [],
                                   SwitchTrace,
                                   count_from=lambda pkt:(pkt.count*1)) ]


def main():

    if len(sys.argv)<4:
        print('pass 3 arguments: <destination> "<message>" <number of packets>')
        exit(1)

    addr = socket.gethostbyname(sys.argv[1])
    iface = get_if()

    pkt = Ether(src=get_if_hwaddr(iface), dst="ff:ff:ff:ff:ff:ff") / IP(
        dst=addr, options = IPOption_INT(count=0,
            int_headers=[])) / UDP(
            dport=1234, sport=4321) / sys.argv[2]

    pkt.show2()

    try:
      for i in range(int(sys.argv[3])):
        sendp(pkt, iface=iface)
        sleep(1)
    except KeyboardInterrupt:
        raise


if __name__ == '__main__':
    main()
+381 −0
Original line number Diff line number Diff line
{
  "header_types" : [
    {
      "name" : "scalars_0",
      "id" : 0,
      "fields" : [
        ["local_metadata_t.is_multicast", 1, false],
        ["_padding_0", 7, false]
      ]
    },
    {
      "name" : "standard_metadata",
      "id" : 1,
      "fields" : [
        ["ingress_port", 9, false],
        ["egress_spec", 9, false],
        ["egress_port", 9, false],
        ["clone_spec", 32, false],
        ["instance_type", 32, false],
        ["drop", 1, false],
        ["recirculate_port", 16, false],
        ["packet_length", 32, false],
        ["enq_timestamp", 32, false],
        ["enq_qdepth", 19, false],
        ["deq_timedelta", 32, false],
        ["deq_qdepth", 19, false],
        ["ingress_global_timestamp", 48, false],
        ["egress_global_timestamp", 48, false],
        ["lf_field_list", 32, false],
        ["mcast_grp", 16, false],
        ["resubmit_flag", 32, false],
        ["egress_rid", 16, false],
        ["recirculate_flag", 32, false],
        ["checksum_error", 1, false],
        ["parser_error", 32, false],
        ["priority", 3, false],
        ["_padding", 2, false]
      ]
    },
    {
      "name" : "ethernet_t",
      "id" : 2,
      "fields" : [
        ["dst_addr", 48, false],
        ["src_addr", 48, false],
        ["ether_type", 16, false]
      ]
    }
  ],
  "headers" : [
    {
      "name" : "scalars",
      "id" : 0,
      "header_type" : "scalars_0",
      "metadata" : true,
      "pi_omit" : true
    },
    {
      "name" : "standard_metadata",
      "id" : 1,
      "header_type" : "standard_metadata",
      "metadata" : true,
      "pi_omit" : true
    },
    {
      "name" : "ethernet",
      "id" : 2,
      "header_type" : "ethernet_t",
      "metadata" : false,
      "pi_omit" : true
    }
  ],
  "header_stacks" : [],
  "header_union_types" : [],
  "header_unions" : [],
  "header_union_stacks" : [],
  "field_lists" : [],
  "errors" : [
    ["NoError", 1],
    ["PacketTooShort", 2],
    ["NoMatch", 3],
    ["StackOutOfBounds", 4],
    ["HeaderTooShort", 5],
    ["ParserTimeout", 6],
    ["ParserInvalidArgument", 7]
  ],
  "enums" : [],
  "parsers" : [
    {
      "name" : "parser",
      "id" : 0,
      "init_state" : "start",
      "parse_states" : [
        {
          "name" : "start",
          "id" : 0,
          "parser_ops" : [
            {
              "parameters" : [
                {
                  "type" : "regular",
                  "value" : "ethernet"
                }
              ],
              "op" : "extract"
            }
          ],
          "transitions" : [
            {
              "value" : "default",
              "mask" : null,
              "next_state" : null
            }
          ],
          "transition_key" : []
        }
      ]
    }
  ],
  "parse_vsets" : [],
  "deparsers" : [
    {
      "name" : "deparser",
      "id" : 0,
      "source_info" : {
        "filename" : "p4src/main.p4",
        "line" : 130,
        "column" : 8,
        "source_fragment" : "DeparserImpl"
      },
      "order" : ["ethernet"]
    }
  ],
  "meter_arrays" : [],
  "counter_arrays" : [],
  "register_arrays" : [],
  "calculations" : [],
  "learn_lists" : [],
  "actions" : [
    {
      "name" : "IngressPipeImpl.drop",
      "id" : 0,
      "runtime_data" : [],
      "primitives" : [
        {
          "op" : "mark_to_drop",
          "parameters" : [
            {
              "type" : "header",
              "value" : "standard_metadata"
            }
          ],
          "source_info" : {
            "filename" : "p4src/main.p4",
            "line" : 77,
            "column" : 8,
            "source_fragment" : "mark_to_drop(standard_metadata)"
          }
        }
      ]
    },
    {
      "name" : "IngressPipeImpl.set_egress_port",
      "id" : 1,
      "runtime_data" : [
        {
          "name" : "port",
          "bitwidth" : 9
        }
      ],
      "primitives" : [
        {
          "op" : "assign",
          "parameters" : [
            {
              "type" : "field",
              "value" : ["standard_metadata", "egress_spec"]
            },
            {
              "type" : "runtime_data",
              "value" : 0
            }
          ],
          "source_info" : {
            "filename" : "p4src/main.p4",
            "line" : 81,
            "column" : 8,
            "source_fragment" : "standard_metadata.egress_spec = port"
          }
        }
      ]
    },
    {
      "name" : "IngressPipeImpl.set_multicast_group",
      "id" : 2,
      "runtime_data" : [
        {
          "name" : "gid",
          "bitwidth" : 16
        }
      ],
      "primitives" : [
        {
          "op" : "assign",
          "parameters" : [
            {
              "type" : "field",
              "value" : ["standard_metadata", "mcast_grp"]
            },
            {
              "type" : "runtime_data",
              "value" : 0
            }
          ],
          "source_info" : {
            "filename" : "p4src/main.p4",
            "line" : 89,
            "column" : 8,
            "source_fragment" : "standard_metadata.mcast_grp = gid"
          }
        },
        {
          "op" : "assign",
          "parameters" : [
            {
              "type" : "field",
              "value" : ["scalars", "local_metadata_t.is_multicast"]
            },
            {
              "type" : "expression",
              "value" : {
                "type" : "expression",
                "value" : {
                  "op" : "b2d",
                  "left" : null,
                  "right" : {
                    "type" : "bool",
                    "value" : true
                  }
                }
              }
            }
          ],
          "source_info" : {
            "filename" : "p4src/main.p4",
            "line" : 90,
            "column" : 8,
            "source_fragment" : "local_metadata.is_multicast = true"
          }
        }
      ]
    }
  ],
  "pipelines" : [
    {
      "name" : "ingress",
      "id" : 0,
      "source_info" : {
        "filename" : "p4src/main.p4",
        "line" : 71,
        "column" : 8,
        "source_fragment" : "IngressPipeImpl"
      },
      "init_table" : "IngressPipeImpl.l2_exact_table",
      "tables" : [
        {
          "name" : "IngressPipeImpl.l2_exact_table",
          "id" : 0,
          "source_info" : {
            "filename" : "p4src/main.p4",
            "line" : 95,
            "column" : 10,
            "source_fragment" : "l2_exact_table"
          },
          "key" : [
            {
              "match_type" : "exact",
              "name" : "standard_metadata.ingress_port",
              "target" : ["standard_metadata", "ingress_port"],
              "mask" : null
            }
          ],
          "match_type" : "exact",
          "type" : "simple",
          "max_size" : 1024,
          "with_counters" : false,
          "support_timeout" : false,
          "direct_meters" : null,
          "action_ids" : [1, 2, 0],
          "actions" : ["IngressPipeImpl.set_egress_port", "IngressPipeImpl.set_multicast_group", "IngressPipeImpl.drop"],
          "base_default_next" : null,
          "next_tables" : {
            "IngressPipeImpl.set_egress_port" : null,
            "IngressPipeImpl.set_multicast_group" : null,
            "IngressPipeImpl.drop" : null
          },
          "default_entry" : {
            "action_id" : 0,
            "action_const" : true,
            "action_data" : [],
            "action_entry_const" : true
          }
        }
      ],
      "action_profiles" : [],
      "conditionals" : []
    },
    {
      "name" : "egress",
      "id" : 1,
      "source_info" : {
        "filename" : "p4src/main.p4",
        "line" : 116,
        "column" : 8,
        "source_fragment" : "EgressPipeImpl"
      },
      "init_table" : null,
      "tables" : [],
      "action_profiles" : [],
      "conditionals" : []
    }
  ],
  "checksums" : [],
  "force_arith" : [],
  "extern_instances" : [],
  "field_aliases" : [
    [
      "queueing_metadata.enq_timestamp",
      ["standard_metadata", "enq_timestamp"]
    ],
    [
      "queueing_metadata.enq_qdepth",
      ["standard_metadata", "enq_qdepth"]
    ],
    [
      "queueing_metadata.deq_timedelta",
      ["standard_metadata", "deq_timedelta"]
    ],
    [
      "queueing_metadata.deq_qdepth",
      ["standard_metadata", "deq_qdepth"]
    ],
    [
      "intrinsic_metadata.ingress_global_timestamp",
      ["standard_metadata", "ingress_global_timestamp"]
    ],
    [
      "intrinsic_metadata.egress_global_timestamp",
      ["standard_metadata", "egress_global_timestamp"]
    ],
    [
      "intrinsic_metadata.lf_field_list",
      ["standard_metadata", "lf_field_list"]
    ],
    [
      "intrinsic_metadata.mcast_grp",
      ["standard_metadata", "mcast_grp"]
    ],
    [
      "intrinsic_metadata.resubmit_flag",
      ["standard_metadata", "resubmit_flag"]
    ],
    [
      "intrinsic_metadata.egress_rid",
      ["standard_metadata", "egress_rid"]
    ],
    [
      "intrinsic_metadata.recirculate_flag",
      ["standard_metadata", "recirculate_flag"]
    ],
    [
      "intrinsic_metadata.priority",
      ["standard_metadata", "priority"]
    ]
  ],
  "program" : "p4src/main.p4",
  "__meta__" : {
    "version" : [2, 18],
    "compiler" : "https://github.com/p4lang/p4c"
  }
}
 No newline at end of file
+144 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019-present Open Networking Foundation
 *
 * 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 <core.p4>
#include <v1model.p4>

typedef bit<9>   port_num_t;
typedef bit<48>  mac_addr_t;
typedef bit<16>  mcast_group_id_t;

//------------------------------------------------------------------------------
// HEADER DEFINITIONS
//------------------------------------------------------------------------------

header ethernet_t {
    mac_addr_t  dst_addr;
    mac_addr_t  src_addr;
    bit<16>     ether_type;
}

struct parsed_headers_t {
    ethernet_t  ethernet;
}

struct local_metadata_t {
    bool        is_multicast;
}


//------------------------------------------------------------------------------
// INGRESS PIPELINE
//------------------------------------------------------------------------------

parser ParserImpl (packet_in packet,
                   out parsed_headers_t hdr,
                   inout local_metadata_t local_metadata,
                   inout standard_metadata_t standard_metadata)
{
    state start {
      transition parse_ethernet;
    }

    state parse_ethernet {
        packet.extract(hdr.ethernet);
        transition accept;
    }
}


control VerifyChecksumImpl(inout parsed_headers_t hdr,
                           inout local_metadata_t meta)
{
    apply { /* EMPTY */ }
}


control IngressPipeImpl (inout parsed_headers_t    hdr,
                         inout local_metadata_t    local_metadata,
                         inout standard_metadata_t standard_metadata) {

    // Drop action shared by many tables.
    action drop() {
        mark_to_drop(standard_metadata);
    }

    action set_egress_port(port_num_t port) {
        standard_metadata.egress_spec = port;
    }

    action set_multicast_group(mcast_group_id_t gid) {
        // gid will be used by the Packet Replication Engine (PRE) in the
        // Traffic Manager--located right after the ingress pipeline, to
        // replicate a packet to multiple egress ports, specified by the control
        // plane by means of P4Runtime MulticastGroupEntry messages.
        standard_metadata.mcast_grp = gid;
        local_metadata.is_multicast = true;
    }

    // --- l2_exact_table ------------------

    table l2_exact_table {
        key = {
            standard_metadata.ingress_port: exact;
        }
        actions = {
            set_egress_port;
            set_multicast_group;
            @defaultonly drop;
        }
        const default_action = drop;
    }

    apply {
        l2_exact_table.apply();
    }
}

//------------------------------------------------------------------------------
// EGRESS PIPELINE
//------------------------------------------------------------------------------

control EgressPipeImpl (inout parsed_headers_t hdr,
                        inout local_metadata_t local_metadata,
                        inout standard_metadata_t standard_metadata) {
    apply { /* EMPTY */ }
}


control ComputeChecksumImpl(inout parsed_headers_t hdr,
                            inout local_metadata_t local_metadata)
{
    apply { /* EMPTY */ }
}


control DeparserImpl(packet_out packet, in parsed_headers_t hdr) {
    apply {
        packet.emit(hdr.ethernet);
    }
}


V1Switch(
    ParserImpl(),
    VerifyChecksumImpl(),
    IngressPipeImpl(),
    EgressPipeImpl(),
    ComputeChecksumImpl(),
    DeparserImpl()
) main;
+62 −0
Original line number Diff line number Diff line
pkg_info {
  arch: "v1model"
}
tables {
  preamble {
    id: 33605373
    name: "IngressPipeImpl.l2_exact_table"
    alias: "l2_exact_table"
  }
  match_fields {
    id: 1
    name: "standard_metadata.ingress_port"
    bitwidth: 9
    match_type: EXACT
  }
  action_refs {
    id: 16812802
  }
  action_refs {
    id: 16841371
  }
  action_refs {
    id: 16796182
    annotations: "@defaultonly"
    scope: DEFAULT_ONLY
  }
  const_default_action_id: 16796182
  size: 1024
}
actions {
  preamble {
    id: 16796182
    name: "IngressPipeImpl.drop"
    alias: "drop"
  }
}
actions {
  preamble {
    id: 16812802
    name: "IngressPipeImpl.set_egress_port"
    alias: "set_egress_port"
  }
  params {
    id: 1
    name: "port"
    bitwidth: 9
  }
}
actions {
  preamble {
    id: 16841371
    name: "IngressPipeImpl.set_multicast_group"
    alias: "set_multicast_group"
  }
  params {
    id: 1
    name: "gid"
    bitwidth: 16
  }
}
type_info {
}
Loading