github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/config/utils.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package config
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  
    10  	"github.com/cilium/cilium/pkg/byteorder"
    11  	"github.com/cilium/cilium/pkg/common"
    12  )
    13  
    14  // FmtDefineAddress returns the a define string from the given name and addr.
    15  // Example:
    16  // fmt.Print(FmtDefineAddress("foo", []byte{1, 2, 3})) // "#define foo { .addr = { 0x1, 0x2, 0x3 } }\n"
    17  func FmtDefineAddress(name string, addr []byte) string {
    18  	return fmt.Sprintf("#define %s { .addr = { %s } }\n", name, common.GoArray2C(addr))
    19  }
    20  
    21  // defineUint16 writes the C definition for an unsigned 16-bit value.
    22  func defineUint16(name string, value uint16) string {
    23  	return fmt.Sprintf("DEFINE_U16(%s, %#04x);\t/* %d */\n#define %s fetch_u16(%s)\n",
    24  		name, value, value, name, name)
    25  }
    26  
    27  // defineUint32 writes the C definition for an unsigned 32-bit value.
    28  func defineUint32(name string, value uint32) string {
    29  	return fmt.Sprintf("DEFINE_U32(%s, %#08x);\t/* %d */\n#define %s fetch_u32(%s)\n",
    30  		name, value, value, name, name)
    31  }
    32  
    33  // defineIPv4 writes the C definition for the given IPv4 address.
    34  func defineIPv4(name string, addr []byte) string {
    35  	if len(addr) != net.IPv4len {
    36  		return fmt.Sprintf("/* BUG: bad ip define %s %s */\n", name, common.GoArray2C(addr))
    37  	}
    38  	nboAddr := byteorder.NetIPv4ToHost32(addr)
    39  	return defineUint32(name, nboAddr)
    40  }
    41  
    42  // defineIPv6 writes the C definition for the given IPv6 address.
    43  func defineIPv6(name string, addr []byte) string {
    44  	if len(addr) != net.IPv6len {
    45  		return fmt.Sprintf("/* BUG: bad ip define %s %s */\n", name, common.GoArray2C(addr))
    46  	}
    47  	return fmt.Sprintf("DEFINE_IPV6(%s, %s);\n#define %s_V\n",
    48  		name, common.GoArray2C(addr), name)
    49  }
    50  
    51  func dumpRaw(name string, addr []byte) string {
    52  	return fmt.Sprintf(" %s%s\n", name, common.GoArray2C(addr))
    53  }
    54  
    55  // defineMAC writes the C definition for the given MAC name and addr.
    56  func defineMAC(name string, addr []byte) string {
    57  	if len(addr) != 6 { /* MAC len */
    58  		return fmt.Sprintf("/* BUG: bad mac define %s %s */\n", name, common.GoArray2C(addr))
    59  	}
    60  	return fmt.Sprintf("DEFINE_MAC(%s, %s);\n#define %s fetch_mac(%s)\n",
    61  		name, common.GoArray2C(addr), name, name)
    62  }
    63  
    64  // declareConfig writes the C macro for declaring a global configuration variable that can be
    65  // modified at runtime.
    66  func declareConfig(name string, value any, description string) string {
    67  	var t string
    68  	switch value.(type) {
    69  	case uint16:
    70  		t = "__u16"
    71  	case uint32:
    72  		t = "__u32"
    73  	case uint64:
    74  		t = "__u64"
    75  	default:
    76  		return fmt.Sprintf("/* BUG: %s has invalid type for DECLARE_CONFIG: %T*/\n", name, value)
    77  	}
    78  	return fmt.Sprintf("DECLARE_CONFIG(%s, %s, \"%s\");\n", t, name, description)
    79  }
    80  
    81  // assignConfig writes the C macro for assigning a value to the given config variable at compile
    82  // time. This value can be overridden at runtime.
    83  func assignConfig(name string, value any) string {
    84  	var t string
    85  	switch value.(type) {
    86  	case uint16:
    87  		t = "__u16"
    88  	case uint32:
    89  		t = "__u32"
    90  	case uint64:
    91  		t = "__u64"
    92  	default:
    93  		return fmt.Sprintf("/* BUG: %s has invalid type for ASSIGN_CONFIG: %T/*\n", name, value)
    94  	}
    95  	return fmt.Sprintf("ASSIGN_CONFIG(%s, %s, %v);\n", t, name, value)
    96  }