Coverage for birdplan/bird_config/sections/tables/master/__init__.py: 100%
60 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
1#
2# SPDX-License-Identifier: GPL-3.0-or-later
3#
4# Copyright (c) 2019-2024, AllWorldIT
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
19"""BIRD master table configuration."""
21from ....globals import BirdConfigGlobals
22from ...base import SectionBase
23from ...protocols.pipe import ProtocolPipe, ProtocolPipeFilterType
24from .master_attributes import MasterTableAttributes, MasterTableRoutePolicyExport
26__all__ = ["TableMaster"]
29class TableMaster(SectionBase):
30 """BIRD master table configuration."""
32 _master_attributes: MasterTableAttributes
34 def __init__(self, birdconfig_globals: BirdConfigGlobals):
35 """Initialize the object."""
36 super().__init__(birdconfig_globals)
38 # Set section header
39 self._section = "Master Table"
41 self._master_attributes = MasterTableAttributes()
43 def configure(self) -> None:
44 """Configure the master tables."""
45 super().configure()
47 # Setup filters
48 self._master_to_kernel_export_filter("4")
49 self._master_to_kernel_export_filter("6")
51 self._master_to_kernel_import_filter("4")
52 self._master_to_kernel_import_filter("6")
54 # Configure pipe from kernel table to master table
55 kernel_master_pipe = ProtocolPipe(
56 birdconfig_globals=self.birdconfig_globals,
57 table_from="master",
58 table_to="kernel",
59 export_filter_type=ProtocolPipeFilterType.VERSIONED,
60 import_filter_type=ProtocolPipeFilterType.VERSIONED,
61 )
62 self.conf.add(kernel_master_pipe)
64 def _master_to_kernel_import_filter(self, ipv: str) -> None:
65 """Master to kernel import filter setup."""
66 # Configure import filter to master table
67 self.conf.add(f"filter f_master{ipv}_kernel{ipv}_import {{")
68 self.conf.add(" # Accept all routes from the kernel, always")
69 self.conf.add(" if (source = RTS_INHERIT) then {")
70 self.conf.add(" accept;")
71 self.conf.add(" }")
72 self.conf.add(" reject;")
73 self.conf.add("};")
74 self.conf.add("")
76 def _master_to_kernel_export_filter(self, ipv: str) -> None:
77 """Master to kernel export filter setup."""
78 # Configure export filter to master table
79 self.conf.add(f"filter f_master{ipv}_kernel{ipv}_export {{")
81 if self.route_policy_export.kernel.static:
82 self.conf.add(" # Export static routes to kernel")
83 self.conf.add(" if (source = RTS_STATIC) then {")
84 self.conf.add(" accept;")
85 self.conf.add(" }")
87 if self.route_policy_export.kernel.rip:
88 self.conf.add(" # Export RIP routes to kernel")
89 self.conf.add(" if (source = RTS_RIP) then {")
90 self.conf.add(" accept;")
91 self.conf.add(" }")
93 if self.route_policy_export.kernel.ospf:
94 self.conf.add(" # Export OSPF routes to kernel")
95 # NK: We cannot seem to filter out the device routes
96 self.conf.add(" if (source ~ [RTS_OSPF, RTS_OSPF_IA, RTS_OSPF_EXT1, RTS_OSPF_EXT2]) then {")
97 self.conf.add(" accept;")
98 self.conf.add(" }")
100 if self.route_policy_export.kernel.bgp:
101 self.conf.add(" # Export BGP routes to kernel")
102 self.conf.add(" if (source = RTS_BGP) then {")
103 self.conf.add(" accept;")
104 self.conf.add(" }")
106 self.conf.add(" reject;")
107 self.conf.add("};")
108 self.conf.add("")
110 @property
111 def master_attributes(self) -> MasterTableAttributes:
112 """Return our table attributes."""
113 return self._master_attributes
115 @property
116 def route_policy_export(self) -> MasterTableRoutePolicyExport:
117 """Return our route policy for exporting routes from the master table."""
118 return self.master_attributes.route_policy_export