container_gso_linux.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package tio
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. "golang.org/x/sys/unix"
  7. )
  8. type offloadContainer struct {
  9. pq []*Offload
  10. // pqi is exactly the same as pq, but stored as the interface type
  11. pqi []Queue
  12. shutdownFd int
  13. }
  14. func NewOffloadContainer() (Container, error) {
  15. shutdownFd, err := unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC)
  16. if err != nil {
  17. return nil, fmt.Errorf("failed to create eventfd: %w", err)
  18. }
  19. out := &offloadContainer{
  20. pq: []*Offload{},
  21. pqi: []Queue{},
  22. shutdownFd: shutdownFd,
  23. }
  24. return out, nil
  25. }
  26. func (c *offloadContainer) Queues() []Queue {
  27. return c.pqi
  28. }
  29. func (c *offloadContainer) Add(fd int) error {
  30. x, err := newOffload(fd, c.shutdownFd)
  31. if err != nil {
  32. return err
  33. }
  34. c.pq = append(c.pq, x)
  35. c.pqi = append(c.pqi, x)
  36. return nil
  37. }
  38. func (c *offloadContainer) wakeForShutdown() error {
  39. var buf [8]byte
  40. binary.NativeEndian.PutUint64(buf[:], 1)
  41. _, err := unix.Write(c.shutdownFd, buf[:])
  42. return err
  43. }
  44. func (c *offloadContainer) Close() error {
  45. errs := []error{}
  46. // Signal all readers blocked in poll to wake up and exit
  47. if err := c.wakeForShutdown(); err != nil {
  48. errs = append(errs, err)
  49. }
  50. for _, x := range c.pq {
  51. if err := x.Close(); err != nil {
  52. errs = append(errs, err)
  53. }
  54. }
  55. return errors.Join(errs...)
  56. }