Skip to content

Commit

Permalink
Send vl_api_sockclnt_delete_t message on disconnect (#248)
Browse files Browse the repository at this point in the history
This change practically reverts previous commit [1548c7e](1548c7e), because it seems VPP is not correctly cleaning up opened sockets when they are closed from client side, which increases CPU usage of VPP.
This correctly sets client_index field for the message unlike previous implementation.

Signed-off-by: Ondrej Fabry <[email protected]>
  • Loading branch information
ondrej-fabry authored Nov 13, 2024
1 parent 4574cf0 commit 2d443be
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions adapter/socketclient/socketclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,10 @@ func (c *Client) Disconnect() error {
// wait for readerLoop to return
c.wg.Wait()

// Don't bother sending a vl_api_sockclnt_delete_t message,
// just close the socket.
if err := c.close(); err != nil {
log.Debugf("closing failed: %v", err)
}

if err := c.disconnect(); err != nil {
return err
}
Expand Down Expand Up @@ -408,6 +410,49 @@ func (c *Client) open(clientName string) error {
return nil
}

func (c *Client) close() error {
var msgCodec = codec.DefaultCodec

req := &memclnt.SockclntDelete{
Index: c.clientIndex,
}
msg, err := msgCodec.EncodeMsg(req, c.sockDelMsgId)
if err != nil {
log.Debugln("Encode error:", err)
return err
}
setMsgRequestHeader(msg, c.clientIndex, uint32(deleteMsgContext))

log.Infof("sending socklntDel (%d bytes): % 0X", len(msg), msg)

if err := c.writeMsg(msg); err != nil {
log.Debugln("Write error: ", err)
return err
}

msgReply, err := c.readMsgTimeout(nil, c.disconnectTimeout)
if err != nil {
var nerr net.Error
if errors.As(err, &nerr) && nerr.Timeout() {
log.Info("sockclnt_delete_reply: timeout")
// we accept timeout for reply
return nil
}
log.Debugln("Read sockclnt_delete_reply error:", err)
return err
}

reply := new(memclnt.SockclntDeleteReply)
if err := msgCodec.DecodeMsg(msgReply, reply); err != nil {
log.Debugln("Decoding sockclnt_delete_reply failed:", err)
return err
} else if reply.Response != 0 {
return fmt.Errorf("sockclnt_delete_reply: response error (%d)", reply.Response)
}

return nil
}

func (c *Client) setMsgTable(msgTable map[string]uint16, sockDelMsgId uint16) {
c.msgTableMu.Lock()
defer c.msgTableMu.Unlock()
Expand Down

0 comments on commit 2d443be

Please sign in to comment.