|
| 1 | +// Module to enable or disable CLT in the kernel by using generic netlink |
| 2 | + |
| 3 | +package clt_genl |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/mdlayher/genetlink" |
| 12 | + "github.com/mdlayher/netlink" |
| 13 | + "github.com/mdlayher/netlink/nlenc" |
| 14 | +) |
| 15 | + |
| 16 | +// Family name |
| 17 | +const IOAM6_GENL_NAME string = "IOAM6" |
| 18 | + |
| 19 | +// Attributes |
| 20 | +const IOAM6_ATTR_SOCKFD uint16 = 8 |
| 21 | +const IOAM6_ATTR_ID_HIGH uint16 = 9 |
| 22 | +const IOAM6_ATTR_ID_LOW uint16 = 10 |
| 23 | +const IOAM6_ATTR_SUBID uint16 = 11 |
| 24 | + |
| 25 | +// Commands |
| 26 | +const IOAM6_CMD_PKT_ID_ENABLE uint8 = 8 |
| 27 | +const IOAM6_CMD_PKT_ID_DISABLE uint8 = 9 |
| 28 | + |
| 29 | +/* |
| 30 | +Create socket for IOAM6 generic netlink and get the IOAM6 generic netlink family. |
| 31 | +
|
| 32 | +Return: |
| 33 | +- Created connection if no error. Else, nil. |
| 34 | +- IOAM6 generic netlink family if no error. Else, empty family. |
| 35 | +- Error if any. Else, nil. |
| 36 | +*/ |
| 37 | +func ioam6_generic_netlink() (*genetlink.Conn, genetlink.Family, error) { |
| 38 | + // Create generic netlink socket to interact with kernel |
| 39 | + c, err := genetlink.Dial(nil) |
| 40 | + if err != nil { |
| 41 | + log.Printf("failed to dial generic netlink: %v\n", err) |
| 42 | + return nil, genetlink.Family{}, err |
| 43 | + } |
| 44 | + |
| 45 | + // Get family because depending on kernel the id for the family can change |
| 46 | + family, err := c.GetFamily(IOAM6_GENL_NAME) |
| 47 | + if err != nil { |
| 48 | + if errors.Is(err, os.ErrNotExist) { |
| 49 | + log.Printf("%q family not available", IOAM6_GENL_NAME) |
| 50 | + return nil, genetlink.Family{}, err |
| 51 | + } |
| 52 | + |
| 53 | + log.Fatalf("failed to query for family: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + return c, family, nil |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | +Enable CLT on the given socket for the given trace and span ids. |
| 61 | +
|
| 62 | +Parameters: |
| 63 | +- socketFD: socket on which to enable clt |
| 64 | +- traceIDHigh: MSB of trace id |
| 65 | +- traceIDLow: LSB of trace id |
| 66 | +- spanID: span id |
| 67 | +
|
| 68 | +Return: |
| 69 | +Error if any. Else, nil. |
| 70 | +*/ |
| 71 | +func clt_enable(socketFD uint32, traceIDHigh uint64, traceIDLow uint64, spanID uint64) error { |
| 72 | + c, family, err := ioam6_generic_netlink() |
| 73 | + if err != nil { |
| 74 | + fmt.Println("Error while establishing generic netlink socket") |
| 75 | + return err |
| 76 | + } |
| 77 | + defer c.Close() |
| 78 | + |
| 79 | + // Create attributes for the request |
| 80 | + b, err := netlink.MarshalAttributes([]netlink.Attribute{ |
| 81 | + { |
| 82 | + Type: IOAM6_ATTR_SOCKFD, |
| 83 | + Data: nlenc.Uint32Bytes(socketFD), |
| 84 | + }, |
| 85 | + { |
| 86 | + Type: IOAM6_ATTR_ID_HIGH, |
| 87 | + Data: nlenc.Uint64Bytes(traceIDHigh), |
| 88 | + }, |
| 89 | + { |
| 90 | + Type: IOAM6_ATTR_ID_LOW, |
| 91 | + Data: nlenc.Uint64Bytes(traceIDLow), |
| 92 | + }, |
| 93 | + { |
| 94 | + Type: IOAM6_ATTR_SUBID, |
| 95 | + Data: nlenc.Uint64Bytes(spanID), |
| 96 | + }, |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + fmt.Println("Cannot create attributes for generic netlink message") |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + // Create message with headers + attributes |
| 104 | + req := genetlink.Message{ |
| 105 | + Header: genetlink.Header{ |
| 106 | + Command: IOAM6_CMD_PKT_ID_ENABLE, |
| 107 | + Version: uint8(family.Version), |
| 108 | + }, |
| 109 | + Data: b, |
| 110 | + } |
| 111 | + |
| 112 | + // Send message |
| 113 | + flags := netlink.Request | netlink.Acknowledge |
| 114 | + if _, err = c.Execute(req, family.ID, flags); err != nil { |
| 115 | + fmt.Println("clt_enable execute error: " + err.Error()) |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +/* |
| 123 | +Disable CLT on the given socket. |
| 124 | +
|
| 125 | +Parameters: |
| 126 | +- socketFD: socket on which to disable clt |
| 127 | +
|
| 128 | +Return: |
| 129 | +Error if any. Else, nil. |
| 130 | +*/ |
| 131 | +func clt_disable(socketFD uint32) error { |
| 132 | + c, family, err := ioam6_generic_netlink() |
| 133 | + if err != nil { |
| 134 | + fmt.Println("Error while establishing generic netlink socket") |
| 135 | + return err |
| 136 | + } |
| 137 | + defer c.Close() |
| 138 | + |
| 139 | + // Create attributes for the request |
| 140 | + b, err := netlink.MarshalAttributes([]netlink.Attribute{ |
| 141 | + { |
| 142 | + Type: IOAM6_ATTR_SOCKFD, |
| 143 | + Data: nlenc.Uint32Bytes(socketFD), |
| 144 | + }, |
| 145 | + }) |
| 146 | + if err != nil { |
| 147 | + fmt.Println("Cannot create attributes for generic netlink message") |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + // Create message with headers + attributes |
| 152 | + req := genetlink.Message{ |
| 153 | + Header: genetlink.Header{ |
| 154 | + Command: IOAM6_CMD_PKT_ID_DISABLE, |
| 155 | + Version: uint8(family.Version), |
| 156 | + }, |
| 157 | + Data: b, |
| 158 | + } |
| 159 | + |
| 160 | + // Send message |
| 161 | + flags := netlink.Request | netlink.Acknowledge |
| 162 | + if _, err = c.Execute(req, family.ID, flags); err != nil { |
| 163 | + fmt.Println("clt_disable execute error: " + err.Error()) |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
0 commit comments