Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | /* -*- P4_16 -*- */ |
| 18 | #ifndef __INT_SOURCE__ |
| 19 | #define __INT_SOURCE__ |
| 20 | |
| 21 | // Insert INT header to the packet |
| 22 | control process_int_source ( |
| 23 | inout headers_t hdr, |
| 24 | inout local_metadata_t local_metadata, |
| 25 | inout standard_metadata_t standard_metadata) { |
| 26 | |
| 27 | direct_counter(CounterType.packets_and_bytes) counter_int_source; |
| 28 | |
| 29 | action int_source(bit<8> max_hop, bit<5> ins_cnt, bit<4> ins_mask0003, bit<4> ins_mask0407) { |
| 30 | // insert INT shim header |
| 31 | hdr.intl4_shim.setValid(); |
| 32 | // int_type: Hop-by-hop type (1) , destination type (2) |
| 33 | hdr.intl4_shim.int_type = 1; |
| 34 | hdr.intl4_shim.len = INT_HEADER_LEN_WORD; |
| 35 | |
| 36 | // insert INT header |
| 37 | hdr.int_header.setValid(); |
| 38 | hdr.int_header.ver = 0; |
| 39 | hdr.int_header.rep = 0; |
| 40 | hdr.int_header.c = 0; |
| 41 | hdr.int_header.e = 0; |
| 42 | hdr.int_header.rsvd1 = 0; |
| 43 | hdr.int_header.ins_cnt = ins_cnt; |
| 44 | hdr.int_header.max_hop_cnt = max_hop; |
| 45 | hdr.int_header.total_hop_cnt = 0; |
| 46 | hdr.int_header.instruction_mask_0003 = ins_mask0003; |
| 47 | hdr.int_header.instruction_mask_0407 = ins_mask0407; |
| 48 | hdr.int_header.instruction_mask_0811 = 0; // not supported |
| 49 | hdr.int_header.instruction_mask_1215 = 0; // not supported |
| 50 | |
| 51 | // insert INT tail header |
| 52 | hdr.intl4_tail.setValid(); |
| 53 | hdr.intl4_tail.next_proto = hdr.ipv4.protocol; |
| 54 | hdr.intl4_tail.dest_port = local_metadata.l4_dst_port; |
| 55 | hdr.intl4_tail.dscp = 0; // not used |
| 56 | |
| 57 | hdr.udp.dst_port = INT_PORT; |
| 58 | |
| 59 | // add the header len (8 bytes) to total len |
| 60 | hdr.ipv4.len = hdr.ipv4.len + 16; |
| 61 | hdr.udp.length_ = hdr.udp.length_ + 16; |
| 62 | } |
| 63 | |
| 64 | table tb_int_source { |
| 65 | key = { |
| 66 | local_metadata.int_meta.sink: exact; |
| 67 | local_metadata.int_meta.source: exact; |
| 68 | hdr.ipv4.src_addr: ternary; |
| 69 | hdr.ipv4.dst_addr: ternary; |
| 70 | local_metadata.l4_src_port: ternary; |
| 71 | local_metadata.l4_dst_port: ternary; |
| 72 | } |
| 73 | actions = { |
| 74 | int_source; // sink = 0 & source = 1 |
| 75 | } |
| 76 | counters = counter_int_source; |
| 77 | size = 1024; |
| 78 | } |
| 79 | |
| 80 | apply { |
| 81 | tb_int_source.apply(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | control process_set_source_sink ( |
| 86 | inout headers_t hdr, |
| 87 | inout local_metadata_t local_metadata, |
| 88 | inout standard_metadata_t standard_metadata) { |
| 89 | |
| 90 | direct_counter(CounterType.packets_and_bytes) counter_set_source; |
| 91 | direct_counter(CounterType.packets_and_bytes) counter_set_sink; |
| 92 | |
| 93 | action int_set_source () { |
| 94 | local_metadata.int_meta.source = 1; |
| 95 | } |
| 96 | |
| 97 | action int_set_sink () { |
| 98 | local_metadata.int_meta.sink = 1; |
| 99 | } |
| 100 | |
| 101 | table tb_set_source { |
| 102 | key = { |
| 103 | hdr.ipv4.src_addr: ternary; |
| 104 | hdr.ipv4.dst_addr: ternary; |
| 105 | local_metadata.l4_src_port: ternary; |
| 106 | local_metadata.l4_dst_port: ternary; |
| 107 | } |
| 108 | actions = { |
| 109 | int_set_source; |
| 110 | } |
| 111 | counters = counter_set_source; |
| 112 | size = 1024; |
| 113 | } |
| 114 | |
| 115 | table tb_set_sink { |
| 116 | key = { |
| 117 | hdr.ipv4.src_addr: ternary; |
| 118 | hdr.ipv4.dst_addr: ternary; |
| 119 | local_metadata.l4_src_port: ternary; |
| 120 | local_metadata.l4_dst_port: ternary; |
| 121 | } |
| 122 | actions = { |
| 123 | int_set_sink; |
| 124 | } |
| 125 | counters = counter_set_sink; |
| 126 | size = 1024; |
| 127 | } |
| 128 | |
| 129 | apply { |
| 130 | if (hdr.udp.isValid()) { |
| 131 | tb_set_source.apply(); |
| 132 | tb_set_sink.apply(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | #endif |