e4b40920dca4ced12fd1c5fe97dc0a1247bc347d
[vpp.git] / src / vpp-api / vom / acl_l3_rule.cpp
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <sstream>
17
18 #include "vom/acl_l3_rule.hpp"
19
20 namespace VOM {
21 namespace ACL {
22 l3_rule::l3_rule(uint32_t priority,
23                  const action_t& action,
24                  const route::prefix_t& src,
25                  const route::prefix_t& dst)
26   : m_priority(priority)
27   , m_action(action)
28   , m_src(src)
29   , m_dst(dst)
30   , m_proto(0)
31   , m_srcport_or_icmptype_first(0)
32   , m_srcport_or_icmptype_last(0)
33   , m_dstport_or_icmpcode_first(0)
34   , m_dstport_or_icmpcode_last(0)
35   , m_tcp_flags_mask(0)
36   , m_tcp_flags_value(0)
37 {
38 }
39
40 bool
41 l3_rule::operator<(const l3_rule& other) const
42 {
43   return (other.m_priority < m_priority);
44 }
45
46 void
47 l3_rule::to_vpp(vapi_type_acl_rule& rule) const
48 {
49   rule.is_permit = m_action.value();
50   m_src.to_vpp(&rule.is_ipv6, rule.src_ip_addr, &rule.src_ip_prefix_len);
51   m_dst.to_vpp(&rule.is_ipv6, rule.dst_ip_addr, &rule.dst_ip_prefix_len);
52
53   rule.proto = m_proto;
54   rule.srcport_or_icmptype_first = m_srcport_or_icmptype_first;
55   rule.srcport_or_icmptype_last = m_srcport_or_icmptype_last;
56   rule.dstport_or_icmpcode_first = m_dstport_or_icmpcode_first;
57   rule.dstport_or_icmpcode_last = m_dstport_or_icmpcode_last;
58
59   rule.tcp_flags_mask = m_tcp_flags_mask;
60   rule.tcp_flags_value = m_tcp_flags_value;
61 }
62
63 bool
64 l3_rule::operator==(const l3_rule& rule) const
65 {
66   return ((m_action == rule.m_action) && (m_src == rule.m_src) &&
67           (m_dst == rule.m_dst) && (m_proto == rule.m_proto) &&
68           (m_srcport_or_icmptype_first == rule.m_srcport_or_icmptype_first) &&
69           (m_srcport_or_icmptype_last == rule.m_srcport_or_icmptype_last) &&
70           (m_dstport_or_icmpcode_first == rule.m_dstport_or_icmpcode_first) &&
71           (m_dstport_or_icmpcode_last == rule.m_dstport_or_icmpcode_last) &&
72           (m_tcp_flags_mask == rule.m_tcp_flags_mask) &&
73           (m_tcp_flags_value == rule.m_tcp_flags_value));
74 }
75
76 std::string
77 l3_rule::to_string() const
78 {
79   std::ostringstream s;
80
81   s << "L3-rule:["
82     << "priority:" << m_priority << " action:" << m_action.to_string()
83     << " src:" << m_src.to_string() << " dst:" << m_dst.to_string()
84     << " proto:" << std::to_string(m_proto)
85     << " srcportfrom:" << m_srcport_or_icmptype_first
86     << " srcportto: " << m_srcport_or_icmptype_last
87     << " dstportfrom:" << m_dstport_or_icmpcode_first
88     << " dstportto:" << m_dstport_or_icmpcode_last
89     << " tcpflagmask:" << m_tcp_flags_mask
90     << " tcpflagvalue:" << m_tcp_flags_value << "]";
91
92   return (s.str());
93 }
94
95 void
96 l3_rule::set_src_ip(route::prefix_t src)
97 {
98   m_src = src;
99 }
100
101 void
102 l3_rule::set_dst_ip(route::prefix_t dst)
103 {
104   m_dst = dst;
105 }
106
107 void
108 l3_rule::set_proto(uint8_t proto)
109 {
110   m_proto = proto;
111 }
112 void
113 l3_rule::set_src_from_port(uint16_t srcport_or_icmptype_first)
114 {
115   m_srcport_or_icmptype_first = srcport_or_icmptype_first;
116 }
117
118 void
119 l3_rule::set_src_to_port(uint16_t srcport_or_icmptype_last)
120 {
121   m_srcport_or_icmptype_last = srcport_or_icmptype_last;
122 }
123
124 void
125 l3_rule::set_dst_from_port(uint16_t dstport_or_icmpcode_first)
126 {
127   m_dstport_or_icmpcode_first = dstport_or_icmpcode_first;
128 }
129
130 void
131 l3_rule::set_dst_to_port(uint16_t dstport_or_icmpcode_last)
132 {
133   m_dstport_or_icmpcode_last = dstport_or_icmpcode_last;
134 }
135
136 void
137 l3_rule::set_tcp_flags_mask(uint8_t tcp_flags_mask)
138 {
139   m_tcp_flags_mask = tcp_flags_mask;
140 }
141
142 void
143 l3_rule::set_tcp_flags_value(uint8_t tcp_flags_value)
144 {
145   m_tcp_flags_value = tcp_flags_value;
146 }
147 }
148 }
149
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "mozilla")
155  * End:
156  */