github.com/elfadel/cilium@v1.6.12/pkg/datapath/linux/utils.go (about) 1 // Copyright 2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package linux 16 17 import ( 18 "fmt" 19 "net" 20 "reflect" 21 22 "github.com/cilium/cilium/pkg/byteorder" 23 ) 24 25 // goArray2C transforms a byte slice into its hexadecimal string representation. 26 // Example: 27 // array := []byte{0x12, 0xFF, 0x0, 0x01} 28 // fmt.Print(GoArray2C(array)) // "{ 0x12, 0xff, 0x0, 0x1 }" 29 func goArray2C(array []byte) string { 30 ret := "" 31 32 for i, e := range array { 33 if i == 0 { 34 ret = ret + fmt.Sprintf("%#x", e) 35 } else { 36 ret = ret + fmt.Sprintf(", %#x", e) 37 } 38 } 39 return ret 40 } 41 42 // FmtDefineAddress returns the a define string from the given name and addr. 43 // Example: 44 // fmt.Print(FmtDefineAddress("foo", []byte{1, 2, 3})) // "#define foo { .addr = { 0x1, 0x2, 0x3 } }\n" 45 func FmtDefineAddress(name string, addr []byte) string { 46 return fmt.Sprintf("#define %s { .addr = { %s } }\n", name, goArray2C(addr)) 47 } 48 49 // defineUint32 writes the C definition for an unsigned 32-bit value. 50 func defineUint32(name string, value uint32) string { 51 return fmt.Sprintf("DEFINE_U32(%s, %#08x);\t/* %d */\n#define %s fetch_u32(%s)\n", 52 name, value, value, name, name) 53 } 54 55 // defineIPv4 writes the C definition for the given IPv4 address. 56 func defineIPv4(name string, addr []byte) string { 57 if len(addr) != net.IPv4len { 58 return fmt.Sprintf("/* BUG: bad ip define %s %s */\n", name, goArray2C(addr)) 59 } 60 nboAddr := byteorder.HostSliceToNetwork(addr, reflect.Uint32).(uint32) 61 return defineUint32(name, nboAddr) 62 } 63 64 // defineIPv6 writes the C definition for the given IPv6 address. 65 func defineIPv6(name string, addr []byte) string { 66 if len(addr) != net.IPv6len { 67 return fmt.Sprintf("/* BUG: bad ip define %s %s */\n", name, goArray2C(addr)) 68 } 69 return fmt.Sprintf("DEFINE_IPV6(%s, %s);\n", name, goArray2C(addr)) 70 } 71 72 func dumpRaw(name string, addr []byte) string { 73 return fmt.Sprintf(" %s%s\n", name, goArray2C(addr)) 74 } 75 76 // defineMAC writes the C definition for the given MAC name and addr. 77 func defineMAC(name string, addr []byte) string { 78 if len(addr) != 6 { /* MAC len */ 79 return fmt.Sprintf("/* BUG: bad mac define %s %s */\n", name, goArray2C(addr)) 80 } 81 return fmt.Sprintf("DEFINE_MAC(%s, %s);\n#define %s fetch_mac(%s)\n", 82 name, goArray2C(addr), name, name) 83 }