blob: 6cb2a006a331c2cd4263338ec1600eff9e138d0c [file] [log] [blame]
Chip Boling67b674a2019-02-08 11:42:18 -06001# Copyright 2017-present Open Networking Foundation
2#
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.
Zack Williams84a71e92019-11-15 09:00:19 -070014from __future__ import absolute_import
Chip Boling67b674a2019-02-08 11:42:18 -060015from unittest import TestCase, main
16
17from scapy.layers.l2 import Ether, Dot1Q
18
19from pyvoltha.adapters.common.frameio.frameio import BpfProgramFilter
20
21
22class TestBpf(TestCase):
23
24 def test_bpf1(self):
25 vid = 4090
26 pcp = 7
27 frame_match = 'ether[14:2] = 0x{:01x}{:03x}'.format(pcp << 1, vid)
28 filter = BpfProgramFilter(frame_match)
Zack Williams84a71e92019-11-15 09:00:19 -070029 self.assertTrue(filter(bytes(Ether()/Dot1Q(prio=pcp, vlan=vid))))
30 self.assertFalse(filter(bytes(Ether()/Dot1Q(prio=pcp, vlan=4000))))
Chip Boling67b674a2019-02-08 11:42:18 -060031
32 def test_bpf2(self):
33 vid1 = 4090
34 pcp1 = 7
35 frame_match_case1 = 'ether[14:2] = 0x{:01x}{:03x}'.format(
36 pcp1 << 1, vid1)
37
38 vid2 = 4000
39 frame_match_case2 = '(ether[14:2] & 0xfff) = 0x{:03x}'.format(vid2)
40
41 filter = BpfProgramFilter('{} or {}'.format(
42 frame_match_case1, frame_match_case2))
Zack Williams84a71e92019-11-15 09:00:19 -070043 self.assertTrue(filter(bytes(Ether()/Dot1Q(prio=pcp1, vlan=vid1))))
44 self.assertTrue(filter(bytes(Ether()/Dot1Q(vlan=vid2))))
45 self.assertFalse(filter(bytes(Ether()/Dot1Q(vlan=4001))))
Chip Boling67b674a2019-02-08 11:42:18 -060046
47
48if __name__ == '__main__':
49 main()