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 | #define SELECTOR_WIDTH 64 |
| 23 | const bit<SELECTOR_WIDTH> ONE = 64w1; |
| 24 | |
| 25 | typedef bit<16> group_id_t; |
| 26 | |
| 27 | struct wcmp_metadata_t { |
| 28 | group_id_t group_id; |
| 29 | bit<8> numBits; |
| 30 | bit<SELECTOR_WIDTH> selector; |
| 31 | } |
| 32 | |
| 33 | struct metadata_t { |
| 34 | wcmp_metadata_t wcmp_metadata; |
| 35 | intrinsic_metadata_t intrinsic_metadata; |
| 36 | } |
| 37 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 38 | #include "include/parsers.p4" |
| 39 | #include "include/port_counters.p4" |
| 40 | #include "include/checksums.p4" |
| 41 | #include "include/actions.p4" |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 42 | #include "include/packet_io.p4" |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 43 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 44 | control ingress(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t standard_metadata) { |
| 45 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 46 | direct_counter(CounterType.packets) table0_counter; |
| 47 | direct_counter(CounterType.packets) wcmp_group_table_counter; |
| 48 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 49 | action wcmp_group(group_id_t group_id) { |
Carmelo Cascone | 3304fd5 | 2017-07-30 00:43:01 -0400 | [diff] [blame] | 50 | meta.wcmp_metadata.group_id = group_id; |
| 51 | hash(meta.wcmp_metadata.numBits, HashAlgorithm.crc16, (bit<64>)2, |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 52 | { hdr.ipv4.srcAddr, hdr.ipv4.dstAddr, hdr.ipv4.protocol, hdr.tcp.srcPort, hdr.tcp.dstPort, hdr.udp.srcPort, |
| 53 | hdr.udp.dstPort }, |
| 54 | (bit<128>)62); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | action wcmp_set_selector() { |
Carmelo Cascone | 3304fd5 | 2017-07-30 00:43:01 -0400 | [diff] [blame] | 58 | meta.wcmp_metadata.selector = ((ONE << meta.wcmp_metadata.numBits) - ONE) << (SELECTOR_WIDTH - meta.wcmp_metadata.numBits); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | table table0 { |
| 62 | support_timeout = true; |
| 63 | actions = { |
| 64 | set_egress_port(standard_metadata); |
| 65 | wcmp_group; |
| 66 | send_to_cpu(standard_metadata); |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 67 | _drop(standard_metadata); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 68 | } |
| 69 | key = { |
| 70 | standard_metadata.ingress_port: ternary; |
| 71 | hdr.ethernet.dstAddr : ternary; |
| 72 | hdr.ethernet.srcAddr : ternary; |
| 73 | hdr.ethernet.etherType : ternary; |
| 74 | } |
| 75 | counters = table0_counter; |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame] | 76 | default_action = _drop(standard_metadata); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | table wcmp_group_table { |
| 80 | actions = { |
| 81 | set_egress_port(standard_metadata); |
| 82 | } |
| 83 | key = { |
Carmelo Cascone | 3304fd5 | 2017-07-30 00:43:01 -0400 | [diff] [blame] | 84 | meta.wcmp_metadata.group_id : exact; |
| 85 | meta.wcmp_metadata.selector: lpm; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 86 | } |
| 87 | counters = wcmp_group_table_counter; |
| 88 | } |
| 89 | |
| 90 | PortCountersControl() port_counters_control; |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 91 | PacketIoIngressControl() packet_io_ingress_control; |
| 92 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 93 | apply { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 94 | packet_io_ingress_control.apply(hdr, standard_metadata); |
| 95 | if (!hdr.packet_out.isValid()) { |
| 96 | switch (table0.apply().action_run) { |
| 97 | wcmp_group: { |
| 98 | wcmp_set_selector(); |
| 99 | wcmp_group_table.apply(); |
| 100 | } |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | port_counters_control.apply(hdr, meta, standard_metadata); |
| 104 | } |
| 105 | } |
| 106 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 107 | control egress(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t standard_metadata) { |
| 108 | |
| 109 | PacketIoEgressControl() packet_io_egress_control; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 110 | apply { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 111 | packet_io_egress_control.apply(hdr, standard_metadata); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | V1Switch(ParserImpl(), verifyChecksum(), ingress(), egress(), computeChecksum(), DeparserImpl()) main; |