Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ip route save and ip route restore commands #1203

Closed
pseusys opened this issue Jul 31, 2024 · 5 comments
Closed

Support for ip route save and ip route restore commands #1203

pseusys opened this issue Jul 31, 2024 · 5 comments
Labels

Comments

@pseusys
Copy link

pseusys commented Jul 31, 2024

Browsing the ip command docs I have noticed three different commands for route bulk manipulation: ip route flush, ip route save and ip route restore. Sadly, it seems like only the first one is implemented in pyroute2. Is there an explanation for that? The latter two commands might be really useful for routing table manipulation. Is there any official workaround (involving IPRoute.get_routes(table=...) probably)? Could I maybe help adding support for that?
If no support is planned, could you please let me know, how can I restore a route thaat I once received with IPRoute.get_routes? It is received in a form of a dictionary.

@pseusys
Copy link
Author

pseusys commented Jan 12, 2025

Here's a patched implementation o IPRoute class, that implements both ip route save and ip route restore commands:

class PatchedIPRoute(IPRoute):
    def save_routes(self, *argv, **kwarg):
        return list(self.get_routes(*argv, **kwarg))

    def restore_routes(self, routes):
        for route in routes:
            self.put(route, msg_type=RTM_NEWROUTE, msg_flags=NLM_F_REQUEST)

@svinota
Copy link
Owner

svinota commented Jan 12, 2025

This might require some additional investigation if we need to serialize route objects — just not to exhaust all the memory on huge numbers of routes.

Beside of that it might be useful to make it compatible with ip route save format.

Ok, got it. Thanks for the request, I will dig around, and update you here next week.

svinota added a commit that referenced this issue Jan 14, 2025
A proof of concept of a binary stream dumper.

* ipr.route_dump(fd, family=AF_UNSPEC) -- save routes to a
  file object; it can be an open file, BytesIO() or like that
* ipr.route_dumps(family=AF_UNSPEC) -- return routes as a binary
  string

Bug-Url: #1203
@svinota
Copy link
Owner

svinota commented Jan 14, 2025

@pseusys started to implement the feature, you can follow that on branch 1203-iproute-save.

svinota added a commit that referenced this issue Jan 14, 2025
A proof of concept of a binary stream dumper.

* ipr.route_dump(fd, family=AF_UNSPEC) -- save routes to a
  file object; it can be an open file, BytesIO() or like that
* ipr.route_dumps(family=AF_UNSPEC) -- return routes as a binary
  string

Bug-Url: #1203
svinota added a commit that referenced this issue Jan 14, 2025
A proof of concept of a binary stream dumper.

* ipr.route_dump(fd, family=AF_UNSPEC) -- save routes to a
  file object; it can be an open file, BytesIO() or like that
* ipr.route_dumps(family=AF_UNSPEC) -- return routes as a binary
  string

Bug-Url: #1203
svinota added a commit that referenced this issue Jan 14, 2025
A proof of concept of a binary stream dumper.

* ipr.route_dump(fd, family=AF_UNSPEC) -- save routes to a
  file object; it can be an open file, BytesIO() or like that
* ipr.route_dumps(family=AF_UNSPEC) -- return routes as a binary
  string

Bug-Url: #1203
svinota added a commit that referenced this issue Jan 14, 2025
A proof of concept of a binary stream dumper.

* ipr.route_dump(fd, family=AF_UNSPEC) -- save routes to a
  file object; it can be an open file, BytesIO() or like that
* ipr.route_dumps(family=AF_UNSPEC) -- return routes as a binary
  string

Bug-Url: #1203
svinota added a commit that referenced this issue Jan 15, 2025
* ipr.route_load(fd, fmt='iproute2') -- load a dump from
  a file object; compatible with `iproute2 routes save` files
* ipr.route_load(data, fmt='iproute2') -- load a dump from
  a bytes object

Bug-Url: #1203
svinota added a commit that referenced this issue Jan 15, 2025
iproute: route_dump() and route_load() functionality

Bug-Url: #1247
Bug-Url: #1203
@svinota
Copy link
Owner

svinota commented Jan 15, 2025

Merged into the master branch. Some more tests and docs will be added, route_load() will be optimized, but it's usable already.

@svinota
Copy link
Owner

svinota commented Jan 17, 2025

@pseusys I'm closing the ticket, but don't hesitate to reopen, or to open a new one in the case there will be any issues using the feature.

@svinota svinota closed this as completed Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants