added debug-level logging

This commit is contained in:
Dmitri Dolguikh 2017-10-24 14:12:50 -07:00 committed by Dave Anderson
parent 2c42665a0b
commit 11fbcd1ae1
5 changed files with 24 additions and 17 deletions

View File

@ -22,6 +22,7 @@ import (
// qemu-system-x86_64 -L . --bios /usr/share/edk2-firmware/ipv6/OVMF.fd -netdev bridge,br=br1,id=net0 -device virtio-net-pci,netdev=net0
// sudo ./pixiecore bootipv6 --listen-addr=2001:db8:f00f:cafe::4/64 --httpboot-url=http://[2001:db8:f00f:cafe::4]/bootx64.efi --ipxe-url=http://[2001:db8:f00f:cafe::4]/script.ipxe
// sudo ./pixiecore ipv6api --listen-addr=2001:db8:f00f:cafe::4 --api-request-url=http://[2001:db8:f00f:cafe::4]:8888
func main() {
cli.Ipxe[pixiecore.FirmwareX86PC] = ipxe.MustAsset("undionly.kpxe")

View File

@ -26,8 +26,15 @@ var bootIPv6Cmd = &cobra.Command{
s := pixiecorev6.NewServerV6()
s.Log = logWithStdFmt
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
s.Debug = logWithStdFmt
}
if debug { s.Debug = logWithStdFmt }
if addr == "" {
fatalf("Please specify address to listen on")
fatalf("Please specify address to bind to")
} else {
}
if ipxeUrl == "" {
@ -48,6 +55,7 @@ func serverv6ConfigFlags(cmd *cobra.Command) {
cmd.Flags().StringP("listen-addr", "", "", "IPv6 address to listen on")
cmd.Flags().StringP("ipxe-url", "", "", "IPXE config file url, e.g. http://[2001:db8:f00f:cafe::4]/script.ipxe")
cmd.Flags().StringP("httpboot-url", "", "", "HTTPBoot url, e.g. http://[2001:db8:f00f:cafe::4]/bootx64.efi")
cmd.Flags().Bool("debug", false, "Enable debug-level logging")
}
func init() {

View File

@ -26,9 +26,15 @@ var ipv6ApiCmd = &cobra.Command{
}
s := pixiecorev6.NewServerV6()
s.Log = logWithStdFmt
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
s.Debug = logWithStdFmt
}
if debug { s.Debug = logWithStdFmt }
if addr == "" {
fatalf("Please specify address to listen on")
fatalf("Please specify address to bind to")
} else {
}
if apiUrl == "" {
@ -45,11 +51,12 @@ var ipv6ApiCmd = &cobra.Command{
func serverv6ApiConfigFlags(cmd *cobra.Command) {
cmd.Flags().StringP("listen-addr", "", "", "IPv6 address to listen on")
cmd.Flags().StringP("api-request-url", "", "", "Ipv6-specific API server url")
cmd.Flags().Duration("api-request-timeout", 5*time.Second, "Timeout for request to the API server")
cmd.Flags().Bool("debug", false, "Enable debug-level logging")
}
func init() {
rootCmd.AddCommand(ipv6ApiCmd)
serverv6ApiConfigFlags(ipv6ApiCmd)
ipv6ApiCmd.Flags().Duration("api-request-timeout", 5*time.Second, "Timeout for request to the API server")
}

View File

@ -6,18 +6,18 @@ import (
)
func (s *ServerV6) serveDHCP(conn *dhcp6.Conn, packetBuilder *dhcp6.PacketBuilder) error {
s.log("dhcpv6", "Waiting for packets...\n")
s.debug("dhcpv6", "Waiting for packets...\n")
for {
pkt, src, err := conn.RecvDHCP()
if err != nil {
return fmt.Errorf("Error receiving DHCP packet: %s", err)
}
if err := pkt.ShouldDiscard(s.Duid); err != nil {
s.log("dhcpv6", fmt.Sprintf("Discarding (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, err))
s.debug("dhcpv6", fmt.Sprintf("Discarding (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, err))
continue
}
s.log("dhcpv6", fmt.Sprintf("Received (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, pkt.Options.HumanReadable()))
s.debug("dhcpv6", fmt.Sprintf("Received (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, pkt.Options.HumanReadable()))
response, err := packetBuilder.BuildResponse(pkt)
if err != nil {
@ -40,6 +40,6 @@ func (s *ServerV6) serveDHCP(conn *dhcp6.Conn, packetBuilder *dhcp6.PacketBuilde
continue
}
s.log("dhcpv6", fmt.Sprintf("Sent (%d) packet (%d): %s\n", response.Type, response.TransactionID, response.Options.HumanReadable()))
s.debug("dhcpv6", fmt.Sprintf("Sent (%d) packet (%d): %s\n", response.Type, response.TransactionID, response.Options.HumanReadable()))
}
}

View File

@ -2,7 +2,6 @@ package pixiecorev6
import (
"go.universe.tf/netboot/dhcp6"
"sync"
"fmt"
"time"
"encoding/binary"
@ -17,18 +16,13 @@ type ServerV6 struct {
errs chan error
eventsMu sync.Mutex
Log func(subsystem, msg string)
Debug func(subsystem, msg string)
}
func NewServerV6() *ServerV6 {
log := func(subsystem, msg string) { fmt.Printf("[%s] %s", subsystem, msg) }
ret := &ServerV6{
Port: "547",
Log: log,
Debug: log,
}
return ret
}
@ -41,7 +35,7 @@ func (s *ServerV6) Serve() error {
return err
}
s.log("dhcp", "new connection...")
s.debug("dhcp", "new connection...")
// 5 buffer slots, one for each goroutine, plus one for
// Shutdown(). We only ever pull the first error out, but shutdown
@ -50,12 +44,9 @@ func (s *ServerV6) Serve() error {
// blocking.
s.errs = make(chan error, 6)
//s.debug("Init", "Starting Pixiecore goroutines")
s.SetDUID(dhcp.SourceHardwareAddress())
addressPool := dhcp6.NewRandomAddressPool(net.ParseIP("2001:db8:f00f:cafe::10"), net.ParseIP("2001:db8:f00f:cafe::100"), 1850)
// bootConfiguration := dhcp6.MakeStaticBootConfiguration("http://[2001:db8:f00f:cafe::4]/bootx64.efi", "http://[2001:db8:f00f:cafe::4]/script.ipxe")
// bootConfiguration := dhcp6.MakeApiBootConfiguration("http://[2001:db8:f00f:cafe::4]:8888/", 10 *time.Second)
packetBuilder := dhcp6.MakePacketBuilder(s.Duid, 1800, 1850, s.BootUrls, addressPool)
go func() { s.errs <- s.serveDHCP(dhcp, packetBuilder) }()