Exemple #1
0
            public packet_out ProcessPacket(byte[] packet, Parser <H, M> p, VerifyChecksum <H, M> vr, Ingress <H, M> ig, Egress <H, M> eg, ComputeChecksum <H, M> ck, Deparser <H> dep)
            {
                H hdr  = default(H);
                M meta = default(M);
                standard_metadata_t smeta = new standard_metadata_t();

                try
                {
                    p.apply(new packet_in(packet), out hdr, meta, ref meta, smeta, ref smeta);
                }
                catch (P4Exception ex)
                {
                    Console.Error.WriteLine("P4 exception. V1Model not sure how to give to remainder of pipeline.");
                    Console.Error.WriteLine(ex.Error);
                }
                vr.apply(hdr, meta, ref meta);
                ig.apply(hdr, ref hdr, meta, ref meta, smeta, ref smeta);
                eg.apply(hdr, ref hdr, meta, ref meta, smeta, ref smeta);
                ck.apply(hdr, ref hdr, meta, ref meta);
                packet_out po = new packet_out(smeta);

                dep.apply(po, hdr);
                // TODO do something with the result
                return(po);
            }
Exemple #2
0
            private ProcessResult ProcessPacket(int in_port, byte[] packet)
            {
                H hdr = default(H);

                P4ToCSharp.Library.error parserError = Architecture.error.NoError; // FIXME move lib.error to Core.error?
                try
                {
                    p.apply(new packet_in(packet), out hdr);
                }
                catch (P4Exception ex)
                {
                    parserError = ex.Error;
                    Console.Error.WriteLine("P4 exception. " + ex.Error);
                }
                var inCtrl = new InControl()
                {
                    inputPort = new bit4((byte)in_port)
                };
                OutControl outCtrl;

                map.apply(hdr, ref hdr, parserError, inCtrl, out outCtrl);
                packet_out po = new packet_out();

                d.apply(hdr, ref hdr, po);
                // TODO do something with the result
                return(new ProcessResult()
                {
                    po = po, outCtrl = outCtrl
                });
            }
Exemple #3
0
            public override void process_packet(int in_port, byte[] packet_data)
            {
                // Provided by the user - NOT generated
                var inCtrl = new InControl()
                {
                    inputPort = (PortId)in_port
                };
                var       outCtrl  = new OutControl();
                packet_in packetIn = new packet_in(packet_data);
                H         headers  = null; // FIXME is this okay?
                error     err      = error.NoError;

                try
                {
                    p.apply(packetIn, out headers); // FIXME where does the error come from? Catch?
                } catch (error ex)
                {
                    err = ex;
                }
                map.apply(ref headers, err, inCtrl, out outCtrl);
                packet_out packetOut = new packet_out();

                d.apply(ref headers, packetOut);

                this.send_packet((int)outCtrl.outputPort, packetOut.RawData, packetOut.Length);
            }
Exemple #4
0
 public void apply(ref Parsed_packet p, packet_out b)
 {
     b.emit(p.ethernet);
     if (p.ip.isValid())
     {
         ck.clear();
         p.ip.hdrChecksum = (bit16)0;
         ck.update(p.ip);
         p.ip.hdrChecksum = ck.get();
     }
     b.emit(p.ip);
 }
Exemple #5
0
        public void apply(Parsed_packet p_capture, ref Parsed_packet p, packet_out b)
        {
            p = p_capture;
            TopDeparser_Args TopDeparser_Args = new TopDeparser_Args(this, p, b);

            TopDeparser_Args.b.emit <Ethernet_h>(TopDeparser_Args.p.ethernet);
            if (TopDeparser_Args.p.ip.isValid())
            {
                TopDeparser_Args.Instance.ck.clear();
                TopDeparser_Args.p.ip.hdrChecksum = ((bit16)0);
                TopDeparser_Args.Instance.ck.update <Ipv4_h>(TopDeparser_Args.p.ip);
                TopDeparser_Args.p.ip.hdrChecksum = TopDeparser_Args.Instance.ck.get();
            }

            TopDeparser_Args.b.emit <Ipv4_h>(TopDeparser_Args.p.ip);
        }
Exemple #6
0
            public void Run()
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.AppDomain.CurrentDomain.ProcessExit += (o, e) =>
                    {
                        Console.ReadKey();
                    }
                }
                ;

                var args = Environment.GetCommandLineArgs().Skip(1).ToArray();

                if (args.Length == 0)
                {
                    // TODO Process packets from the network
                    throw new NotImplementedException("Processing packets from the network is not implemented yet.");
                }
                else
                {
                    // Process packets from the STF file, checking outputs are correct
                    string stfPath = args[0];
                    if (!stfPath.EndsWith(".stf") || !System.IO.File.Exists(stfPath))
                    {
                        Console.Error.WriteLine("Invalid STF file specified or could not be found.");
                        Environment.Exit(1);
                    }
                    System.IO.FileInfo stfFile = new System.IO.FileInfo(stfPath);
                    using (var reader = stfFile.OpenText())
                    {
                        packet_out lastPacketOut = null;
                        while (!reader.EndOfStream)
                        {
                            string line      = reader.ReadLine().Split('#')[0];
                            var    lineParts = line.Split(' ');
                            switch (lineParts[0])
                            {
                            case "": break;

                            case "packet":
                            {
                                var intf = Convert.ToInt32(lineParts[1]);
                                var arr  = GetBytes(String.Join("", lineParts.Skip(2)));
                                var po   = ProcessPacket(arr, p, vr, ig, eg, ck, dep);
                                lastPacketOut = po;
                                break;
                            }

                            case "expect":
                            {
                                var intf = Convert.ToInt32(lineParts[1]);
                                var arr  = String.Join("", lineParts.Skip(2));
                                if (lastPacketOut != null)
                                {
                                    uint len    = (lastPacketOut.LengthInBits + 7) / 8;
                                    var  result = new byte[len];
                                    Array.Copy(lastPacketOut.Data, result, result.Length);
                                    if (StrArrEquals(arr, result))
                                    {
                                    }
                                    else
                                    {
                                        Console.Error.WriteLine("Output not what was expected:");
                                        Console.Error.WriteLine(" EXP: {0}", arr);
                                        Console.Error.WriteLine(" RCV: {0}", GetHex(result));
                                        Environment.Exit(1);
                                    }
                                }
                                else
                                {
                                    Console.Error.WriteLine("Expected packet but there was none.");
                                    Environment.Exit(1);
                                }
                                break;
                            }

                            case "add":
                            case "set_default":
                            default:
                                Console.Error.WriteLine("Cannot handle command {0}", lineParts[0]);
                                Environment.Exit(1);
                                break;
                            }
                        }
                    }
                }
                Console.WriteLine("Done.");
            }
        }
Exemple #7
0
 public TopDeparser_Args(TopDeparser Instance, Parsed_packet p, packet_out b)
 {
     this.Instance = Instance;
     this.p        = p;
     this.b        = b;
 }