Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 1 | # 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 Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame^] | 14 | from __future__ import absolute_import |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 15 | from unittest import TestCase, main |
| 16 | |
| 17 | from scapy.layers.l2 import Ether, Dot1Q |
| 18 | |
| 19 | from pyvoltha.adapters.common.frameio.frameio import BpfProgramFilter |
| 20 | |
| 21 | |
| 22 | class 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 Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame^] | 29 | self.assertTrue(filter(bytes(Ether()/Dot1Q(prio=pcp, vlan=vid)))) |
| 30 | self.assertFalse(filter(bytes(Ether()/Dot1Q(prio=pcp, vlan=4000)))) |
Chip Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 31 | |
| 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 Williams | 84a71e9 | 2019-11-15 09:00:19 -0700 | [diff] [blame^] | 43 | 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 Boling | 67b674a | 2019-02-08 11:42:18 -0600 | [diff] [blame] | 46 | |
| 47 | |
| 48 | if __name__ == '__main__': |
| 49 | main() |