mgmt icon indicating copy to clipboard operation
mgmt copied to clipboard

Consider using github.com/mdlayher/netlink

Open mdlayher opened this issue 6 years ago • 8 comments

Hey folks! I was browsing through GitHub search and came across this blog by @jonathangold that talks about using netlink sockets in this project:

https://github.com/jonathangold/jonathangold.ca/blob/706d0af6aa568cbd1524b56543410227d466d0db/content/blog/go-netlink-and-select.md

Just wanted to let you all know that as of Go 1.12, https://github.com/mdlayher/netlink is fully integrated with the runtime network poller, so we get full support for timeouts, cancelation of receive, etc.

You may also be interested in https://github.com/mdlayher/kobject for kobject uevents, and https://github.com/jsimonetti/rtnetlink as an alternative to vishvananda/netlink.

Finally, a group of us who work on various low-level networking and netlink related things can be found in #networking on Gophers Slack, and we'd love to have you join us if possible!

Thanks for your time, and feel free to close this out if you'd like.

mdlayher avatar Apr 10 '19 16:04 mdlayher

@mdlayher Thanks for dropping in! If you're interested, a lot of us are hanging out in #mgmtconfig on Freenode IRC. I'd love to join you, but I don't have slack.

If you'd like to help us patch our current code to improve it as you suggest, we'd love that! Also, any comments on this: https://github.com/purpleidea/mgmt/blob/master/util/socketset/socketset.go#L20 would be very welcome!

purpleidea avatar Apr 10 '19 18:04 purpleidea

Good to know! I'd like to loop back and make some contributions at some point.

What you're doing in your netlink syscall code seems sane. If you're interested in making use of timeout support and such, my package is in good shape to handle that.

mdlayher avatar Apr 11 '19 14:04 mdlayher

@mdlayher Had a very quick look just now at your new lib...

func (c *Client) Receive() (*Event, error)
func (c *Client) SetDeadline(t time.Time) error

Assuming I understand correctly (which is most possibly not the case) wouldn't you instead want:

func (c *Client) Receive(context.Context) (*Event, error)

Which could replace both of those?

My knowledge of system networking internals is a bit weak, so any help you can provide to this project would be appreciated! We'd love to have more robust and featureful network resources and functions in mgmt!

purpleidea avatar Apr 11 '19 22:04 purpleidea

Basically, the netlink stack under the hood is leaning on https://golang.org/pkg/os/#File.SetDeadline to leverage the runtime network poller's ability to handle non-blocking system calls, timeouts, etc.

Having Receive take a context is an interesting idea, but there's no runtime hook for doing so at this point. Perhaps it'd be worth adding a ReceiveContext or similar, but I haven't had enough time to think it through and see how viable it'd actually be.

mdlayher avatar Apr 12 '19 21:04 mdlayher

@mdlayher The two tricky things which we always need to be able to do are:

  1. Wait on some sort of event to know when something changed (without polling)
  2. Unblock virtually instantly when we don't want to wait anymore. (without polling)

Is that possible with your Receive method? AFAICT, I'd have to set a small deadline and poll. Unless the Close causes the Receive call to unblock, but then I'd have to keep making them...

purpleidea avatar Apr 12 '19 22:04 purpleidea

This is a problem many Golang users have faced, including myself. Even the Golang community can't decide: https://github.com/golang/go/issues/20280 https://github.com/golang/go/issues/36402 https://github.com/golang/go/issues/41054

The cancellation of blocking IO operations in Go is a really tricky problem to solve, afaik. I think the "most accepted" option is to set the deadline to Now() when the context is Done(). It's certainly not the cleanest, but should suffice in most cases I think.

https://golang.org/pkg/os/#File.SetDeadline says

SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call.

frebib avatar Nov 15 '20 15:11 frebib

I'm going to reopen this as we should eventually take some time to revisit and see about if a port to this would make sense. Thanks again for your library Matt!

purpleidea avatar Feb 11 '22 16:02 purpleidea

No worries! I was just closing out things I had no intent of following up on. I do believe the current v1 should suit your needs, but if not, feel free to open an issue.

mdlayher avatar Feb 11 '22 16:02 mdlayher