Carmelo Cascone | c8d3486 | 2017-07-30 00:48:23 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | c8d3486 | 2017-07-30 00:48:23 -0400 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 17 | #include <core.p4> |
| 18 | #include <v1model.p4> |
| 19 | #include "include/defines.p4" |
| 20 | #include "include/headers.p4" |
Carmelo Cascone | 3304fd5 | 2017-07-30 00:43:01 -0400 | [diff] [blame] | 21 | |
| 22 | typedef bit<16> group_id_t; |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | Expected number of ports of an ECMP group. |
| 26 | This value is fixed, .i.e. we do not support ECMP over port groups of different size. |
| 27 | Due to hardware limitations, this value must be constant and a power of 2. |
| 28 | */ |
| 29 | #define ECMP_GROUP_SIZE 128w2 |
Carmelo Cascone | 3304fd5 | 2017-07-30 00:43:01 -0400 | [diff] [blame] | 30 | |
| 31 | struct ecmp_metadata_t { |
| 32 | group_id_t group_id; |
| 33 | bit<16> selector; |
| 34 | } |
| 35 | |
| 36 | struct metadata_t { |
| 37 | ecmp_metadata_t ecmp_metadata; |
| 38 | intrinsic_metadata_t intrinsic_metadata; |
| 39 | } |
| 40 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 41 | #include "include/parsers.p4" |
| 42 | #include "include/port_counters.p4" |
| 43 | #include "include/checksums.p4" |
| 44 | #include "include/actions.p4" |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 45 | #include "include/packet_io.p4" |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 46 | |
| 47 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 48 | control ingress(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t standard_metadata) { |
| 49 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 50 | direct_counter(CounterType.packets) ecmp_group_table_counter; |
| 51 | direct_counter(CounterType.packets) table0_counter; |
| 52 | |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 53 | action ecmp_group(group_id_t group_id) { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 54 | meta.ecmp_metadata.group_id = group_id; |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 55 | hash(meta.ecmp_metadata.selector, HashAlgorithm.crc32, (bit<64>)0, |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 56 | { hdr.ipv4.srcAddr, hdr.ipv4.dstAddr, hdr.ipv4.protocol, hdr.tcp.srcPort, hdr.tcp.dstPort, hdr.udp.srcPort, |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 57 | hdr.udp.dstPort }, ECMP_GROUP_SIZE); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | table ecmp_group_table { |
| 61 | actions = { |
| 62 | set_egress_port(standard_metadata); |
| 63 | } |
| 64 | key = { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 65 | meta.ecmp_metadata.group_id : exact; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 66 | meta.ecmp_metadata.selector: exact; |
| 67 | } |
| 68 | counters = ecmp_group_table_counter; |
| 69 | } |
| 70 | |
| 71 | table table0 { |
| 72 | support_timeout = true; |
| 73 | actions = { |
| 74 | ecmp_group; |
| 75 | set_egress_port(standard_metadata); |
| 76 | send_to_cpu(standard_metadata); |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 77 | _drop(standard_metadata); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 78 | } |
| 79 | key = { |
| 80 | standard_metadata.ingress_port: ternary; |
| 81 | hdr.ethernet.dstAddr : ternary; |
| 82 | hdr.ethernet.srcAddr : ternary; |
| 83 | hdr.ethernet.etherType : ternary; |
| 84 | } |
| 85 | counters = table0_counter; |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 86 | default_action = _drop(standard_metadata); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 87 | } |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 88 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 89 | PortCountersControl() port_counters_control; |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 90 | PacketIoIngressControl() packet_io_ingress_control; |
| 91 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 92 | apply { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 93 | packet_io_ingress_control.apply(hdr, standard_metadata); |
| 94 | if (!hdr.packet_out.isValid()) { |
| 95 | switch (table0.apply().action_run) { |
| 96 | ecmp_group: { |
| 97 | ecmp_group_table.apply(); |
| 98 | } |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 101 | port_counters_control.apply(hdr, meta, standard_metadata); |
| 102 | } |
| 103 | } |
| 104 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 105 | control egress(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t standard_metadata) { |
| 106 | |
| 107 | PacketIoEgressControl() packet_io_egress_control; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 108 | apply { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 109 | packet_io_egress_control.apply(hdr, standard_metadata); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | V1Switch(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main; |