factor out sernum handling.

make incrementing sernum atomic.
declare variables for atomic operations as volatile.
This commit is contained in:
mlelstv 2016-06-01 04:19:08 +00:00
parent 1bafcf7e51
commit 62dba08e32
3 changed files with 45 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: iscsi_globals.h,v 1.14 2016/05/29 13:51:16 mlelstv Exp $ */
/* $NetBSD: iscsi_globals.h,v 1.15 2016/06/01 04:19:08 mlelstv Exp $ */
/*-
* Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
@ -289,7 +289,7 @@ struct ccb_s {
int sense_len_got; /* actual sense data length */
pdu_t *pdu_waiting; /* PDU waiting to be ack'ed */
uint32_t CmdSN; /* CmdSN associated with waiting PDU */
volatile uint32_t CmdSN; /* CmdSN associated with waiting PDU */
int flags;
connection_t *connection; /* connection for CCB */
@ -363,7 +363,7 @@ struct connection_s {
/* if closing down: status */
int recover; /* recovery count */
/* (reset on first successful data transfer) */
unsigned usecount; /* number of active CCBs */
volatile unsigned usecount; /* number of active CCBs */
bool destroy; /* conn will be destroyed */
bool in_session;
@ -724,6 +724,9 @@ void init_sernum(sernum_buffer_t *);
int add_sernum(sernum_buffer_t *, uint32_t);
uint32_t ack_sernum(sernum_buffer_t *, uint32_t);
uint32_t get_sernum(session_t *, bool);
int sernum_in_window(session_t *);
/* in iscsi_text.c */
int assemble_login_parameters(connection_t *, ccb_t *, pdu_t *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: iscsi_send.c,v 1.16 2016/05/29 13:51:16 mlelstv Exp $ */
/* $NetBSD: iscsi_send.c,v 1.17 2016/06/01 04:19:08 mlelstv Exp $ */
/*-
* Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
@ -145,6 +145,7 @@ reassign_tasks(connection_t *oldconn)
pdu_t *opdu;
int no_tm = 1;
int rc = 1;
uint32_t sn;
if ((conn = assign_connection(sess, FALSE)) == NULL) {
DEB(1, ("Reassign_tasks of Session %d, connection %d failed, "
@ -255,14 +256,12 @@ reassign_tasks(connection_t *oldconn)
mutex_enter(&sess->lock);
if (ccb->CmdSN < sess->ExpCmdSN) {
pdu = ccb->pdu_waiting;
sn = get_sernum(sess, !(pdu->pdu.Opcode & OP_IMMEDIATE));
/* update CmdSN */
DEBC(conn, 1, ("Resend Updating CmdSN - old %d, new %d\n",
ccb->CmdSN, sess->CmdSN));
ccb->CmdSN = sess->CmdSN;
if (!(pdu->pdu.Opcode & OP_IMMEDIATE)) {
sess->CmdSN++;
}
ccb->CmdSN = sn;
pdu->pdu.p.command.CmdSN = htonl(ccb->CmdSN);
}
mutex_exit(&sess->lock);
@ -1351,7 +1350,7 @@ send_command(ccb_t *ccb, ccb_disp_t disp, bool waitok, bool immed)
mutex_enter(&sess->lock);
while (/*CONSTCOND*/ISCSI_THROTTLING_ENABLED &&
/*CONSTCOND*/!ISCSI_SERVER_TRUSTED &&
!sn_a_le_b(sess->CmdSN, sess->MaxCmdSN)) {
!sernum_in_window(sess)) {
ccb->disp = disp;
if (waitok)
@ -1419,9 +1418,7 @@ send_command(ccb_t *ccb, ccb_disp_t disp, bool waitok, bool immed)
ccb->flags |= CCBF_REASSIGN;
mutex_enter(&sess->lock);
ccb->CmdSN = sess->CmdSN;
if (!immed)
sess->CmdSN++;
ccb->CmdSN = get_sernum(sess, !immed);
mutex_exit(&sess->lock);
pdu->p.command.CmdSN = htonl(ccb->CmdSN);

View File

@ -1,4 +1,4 @@
/* $NetBSD: iscsi_utils.c,v 1.9 2016/05/29 13:51:16 mlelstv Exp $ */
/* $NetBSD: iscsi_utils.c,v 1.10 2016/06/01 04:19:08 mlelstv Exp $ */
/*-
* Copyright (c) 2004,2005,2006,2008 The NetBSD Foundation, Inc.
@ -678,3 +678,35 @@ ack_sernum(sernum_buffer_t *buff, uint32_t num)
return buff->ExpSN;
}
/*
* next_sernum:
* Return the current command serial number of the session
* and optionally increment it for the next query
*/
uint32_t
get_sernum(session_t *sess, bool bump)
{
uint32_t sn;
KASSERT(mutex_owned(&sess->lock));
sn = sess->CmdSN;
if (bump)
atomic_inc_32(&sess->CmdSN);
return sn;
}
/*
* sernum_in_window:
* Check wether serial number is in send window
*
*/
int
sernum_in_window(session_t *sess)
{
KASSERT(mutex_owned(&sess->lock));
return sn_a_le_b(sess->CmdSN, sess->MaxCmdSN);
}